在Yii2中使用Swift邮件发送电子邮件发送了错误的电子邮件内容

时间:2015-04-28 11:46:10

标签: php yii2 swiftmailer outlook-2010 yii2-advanced-app

我正在使用Swift邮件程序从我基于Yii2的Web应用程序发送电子邮件。但它无法在MS Outlook中正确读取/接收。

请参阅我使用MS Outlook 2010收到的电子邮件正文 -

--_=_swift_v4_1430198154_31f7ff2886dc0fb65796db153d9434096acbae54_=_
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Dear Admin,=20
User John has been si= gned up
successfully.

Thanks,
The App Tea=
m

This message was sent to john@example.com. If y= ou don't want
to receive future emails from ABC, please unsubs= cribe.


--_=_swift_v4_1430198154_31f7ff2886dc0fb65796db153d9434096acbae54_=_
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.=
org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns=3D"http://www.w3.org=
/1999/xhtml">
<head>
<meta http-equiv=3D"Content-Type" con= tent=3D"text/html;
charset=3DUTF-8" />
<title></title>
<=
/head>
<body>
    Dear Admin, <br>
<p>User John has been signed up successfully.</p>

<=
p>Thanks,</p>
<p>The ABC Team</p>

<br><br><br> =

=09
<p style=3D'color: #B6B6B6'>
    This message was sent to =
john@example.com. If you don't want to receive future emai= ls
from ABC, please <a href=3D'#'>unsubscribe</a>.
</p> =



</body>
</html>


--_=_swift_v4_1430198154_31f7ff2886dc0fb65796db153d9434096acbae54_=_--

我正在使用以下配置进行编码 -

 'messageConfig' => [
    'charset' => 'UTF-8',
],

使用 -

发送
Yii::$app->mailer->compose('mailview', ['name' => $name]) 
            ->setFrom([\Yii::$app->params['adminEmail'] => \Yii::$app->name . ' App'])
            ->setTo($to)
            ->setSubject($subject)
            ->send();

我错过了什么?

1 个答案:

答案 0 :(得分:0)

对我来说只有setHtmlBoby()方法, 因为来自文档的示例发送了带有重复内容的3个标题:

  1. 内容类型:multipart / alternative;
  2. Content-Type:text / plain;字符集= UTF-8
  3. Content-Type:text / html;字符集= UTF-8
  4. 下面是代码示例:

    //render view content
    $htmlMailContent = $this->renderPartial('@common/mail/passwordCreateToken-html', ['user' => $model]);
    //render layout
    $htmlMail = $this->renderPartial('@common/mail/layouts/html', ['content' => $htmlMailContent]);
    
    $mailResult = Yii::$app->mailer->compose()
        ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
        ->setTo($model->email)
        ->setSubject('Password reset for ' . Yii::$app->name)
        ->setHtmlBody($htmlMail) //set here mail with layout
        ->send();