Php 将内联图像添加到使用swiftmailer发送的邮件中
请原谅我的php,但是,我使用Swiftmailer从客户网站发送电子邮件。他们要求添加一两张图片作为签名等,因此请查看这里的swiftmailer规范 他们建议要么添加这样的内联图像Php 将内联图像添加到使用swiftmailer发送的邮件中,php,swiftmailer,Php,Swiftmailer,请原谅我的php,但是,我使用Swiftmailer从客户网站发送电子邮件。他们要求添加一两张图片作为签名等,因此请查看这里的swiftmailer规范 他们建议要么添加这样的内联图像 $message->embed(Swift_Image::fromPath('http://site.tld/image here')) 或者像这样(分两步) 然后在邮件正文部分添加 <img src="' . $cid . '" alt="Image" />' 我唯一添加到我已经在工作的
$message->embed(Swift_Image::fromPath('http://site.tld/image here'))
或者像这样(分两步)
然后在邮件正文部分添加
<img src="' . $cid . '" alt="Image" />'
我唯一添加到我已经在工作的代码和电子邮件中的是直接来自文档页面中示例的图像代码。此错误显然会阻止发送电子邮件。如果我删除它,它就会发送电子邮件。因为我需要给它添加一个图像
非常感谢您的帮助。多谢各位
编辑:这是生成和发送电子邮件的部分
$cid=$message->embed(Swift_EmbeddedFile::fromPath(“”))
->setTo($docEmail)
->setBody(“你好”。\r\n\r\n”。
$fullName。“已访问MyForeCYTE.com。访问后,他们要求了解有关测试的更多信息。\r\n\r\n”。
“请访问www.ClarityWomensHealth.com以了解有关ForeCYTE乳腺健康测试的更多信息,或致电1(877)722-6339联系我们的客户支持热线。\r\n\r\n”。
“我们期待收到您的来信。\r\n\r\n”。
“谢谢,”,“文本/普通”)
->添加部分(“你好”。
”。
“.$fullName.”已访问www.MyForeCYTE.com。访问后,他们要求了解更多有关测试的信息。
”。
“请访问www.ClarityWomensHealth.com以了解有关ForeCYTE乳腺健康测试的更多信息,或致电1(877)722-6339联系我们的客户支持热线。
”。
“我们期待着您的回复。
”。
“谢谢”,“text/html”)
;
经过一番周旋,我找到了另一种解决方案。Swiftmailer允许两种方法执行相同的操作
一个是embed()函数
另一个是attach()函数
因此,对于上面的代码,我删除了“embed()”,因为它对我不起作用,并在下面添加了这两行代码,它就起作用了
->attach(Swift_Attachment::fromPath('path to image here.jpg')
->setDisposition('inline'));
它100%有效被接受的答案不起作用(测试版本:5.4.2)。 (矿山可行,但可以完善) 相反,在“原件”(Gmail:Show Original)中,我发现swiftmailer没有在附件中添加两个标题,即:
Content-ID: <ABC123>
X-Attachment-Id: ABC123
Content-ID::我找到了为swiftmailer修复它的方法(这甚至是针对swiftmailer文档的,但它可以工作,而他们的却不能)
这是最终(丑陋的)代码:
$attachment=Swift_attachment::fromPath('image.jpg')->setDisposition('inline');
$attachment->getHeaders()->addTextHeader('Content-ID','');
$attachment->getHeaders()->addTextHeader('X-attachment-Id','ABC123');
$cid=$message->embed($attachment);
$img='';
$html=”
$img
";
$message->setBody($html,'text/html');
没有一个答案真正适合我。我必须使用CID包含内联图像。我必须做的是,让它工作:
$attachment = Swift_Image::newInstance($data, $filename, $mimeType)
->setDisposition('inline');
$cid = $message->embed($attachment); // Generates "cid:something"
重要的部分是使用Swift_图像类。那么html中的图像应该是:
<img src="cid:something" ... />
我认为这个解决方案在不使用swiftmailer(5.4.2版)的情况下工作。这正是文件所说的
如果内联图像有效,请记住测试多个电子邮件客户端(gmail、thunderbird、apple mail、web客户端…)。例如,使用Swift_附件,在线图像显示在gmail中,但在某些web客户端中不显示。使用Swift\u Image,它可以在任何地方工作。如果使用laravel,请尝试在路径开头添加public\u path()
,例如:
<img src="<?php echo $message->embed(public_path().'/img/backend/name.jpg); ?>">
embed(public_path()。/img/backend/name.jpg);?>">
我知道这是一个老生常谈的问题(有公认的答案),但对于任何有相同问题的人,该方法都希望映像路径是本地的(文件系统路径):
因此,如果您的映像路径是外部的(从https://
等开始),则以下操作将失败:
对于外部图像,应使用:
# Load the image file
$data = file_get_contents($image_path);
# Get the filename
$filename = explode("?",basename($image_path))[0];
# Get the mime type
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mime_type = $finfo->buffer($buffer);
# Load the image file
$image_file = new \Swift_Image($data, $filename, $mime_type);
# Set the disposition to inline
$image_file->setDisposition('inline');
由于您没有在此处显示图像的文件名
,因此很难判断其格式是否正确。您能否显示构建消息的完整代码?ask-doc-proc2.php
文件的第89行
上有什么内容?要回答您的问题@Fred,“此处的图像”是“filename.jpg”。至于“89行上有什么内容,它是这样的,“$cid=$message->embed(Swift_Image::fromPath(');”@somdow您是否尝试过添加http://
,或者如果您在www
-http://www.
在myforecyte.com
之前?这是一个有效的解决方案,非常感谢您提供这段代码!您在$message
上调用它,就像$message->attach(Swift_Attachment::fromPath('path to image here.jpg')->setDisposition('inline')代码>?
$attachment = Swift_Image::newInstance($data, $filename, $mimeType)
->setDisposition('inline');
$cid = $message->embed($attachment); // Generates "cid:something"
<img src="cid:something" ... />
<img src="<?php echo $message->embed(public_path().'/img/backend/name.jpg); ?>">
Swift_Image::fromPath("/local/path/to/image.jpg");
// Local path = great success
Swift_Image::fromPath("https://external.path/to/image.jpg");
// External path = epic fail
# Load the image file
$data = file_get_contents($image_path);
# Get the filename
$filename = explode("?",basename($image_path))[0];
# Get the mime type
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mime_type = $finfo->buffer($buffer);
# Load the image file
$image_file = new \Swift_Image($data, $filename, $mime_type);
# Set the disposition to inline
$image_file->setDisposition('inline');