【问题标题】:Yii2: Mail with view is not being sentYii2:未发送有视图的邮件
【发布时间】:2017-02-03 06:35:34
【问题描述】:

我正在尝试使用viewYii2 中发送电子邮件,但未发送。这是我在controller 做的事情:

$message = Yii::$app->mailer->compose('reportview-html', [
                            'positiveReviews' => $positiveReviews,
                            'negativeReviews' => $negativeReviews,
                            'company_name' => $userSurveyConfig->survey_email_from
                        ])->setFrom([\Yii::$app->params['supportEmail'] => "Review Fox"])
                        ->setTo($userSurveyConfig->reports_email_address)
                        ->setSubject("Review Fox Weekly/Monthly Report")
                        ->send();

这是我的config 邮件设置:

'mail' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@frontend/mail',
            'useFileTransport' => false, //set this property to false to send mails to real email addresses
//comment the following array to send mail using php's mail function
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => '**********************',
                'password' => '********',
                'port' => '465',
                'encryption' => 'ssl',
            ],
        ],

当我运行脚本时,它不只是发送email,而是当我尝试发送没有视图的电子邮件时,正文中的简单纯文本,它确实发送了电子邮件。 我究竟做错了什么?有什么帮助吗?

【问题讨论】:

  • 我知道你在配置本身中设置了路径,你可以尝试注释掉并直接尝试这个 ** Yii::$app->mailer->compose('@frontend/mail /layouts/reportview-html',['model' => $model, 'path' => $path,'password' => $password])**
  • @MohanPrasad 试过了,结果一样。

标签: php email yii2 swiftmailer


【解决方案1】:

我认为如果您更改端口和加密类型,可能对您有用。

 'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
                'transport' => [
                    'class' => 'Swift_SmtpTransport',
                    'host' => 'smtp.gmail.com',
                    'username' => '*******',
                    'password' => '*****',
                    'port' => '587',
                    'encryption' => 'tls',
                ]

        ],

它对我来说很好,有不同的邮件布局设置,一切都很好。

希望本教程对您有所帮助。click here

像下面的代码一样设置你的邮件/布局/html文件:

    <?php
use yii\helpers\Html;

/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
    <?php $this->beginBody() ?>
    <?= $content ?>
    <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

现在您可以继续创建其他布局,例如如下所示。

   <?php
use yii\helpers\Html;

/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style>
        .mail-wrapper{ border: 1px solid #ddd2d2; box-shadow: 1px 1px 2px #888888;}
        .mail-font{ font-size: 18px; padding:15px 25px; margin-left:10px !important; margin-top: 10px !important;}
        .mail-button{ text-decoration: none; border: 1px solid #2A3F54; background-color: #2A3F54; color: white; box-shadow: 0 0 5px #FCB3BC; }
        .mail-button:hover {background-color: #286090 !important;}
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
    <?php $this->beginBody() ?>

    <div class="mail-wrapper">

        <div class="mail-font fa fa-paw">
            <img src ="<?= $message->embed($path); ?>" />
        </div>
        <br></br>


        <div class="mail-font">
            <?= "This e-mail and any attachments may contain confidential material for the sole
                use of the intended recipient(s). Any review or distribution by others is
                strictly prohibited. If you are not the intended recipient, please contact the
                sender and delete all copies..  "; ?>
        </div>
                <br></br>

    </div>


    <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

【讨论】:

  • 这个我试过了,没用。结果还是一样。
  • 你改变了端口吗?
  • 据我所知,您没有在 html 文件中输出内容..您这样做应该可以正常工作..测试了上面的代码..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
  • 2020-08-23
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多