How to display image in yii mail

In Yii, you can display an image in a mail message using the CHtml::image method. Here's an example:

$message = new \yii\mail\MessageInterface;
$message->setFrom('[email protected]');
$message->setTo('[email protected]');
$message->setSubject('Test Email');

$imagePath = Yii::getAlias('@webroot'). '/images/image.jpg'; // path to your image
$image = CHtml::image($imagePath, 'Image', array('alt' => 'Image'));

$message->setBody($image, 'text/html');

In this example, CHtml::image is used to generate an HTML image tag that points to the image file. The first argument is the path to the image file, the second argument is the alt text for the image, and the third argument is an array of HTML attributes for the image tag.

You can also use CHtml::link method to create a link to the image:

$image = CHtml::link(CHtml::image($imagePath, 'Image', array('alt' => 'Image')), $imagePath);

In this case, the link will point to the image file, and when clicked, it will download the image.

Make sure to set the Content-Type of the mail message to text/html so that the image is displayed correctly:

$message->setContentType(\yii\mail\MessageInterface::TEXT_HTML);

Finally, you can send the mail message using the MailComponent:

$mail = Yii::$app->mail;
$mail->compose()
    ->setFrom('[email protected]')
    ->setTo('[email protected]')
    ->setSubject('Test Email')
    ->setBody($message->getBody(), 'text/html')
    ->send();

This will send the mail message with the image attached.