symfony4 - 如何使用 swiftmailer 在电子邮件中添加图像?
问题描述
我正在尝试在电子邮件中添加图像。我使用 Symfony 4 和 SwiftMailer。我阅读了文档,论坛,但它不起作用。所以我尝试了:
$message = (new \Swift_Message('Title of the email'))
->setFrom('myemailadress@xxxxxx.com')
->setTo($data['email']);
$headers = $message->getHeaders();
$attachment = \Swift_Attachment::fromPath('assets/img/logo.png');
$headers = $attachment->getHeaders();
$message->setBody(
$this->renderView(
'resetemail.html.twig',
[ 'resetPassword' => $resetPassword], $headers
),
'text/html'
);
$mailer->send($message);
return $this->redirectToRoute("requestMail");
对于树枝,我做了:
<img id="customHeaderImage" align="top" label="Image" src="headers" alt="the title of the logo" class="w640" border="0" style="display: inline">
我还尝试将标题部分替换为: $img = $message->embed(Swift_Image::fromPath('assets/img/logo.png'));
如果您有解决方案,我很乐意阅读它。
谢谢。
解决方案
This is the solution that I found :
$message = (new \Swift_Message('Title of the email'))
->setFrom('myemailadress@xxxxxx.com')
->setTo($data['email'])
->attach(\Swift_Attachment::fromPath('build/logo.png')->setFilename('logo.png'));
So I attach the image to my message and I give it a name.
In my Twig I just put name in the src of the img.
推荐阅读
- javascript - JS星级评分系统
- java - 如何使用 java 套接字在不同网络之间进行通信?
- firebase - Firestore - 不允许一次读取多个文档
- python - AttributeError:“字节”对象没有属性“加密”
- angular - 如何在 Angular 中创建自定义指令
- data-structures - createBinaryTree 给出了一个无限循环,而 createBinarySearchTree 给出了分段错误
- node.js - docker-compose 给出关于不拥有数据文件夹的 mongo 错误
- ssh - 为什么我可以通过 SSH 连接而不使用所需的私钥?
- mysql - R Markdown,SQL代码块生成语法错误
- java - Android Apple Musickit 解码示例错误 -1inputFormat