无法使用带有 Yii2 的 Swiftmailer 嵌入图像
Unable to embed image with Swiftmailer with Yii2
我正在尝试使用 swiftmailer 在我的电子邮件中嵌入图像,但它没有嵌入其中。我已经尝试了论坛中提供的所有解决方案,但仍然无法正常工作。下面是我的代码。请注意,我可以 HTML 我的模板中的内容,但只有图像没有嵌入其中。
$message = Yii::$app->mailer->compose('registration', ['imgN'=>'\Yii::getAlias('@webroot/assets/image.png')]);
return $message->setFrom($from)->setTo($to)->setSubject(self::$subject)->send();
我也试过使用:
i) 'imgN'=> Yii::getAlias('@web/assets/image.png'),
ii) 'imgN'=>Yii::getAlias('@app/assets/image.png'),
iii)'imgN'=> '@app/web/assets/image.png',
视图中的代码:
<?php
use yii\swiftmailer\Message;
$message = new Message;
?>
并在 HTML 正文中:
<img src="<?= $message->embed($imgN); ?>" alt="No Image"/>
请指出我可能做错了什么?
这将是一个很大的帮助。
谢谢!
尝试
$message = Yii::$app->mailer->compose();
$message->setFrom($from);
$message->setTo($to);
$message->setSubject(self::$subject);
$message->setHtmlBody(Yii::$app->mailer->render('registration', [
'img' => $message->embed(\Yii::getAlias('@app/assets/image.png')),
], Yii::$app->mailer->htmlLayout));
return $message->send();
并且在视图中:
<img src="<?= $img ?>">
我想在电子邮件的 html 中显示嵌入的图像。我发现你不能做正常的
Yii::$app->mailer->compose( 'view_file' )
因为您需要在 html.
中引用嵌入图像的 "cid"
因此您需要先创建电子邮件对象
$email = Yii::$app->mailer->compose($view, ['model' => $model ])
->setFrom($from)
->setTo($emailTo)
->setSubject($subject);
然后创建每个嵌入对象
$cid = $email->embed($model->filename,[
'fileName' => $model->id,".jpg",
'contentType' => 'image/jpeg'
]);
// save the returned cid
$images[$item->id] = '<img src="'.$cid.'"/>';
然后通过渲染您的视图(或其他方式)创建 html
$html = Yii::$app->mailer->render('@common/mail/receipt', ['model' => $this, 'images' => $images ]);
$email->setHtmlBody($html);
然后在您的视图中回显图像
foreach ( $images as $image )
echo $image;
然后发送邮件
$result = $email->send()
我正在尝试使用 swiftmailer 在我的电子邮件中嵌入图像,但它没有嵌入其中。我已经尝试了论坛中提供的所有解决方案,但仍然无法正常工作。下面是我的代码。请注意,我可以 HTML 我的模板中的内容,但只有图像没有嵌入其中。
$message = Yii::$app->mailer->compose('registration', ['imgN'=>'\Yii::getAlias('@webroot/assets/image.png')]);
return $message->setFrom($from)->setTo($to)->setSubject(self::$subject)->send();
我也试过使用:
i) 'imgN'=> Yii::getAlias('@web/assets/image.png'),
ii) 'imgN'=>Yii::getAlias('@app/assets/image.png'),
iii)'imgN'=> '@app/web/assets/image.png',
视图中的代码:
<?php
use yii\swiftmailer\Message;
$message = new Message;
?>
并在 HTML 正文中:
<img src="<?= $message->embed($imgN); ?>" alt="No Image"/>
请指出我可能做错了什么? 这将是一个很大的帮助。 谢谢!
尝试
$message = Yii::$app->mailer->compose();
$message->setFrom($from);
$message->setTo($to);
$message->setSubject(self::$subject);
$message->setHtmlBody(Yii::$app->mailer->render('registration', [
'img' => $message->embed(\Yii::getAlias('@app/assets/image.png')),
], Yii::$app->mailer->htmlLayout));
return $message->send();
并且在视图中:
<img src="<?= $img ?>">
我想在电子邮件的 html 中显示嵌入的图像。我发现你不能做正常的 Yii::$app->mailer->compose( 'view_file' ) 因为您需要在 html.
中引用嵌入图像的 "cid"因此您需要先创建电子邮件对象
$email = Yii::$app->mailer->compose($view, ['model' => $model ])
->setFrom($from)
->setTo($emailTo)
->setSubject($subject);
然后创建每个嵌入对象
$cid = $email->embed($model->filename,[
'fileName' => $model->id,".jpg",
'contentType' => 'image/jpeg'
]);
// save the returned cid
$images[$item->id] = '<img src="'.$cid.'"/>';
然后通过渲染您的视图(或其他方式)创建 html
$html = Yii::$app->mailer->render('@common/mail/receipt', ['model' => $this, 'images' => $images ]);
$email->setHtmlBody($html);
然后在您的视图中回显图像
foreach ( $images as $image )
echo $image;
然后发送邮件
$result = $email->send()