Maison  >  Article  >  développement back-end  >  YII2 邮件发不出去

YII2 邮件发不出去

WBOY
WBOYoriginal
2016-06-06 20:36:51813parcourir

<code> main里的代码
   'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.163.com',
            'username' => 'username@163.com',
            'password' => 'myPassword',
            'port' => '994',
            'encryption' => 'ssl',
        ],
    ],

PasswordResetRequestForm里面的代码   
   public function sendEmail()
{
    /* @var $user User */
    $user = User::findOne([
        'status' => User::STATUS_ACTIVE,
        'email' => $this->email,
    ]);

    if ($user) {
        if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
            $user->generatePasswordResetToken();
        }

        if ($user->save()) {
            return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])
                ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
                ->setTo($this->email)
                ->setSubject('Password reset for ' . \Yii::$app->name)
                ->send();
        }
    }

    return false;
}

各位大神有经验吗?
</code>

回复内容:

<code> main里的代码
   'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.163.com',
            'username' => 'username@163.com',
            'password' => 'myPassword',
            'port' => '994',
            'encryption' => 'ssl',
        ],
    ],

PasswordResetRequestForm里面的代码   
   public function sendEmail()
{
    /* @var $user User */
    $user = User::findOne([
        'status' => User::STATUS_ACTIVE,
        'email' => $this->email,
    ]);

    if ($user) {
        if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
            $user->generatePasswordResetToken();
        }

        if ($user->save()) {
            return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])
                ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
                ->setTo($this->email)
                ->setSubject('Password reset for ' . \Yii::$app->name)
                ->send();
        }
    }

    return false;
}

各位大神有经验吗?
</code>

  1. 检查有没有错误日志
  2. 你确定你的邮件服务器的地址配置正确?
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn