无法使用具有Yii2的Swiftmailer嵌入图像

无法使用具有Yii2的Swiftmailer嵌入图像,yii2,swiftmailer,Yii2,Swiftmailer,我试图在我的电子邮件中嵌入一个图像使用swiftmailer,但它没有得到嵌入。我已经尝试了论坛上提供的每一个解决方案,但仍然无法使其发挥作用。下面是我的代码。请注意,我可以在我的模板HTML内容,但只是图像并没有得到嵌入它 $message = Yii::$app->mailer->compose('registration', ['imgN'=>'\Yii::getAlias('@webroot/assets/image.png')]); return $message-

我试图在我的电子邮件中嵌入一个图像使用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"/>
embed($imgN);?>“alt=”无图像“/>
请指出我可能犯的任何错误? 这将是一个很大的帮助。 谢谢!

试试看

$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\u 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.'"/>';
然后在你的视野中,呼出图像

  foreach ( $images as $image )
      echo $image;
然后发送电子邮件

   $result = $email->send()

你把$imgN?它的资产文件夹存储在哪里了?路径上显示。你是用gmail id还是其他id发送电子邮件?我也试过用gmail和其他id发送电子邮件。结果是一样的。我得到了以下错误:获取未知属性:yii\swiftmailer\Message::htmllayout现在给出此错误:未知方法–yii\base\UnknownMethodException调用未知方法:yii\swiftmailer\Message::render()非常感谢@Bizley。
   $result = $email->send()