2

クライアント向けのページを作成しましたが、ニュースレターを作成する必要があります。私はすでにメールのテンプレートを持っています。私が望むのは、それらのメールに画像を挿入することです。

コード :

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が、うまくいきませんでした。

4

2 に答える 2

7

http://swiftmailer.org/docs/messages.htmlの例を次に示します。

必要に応じて、2 段階でファイルを埋め込むことができます。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'
);
于 2013-04-17T21:24:31.507 に答える