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


Send email with embedding binary image in php

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

现在我发送这样的html(以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'