将内嵌图像添加到使用swiftmailer发送的消息中

som*_*dow 11 php swiftmailer

请原谅我的PHP但是,我使用Swiftmailer从客户网站发送电子邮件.他们要求添加一个或两个图像作为签名等,所以在这里查看swiftmailer规范

http://swiftmailer.org/docs/messages.html

他们建议添加这样的内嵌图像

$message->embed(Swift_Image::fromPath('http://site.tld/image here'))
Run Code Online (Sandbox Code Playgroud)

或者像这样(分2步)

$cid = $message->embed(Swift_Image::fromPath('image here'));
Run Code Online (Sandbox Code Playgroud)

然后在电子邮件正文部分添加

<img src="' . $cid . '" alt="Image" />'
Run Code Online (Sandbox Code Playgroud)

我试过这两个步骤但无济于事.当我点击发送电子邮件按钮,我得到这个错误,我不知道该怎么做.

Call to a member function embed() on a non-object in /home/content/78/5152878/html/4testing/erase/ask-doc-proc2.php on line 89
Run Code Online (Sandbox Code Playgroud)

我添加到我已经工作的代码和电子邮件中的唯一内容是直接来自文档页面中示例的图像代码.此错误显然会阻止发送电子邮件.如果我删除它然后它发送电子邮件很好.因为我需要为此添加图像,

任何帮助是极大的赞赏.谢谢

编辑:这是构建电子邮件的部分并发送 $ cid = $ message-> embed(Swift_EmbeddedFile :: fromPath(' http://myforecyte.com/dev/pic.jpg '));

->setTo( $docEmail)

->setBody("Hello" . "\r\n\r\n" .  
$fullName . " has visited MyForeCYTE.com. Upon their visit they have requested to learn more about the test. \r\n\r\n" . 
"Please visit www.ClarityWomensHealth.com to find out more about The ForeCYTE Breast Health Test, or call our customer support line at 1 (877) 722-6339.  \r\n\r\n" .
"We look forward to hearing from you. \r\n\r\n" .
"Thank You," , 'text/plain')

->addPart("Hello" . ",</b><br/><br/>" . 
"<b>" . $fullName . "</b> has visited www.MyForeCYTE.com. Upon their visit they have requested to learn more about the test. <br/>" . 
"Please visit www.ClarityWomensHealth.com to find out more about The ForeCYTE Breast Health Test, or call our customer support line at 1 (877) 722-6339.<br/> " . 
"We look forward to hearing from you. <br/><br/><br/>" . "<img src='" . $cid. "' alt='pic'/>" .

"Thank you " , 'text/html')
;
Run Code Online (Sandbox Code Playgroud)

som*_*dow 10

在所有的跑来跑去之后我找到了另一种解决方案.Swiftmailer允许2种方法执行相同的操作.

一个是embed()函数

另一个是attach()函数

所以对于上面的代码,我删除了"embed()",因为它不适合我,并在下面添加了这两行,它的工作原理

    ->attach(Swift_Attachment::fromPath('path to image here.jpg')  
->setDisposition('inline'));
Run Code Online (Sandbox Code Playgroud)

它工作100%


iai*_*air 6

接受的答案不起作用(测试版本:5.4.2)。(我的作品,但可以完善)

相反,查看“原始”(Gmail:显示原始)的内部,我发现 swiftmailer 省略了向附件添加 2 个标题,即:

Content-ID: <ABC123>
X-Attachment-Id: ABC123
Run Code Online (Sandbox Code Playgroud)

ABC123 是我们必须放入要显示内联内容的正文中的 cid:

所以感谢这个问题:我找到了为 swiftmailer 修复它的方法(这甚至违反了 swiftmailer 文档,但它可以工作,而他们的却没有)

这是最终的(丑陋的)代码:

$attachment = Swift_Attachment::fromPath('image.jpg')->setDisposition('inline');
$attachment->getHeaders()->addTextHeader('Content-ID', '<ABC123>');
$attachment->getHeaders()->addTextHeader('X-Attachment-Id', 'ABC123');
$cid = $message->embed($attachment);
$img = '<img src="cid:ABC123"/>';
$html = "
<html>
<head>
</head>
<body>
$img
</body>
</html>
";
$message->setBody($html, 'text/html');
Run Code Online (Sandbox Code Playgroud)


tom*_*lin 5

没有一个答案对我真正有用。我必须使用CID包括内嵌图像。我要做的是使其生效:

$attachment = Swift_Image::newInstance($data, $filename, $mimeType)
    ->setDisposition('inline');

$cid = $message->embed($attachment); // Generates "cid:something"
Run Code Online (Sandbox Code Playgroud)

重要的部分是使用Swift_Image类。那么html中的图像应为:

<img src="cid:something" ... />
Run Code Online (Sandbox Code Playgroud)

我认为此解决方案可以在不对swiftmailer(5.4.2版)进行任何改动的情况下工作。这正是文档所说的方式。

如果内嵌图片有效,请记住要测试多个电子邮件客户端(gmail,雷鸟,苹果邮件,网络客户端...)。例如,使用Swift_Attachment,内嵌图片会显示在gmail中,但不会显示在某些Web客户端中。使用Swift_Image,它可以在任何地方使用。