How to send image in email in YII?

Here is the mail sending function code. I want to send the image to the set body.

public function SendMail($id) {
    $message = new YiiMailer('', array());
    $message->setFrom('abc@gmail.com', 'test');
    $message->setTo('pqr@gmail.com');
    $message->setSubject('Test');
    $message->IsHTML(true);

    $message->setBody(??????);

    if ($message->send()) {

  }
 }

Give me the code to send the image in the body of the body in yii.

I do not know how to send an image to the set body?

+4
source share
2 answers

How about these

$message[] = Yii::$app->mailer->compose('downNotify', [
    'image' => Url::to('@app/web/mail/images/logo.png')
])

$message->setBody($message);

Or see here

Yii::$app->mailer->compose('embed-email', ['imageFileName' => '/path/to/image.jpg'])
// ...
->send();

<img src="<?= $message->embed($imageFileName); ?>">

Or see here

$uploadedFile = CUploadedFile::getInstance($model,'anexo'); $msg->attach($uploadedFile);

Or this too

$image = Swift_Image::fromPath(dirname(Yii::app()->getBasePath()) . '/images/someimage.jpg');
$cid = $message->embed($image);    
$message->setBody(array('cid' => $cid), 'text/html');

So in protected /views/mail/test.php:

<b>An embedded inline image:</b><br><br>
<img src="<?php echo $cid; ?>" alt="WTF went wrong?" />

Or is it toooo ( Adding attachments )

Yii::setPathOfAlias('webroot.images.mail', '/path/to/your/images/mail/dir');
+3
source

. phpMailer 5.2.x, set_magic_quotes_runtime(), PHP 5.4 . 5.3, phpMailer.

phpMailer , , .

( html) :

<img src="cid:header"/>

, , :

Yii::app()->mailer->IsHTML(true);
$path = Yii::getPathOfAlias('application.views.email');
Yii::app()->mailer->AddEmbeddedImage($path . DIRECTORY_SEPARATOR. "emailHeader.png", "header", "alertHeader.png");

, . AddEmbeddedImage 3 :

  • cid
  • ,
0

Source: https://habr.com/ru/post/1613147/


All Articles