首页 > 解决方案 > 如何使用 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'));

但我只有: 在此处输入图像描述

如果您有解决方案,我很乐意阅读它。

谢谢。

标签: symfony4swiftmailer

解决方案


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.


推荐阅读