如果你使用的是邮件视图,你是否必须在Yii 2邮件中设置HtmlBody / TextBody


Do you have to set HtmlBody / TextBody in Yii 2 mailer if you are using a mail view?

一直在阅读有关在Yii 2中发送邮件的信息,并注意到您可以传入邮件视图。

例如:

Yii::$app->mailer->compose([
    'html' => 'contact-html',
    'text' => 'contact-text',
]);

。以及使用邮件布局。

但是,我的问题是,如果您的所有内容都将包含在视图中,那么以下内容的目的是什么:

// Set body
$mail->setHtmlBody($email_data['html_msg']);
$mail->setTextBody($email_data['message']);

您仍然需要设置它们 - 这是如何工作的?

我不

明白你的问题,我不得不读一遍。

如果要使用 HTML 模板呈现和另一个文本渲染,请执行此操作:

Yii::$app->mail->compose(['html' => '@app/mail-templates/html-email-01', 'text' => '@app/mail-templates/mail'], [/*Some params for the view */])
     ->setFrom('from@me.com')
     ->setTo('someone@domain.com')
     ->setSubject('An email')
     ->send();

视图的路径也可以是相对的,就像在控制器内部一样。(例如,['html' => 'html-email-01'])。

在你的帖子中你提到

Yii::$app->mailer->compose()
    ->setFrom('from@domain.com')
    ->setTo('to@domain.com')
    ->setSubject('Message subject')
    ->setTextBody('Plain text content')
    ->setHtmlBody('<b>HTML content</b>')
    ->send();

您不必设置

$mail->setHtmlBody($email_data['html_msg']);
$mail->setTextBody($email_data['message']);

如果要使用邮件视图