将内嵌图像添加到使用 swiftmailer 发送的消息中 [英] add inline image to a message sent with swiftmailer

查看:20
本文介绍了将内嵌图像添加到使用 swiftmailer 发送的消息中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please excuse my php but, Im using Swiftmailer to send emails from a clients website. They've requested to add an image or two as a signature etc and so looking at the swiftmailer spec here

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

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

They suggest either adding an inline image like this

$message->embed(Swift_Image::fromPath('http://site.tld/image here'))

或者像这样(分两步)

$cid = $message->embed(Swift_Image::fromPath('image here'));

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

then in the emails body section add

<img src="' . $cid . '" alt="Image" />'

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

Both steps ive tried but to no avail. When i hit the send email button, i get this error which i dont quite know what to make of it.

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

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

The only thing i added to my already working code and email was the image code directly from the example in the docs pages. This error obviously prevents the email from being sent. if i remove it then it sends emails fine. Since i need to add an image to this,

非常感谢任何帮助.谢谢

Any help is greatly appreciated. Thank you

这是构建和发送电子邮件的部分$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')
;

推荐答案

在四处奔波之后,我找到了一个替代解决方案.Swiftmailer 允许使用 2 种方法来执行相同的操作.

After all the running around i found an alternate solution. Swiftmailer allows 2 methods in which to perform the same thing.

一个是embed()函数

one is the embed() function

另一个是 attach() 函数

and the other one is the attach() function

所以对于上面的代码,我删除了embed()",因为它对我不起作用,并在下面添加了这两行并且它起作用了

so to the code above, i removed the "embed()" since it wasn't working for me and added these 2 lines below and it works

    ->attach(Swift_Attachment::fromPath('path to image here.jpg')  
->setDisposition('inline'));

它 100% 有效

这篇关于将内嵌图像添加到使用 swiftmailer 发送的消息中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆