2016-05-24 4 views
0

に画像を追加することができません私は、cronジョブに取り組んで、私はループを反復処理するforeachループを使用して、ユーザーに電子メールを送信していますcronのAction内部ではSwiftMailersymfonyの - メール

を使用して電子メールを送信しようとしています。

foreach($hostDinners as $key => $hostDinner) { 
    //generate email 
$message = \Swift_Message::newInstance() 
    ->setSubject("This is a demo subject") 
    ->setFrom($this->container->getParameter('mailer_user')) 
    ->setTo($key) 

    ->setBody(
     $this->renderView(
      'AppBundle:emails:cron-job-request-and-invitations.html.twig', 
      array(
      'dinners' => $hostDinner 
      ) 
     ) 
    ) 
    ->setContentType('text/html'); 
$this->get('mailer')->send($message); 


} 

私は電子メールテンプレートにも配列を渡しています。電子メール内には、ループを再度使用して電子メールコンテンツを表示しています。私はイメージを表示することができないことを除いて、すべて正常に動作します。

{% for dinner in dinners %} 
<img style="display: block;" src="{{ absolute_url(asset('uploads/' ~ dinner.guest_profile_image)) }}" alt="image1" class="section-img" height="auto" width="128"> 
{% endfor %} 

更新

私はdinner.guest_profile_image可変画像をダンプすると、これは私がhttp://127.0.0.1:8000/uploads/42c5681b253c4dc6a1d145f744e6c3cd.jpegを取得し、私はブラウザ上で直接アクセスする場合、私は、電子メールを表示するために必要な画像を見ることができるものです。 CSSとHTMLの多くがそれでありますので

$messageダンプはので、私はここにイメージタグが

enter image description here

あるダンプのスクリーンショットではかなり巨大である私はここで何をしないのですか?イメージを表示するためには何が必要ですか?

答えて

1

asset_url

<img style="display: block;" src="{{ asset_url('uploads/' ~ dinner.guest_profile_image) }}" alt="..."> 

か、このようbase64であなたのイメージを埋め込む試すことができますしてみてください。

<img style="display: block;" src="data:image/png;base64,iVBORw0KG....(this represent the base64 of your image)..." alt="..."/> 

UPDATE 1


foreach($hostDinners as $key => $hostDinner) { 

    //Encode img in base64 
    foreach ($hostDinner as &$item) { 
     $path = $item['guest_profile_image']; //use symfony2 functions to get the abs upload path 
     $type = pathinfo($path, PATHINFO_EXTENSION); 
     $data = file_get_contents($path); 
     $item["guest_profile_image"] = 'data:image/' . $type . ';base64,' . base64_encode($data); 
    } 
    unset($item); // break the reference with the last element 

    //generate email 
    $message = \Swift_Message::newInstance() 
     ->setSubject("This is a demo subject") 
     ->setFrom($this->container->getParameter('mailer_user')) 
     ->setTo($key) 

     ->setBody(
      $this->renderView(
       'AppBundle:emails:cron-job-request-and-invitations.html.twig', 
       array(
       'dinners' => $hostDinner 
       ) 
      ) 
     ) 
     ->setContentType('text/html'); 
    $this->get('mailer')->send($message); 
} 

とビューで:あなたはimgタグの属性srcに入れるために必要なすべてを持っている$item["guest_profile_image"] = 'data:image/' . $type . ';base64,' . base64_encode($data);このラインで

{% for dinner in dinners %} 
     <img style="display: block;" src="{{ dinner.guest_profile_image }}" alt="image..." class="section-img" height="auto" width="128"/> 
{% endfor %} 

UPDATE 2


(あなたはで見ることができます私は配列src="{{ dinner.guest_profile_image }}"の内容を印刷します。 alt属性はhtmlの優れた実践に使用されていますが、必ずしもalt attributeである必要はありません。 &の

公式PHPマニュアル:直接&$valueに先行するループ内の配列要素を変更できるようにするために。その場合、値は参照によって割り当てられます。

ループ内では、配列の画像パスを各base64のコード化に置き換えています。このようにして、コード内の何かを変更する必要はありません。配列のサイズは以前と同じですが、配列内にイメージパスがありません...配列の位置にbase64でイメージをデコードします。 詳しい情報について&

Oficial php documentationでテイクルック(imgタグのBASE64)[https://en.wikipedia.org/wiki/Data_URI_scheme]

私はこの説明はあなたを助けることができる願っています。

+0

私は 'asset_url'を試してもうまくいきませんが、' base64'についてはわかりません。 – Saadia

+0

@Saadia 'base64'の私の更新をテストしましたか? – iarroyo

+0

こんにちは、私は今これをやっています。以前はbase64のコンセプトで作業したことはありませんでした。私は 'alt'属性または' img'タグの中に何かを置く必要があるかどうかお知らせください。 – Saadia

関連する問題