2

I have developed a page for a client, and I have to do a newsletter. I already have the template of the mail and what I want is to insert a picture in those emails.

The code :

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);  
}

This code works and I have a nice email page but what I don't know is how to insert a picture in it.

I tried to insert a picture in the email.html.twig but it didn't work.

4

2 回答 2

7

Here is the example from http://swiftmailer.org/docs/messages.html:

You can embed files in two stages if you prefer. Just capture the return value of embed() in a variable and use that as the src attribute.

// 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'
);
于 2013-04-17T21:24:31.507 回答
2

Put your images on a server. Then link them with an absolute url path in the html of the newsletter. The images will simply be requested from that server.

于 2013-04-17T18:27:08.223 回答