在 php 中发送嵌入二进制图像的电子邮件

Send email with embedding binary image in php

我正在尝试发送带有图像的电子邮件,但我的服务器中只有二进制代码。 知道怎么做吗?

现在我发送这样的 html(以 base 64 编码):

<img src="data:image/png; base64, iVBORw0KGgoAAAANSUhEUgAAANIAAANBCAYAAAC ..." />

我正在使用 Symfony2 中的 SwiftMailer 库来发送电子邮件。

完整的示例代码(二进制代码中有删减):

//params
$subject = "Demo e-mail";
$body = "<html>
    <table>
        <tr>
            <td>
                <img src='data:image/png; base64, iVBORw0KGgo...zIIAAAAASUVORK5CYII='>
            </td>
            <td style='padding-left:20px'>
                <div>
                    <h3>Product name</h3>
                    <h4>Code 3089</h4>
                    <p>14.70 $</p>
                </div>
              </td>
        </tr>
    </table>
    </html>";
$name = "Client name";
$email = "clientmail@domain.com";
$from_address = "mail@domain.com";
$from_name = "App Name";

//SwiftMessag eobject 
$message = \Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom(array($from_address => $from_name))
        ->setTo(array($email => $name))
        ->setBody($body, 'text/html');

//send email
$this->get('mailer')->send($message);

我使用以下代码在电子邮件中嵌入了一张图片:

$message = \Swift_Message::newInstance();

$body = '<html><head></head><body>'.
    '<p><img src="'.$message->embed(\Swift_Image::fromPath(
        \Swift_Attachment::fromPath([full path to you image]
    )
    ->setDisposition('inline'))).'" alt="Image" /></p>'.
    '</body></html>'
;

$message
    ->setSubject('Subject')
    ->setFrom($from)
    ->setTo($to)
    ->setBody(
        $body,
        'text/html'
    )
;

$this->mailer->send($message);

[full path to you image] 替换为您的图片路径,例如 dirname(__FILE__).'/../images/image.png'