Yii2:没有发送带有视图的邮件

时间:2017-02-03 06:35:34

标签: php email yii2 swiftmailer

我正在尝试使用Yii2view发送电子邮件,但不会发送。以下是我在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但是当我尝试发送没有视图的电子邮件时,正文中的简单纯文本,它会发送电子邮件。 我究竟做错了什么?有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

我认为如果您更改端口和加密类型,可能适合您。

 '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() ?>
相关问题