Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 如何使用SwiftMailer(Symfony2)在html格式的电子邮件中添加图片_Email_Symfony_Insert_Image_Swiftmailer - Fatal编程技术网

Email 如何使用SwiftMailer(Symfony2)在html格式的电子邮件中添加图片

Email 如何使用SwiftMailer(Symfony2)在html格式的电子邮件中添加图片,email,symfony,insert,image,swiftmailer,Email,Symfony,Insert,Image,Swiftmailer,我已经为一个客户开发了一个页面,我还得做一份时事通讯。我已经有了邮件的模板,我想在这些邮件中插入一张图片 守则: foreach($customer as $customer) { $mail = $customer->getMailcustomer(); $message = \Swift_Message::newInstance(); $message->setSubject('Info'); $message->setFrom('abcd@noreply.

我已经为一个客户开发了一个页面,我还得做一份时事通讯。我已经有了邮件的模板,我想在这些邮件中插入一张图片

守则:

foreach($customer as $customer)
{
  $mail = $customer->getMailcustomer();
  $message = \Swift_Message::newInstance();
  $message->setSubject('Info');
  $message->setFrom('abcd@noreply.ch');
  $message->setTo($mail);
  $message->setContentType("text/html");

  $message->setBody(
    $this->renderView(
            'MyBundle:Customer:email.html.twig',
            array('form' => $form->createView())
    )
  );

  $this->get('mailer')->send($message);  
}
这段代码很有效,我有一个很好的电子邮件页面,但我不知道如何在其中插入图片


我试图在
email.html.twig
中插入图片,但没有成功。

将图像放到服务器上。然后将它们与新闻稿html中的绝对url路径链接。只需从该服务器请求图像即可。

以下是来自以下服务器的示例:

如果愿意,可以分两个阶段嵌入文件。只需在变量中捕获embed()的返回值,并将其用作src属性

// You can embed files from a URL if allow_url_fopen is on in php.ini
$message->setBody(
'<html>' .
' <head></head>' .
' <body>' .
'  Here is an image <img src="' .
     $message->embed(Swift_Image::fromPath('http://site.tld/logo.png')) .
   '" alt="Image" />' .
'  Rest of message' .
' </body>' .
'</html>',
  'text/html'
);
//如果php.ini中启用了allow\u URL\u fopen,则可以从URL嵌入文件
$message->setBody(
'' .
' ' .
' ' .
'这是一个嵌入的图像(Swift_image::fromPath('http://site.tld/logo.png')) .
““alt=”Image“/>”。
“消息的其余部分”。
' ' .
'',
“文本/html”
);

我确实同意你的观点,但你的句子措辞可能会被误解:)我重新措辞了我的答案,请原谅我的法语。