Yiimailmessage Mime Multipart/mixed

Hey…

I want to sent inline image in mail. I dont want to use img tag which would point to some public accessible image. I know it cant be done by using multipart/mixed content type instead of using text/html but dont know how to do that in YiiMailMessage.

Can somebody help !!

I can help. The documentation that explains how to do it is here: Embedding Inline Media Files

And now for a simple Yii example…

In your controller:




  public function actionTest()

  {    

    $message = new YiiMailMessage;

    // Assumes 'someimage.jpg' exists in your 'images' directory.

    $image = Swift_Image::fromPath(dirname(Yii::app()->getBasePath()) . '/images/someimage.jpg');

    $cid = $message->embed($image);    

    $message->view = 'test';

    $message->setBody(array('cid' => $cid), 'text/html');

    $message->subject = 'Example of embedded inline image';

    $message->addTo('you@youremailaddress.com');

    $message->from = Yii::app()->params['adminEmail'];    

    Yii::app()->mail->send($message);

  }



In your protected/views/mail/test.php:




  <b>An embedded inline image:</b><br><br>

  <img src="<?php echo $cid; ?>" alt="WTF went wrong?" />



Hope that this helps others who have been unable to find any examples on the Interwebs.

A little ugly but interesting feature is the ability to put data straight into the src attribute of the img tag:




<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." />



If you put the base64 encoded value of the image straight into the <img> tag, some mail clients (like the GMail web client for example) will display the base64 encoded characters instead of an image.

Just tested it and it didn’t displayed it at all. But Thunderbird handled it well. Anyway, it turns out it’s not very useful.