How to add image in mail in yii

In Yii, you can add an image to an email by using the CHtml::image method to generate the HTML for the image, and then embedding it in the email body using HTML.

Here's an example of how you can do this:

use yii\helpers\Html;

// assume $imagePath is the path to the image file
$imagePath = '/path/to/image.jpg';

// generate the HTML for the image
$imageHtml = Html::img($imagePath, ['alt' => 'Image description']);

// create the email body
$emailBody = '<p>This is an example email with an image:</p>'. $imageHtml;

// send the email using your preferred email library (e.g. SwiftMailer, PHPMailer, etc.)

In this example, the CHtml::image method is used to generate the HTML for the image, which includes the src attribute set to the path of the image file. The alt attribute is also set to provide a text description of the image for accessibility purposes.

You can then embed the image HTML in the email body using plain HTML.

Alternatively, you can use the Yii::app()->assetManager->publish method to publish the image file to a public directory, and then use the published URL in the email body:

$imagePath = Yii::app()->assetManager->publish(Yii::app()->basePath. '/images/image.jpg');
$imageHtml = Html::img($imagePath, ['alt' => 'Image description']);

This way, you don't need to worry about the image file being accessible from the outside world, as it will be served by the Yii application.

Note that you should adjust the image path and URL according to your specific application configuration and requirements.