简体   繁体   中英

SwiftMailer attach image in email body

Basically I use Yii2 mailer, But in principe, it use Swifmailer. So, I want to display an image in body email.

Our user that use Mozilla thunderbird is no problem. But , in my user which is use email client based Microsoft such as OE, Ms. OE 2007, 2010, 2013 and WIndows live mail, the image displayed as attached file. In body email just displayed Big cross icon. This is my code :

 $mail = Yii::$app->mailer->compose();
        $mail->embed($img,[
            'fileName' => 'Request.png',
            'contentType' => 'image/png'
        ]);
        $arrayTo = array("dzil@tresnamuda.co.id");
        $mail->setFrom(array("itjkt@tresnamuda.co.id" => "TMS IT-JKT SYSTEM"))
                ->setTo($arrayTo)
                ->setCc(array('dziljalal1@gmail.com'/* , \Yii::$app->user->identity->email */))
                ->setSubject("David - $status Request Sudah terkonfirmasi ke Dept. IT dari User : " . Yii::$app->user->identity->username)
                ->setHtmlBody("Kepada Yth. Bapak David V Lengkong, <br>Dengan Hormat, <br> 
                               Dengan ini kami lampirkan data Request Permintaan dan Perbakan IT sebagai berikut. <br> 

                               <img src='$img' alt='Image'>");
        $mail->send();

电子邮件客户端

UPDATED The image has formatted by data:image/png;base64,

Please advise.

Try this:

$mail = Yii::$app->mailer->compose('embed-email', ['imageFileName' => 'Request.png']);

And in your view:

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

Source .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM