yii2邮件未发送

时间:2018-07-29 00:03:49

标签: yii2 mailer

绝对有两种相同的发送邮件的方法(注册新用户,发送字母以激活用户)。在控制台中实现了一种方法,该方法有效:

public function actionSendMessage()
    {
        $user = new User();
        $this->readValue($user, 'username');
        $this->readValue($user, 'login');
        $this->readValue($user, 'email');
        $user->hash_password = Yii::$app->getSecurity()->generatePasswordHash($this->prompt('password'));
        $this->readValue($user, 'phone');
        $this->readValue($user, 'address');;
        $user->state = User::STATUS_WAIT;
        $user->generateAuthKey();
        $user->generateEmailConfirmToken();
        if ($user->save()) {
            Yii::$app->mailer->compose('@app/mail/emailConfirm', ['user' => $user])
                ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
                ->setTo($user->email)
                ->setSubject('Email confirmation for ' . Yii::$app->name)
                ->send();
        }
    }

第二个必须在网站上注册时发送一封信,但是可惜,这封信没有来(没有错误):

public function sign()
    {

        $user = new User();
        $user->username = $this->username;
        $user->login = $this->login;
        $user->email = $this->email;
        $user->hash_password = yii::$app->security->generatePasswordHash($this->password);
        $user->phone = $this->phone;
        $user->address = $this->address;
        $user->state = User::STATUS_WAIT;
        $user->generateAuthKey();
        $user->generateEmailConfirmToken();
        if ($user->save()) {
            Yii::$app->mailer->compose('@app/mail/emailConfirm', ['user' => $user])
                ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
                ->setTo($user->email)
                ->setSubject('Email confirmation for ' . Yii::$app->name)
                ->send();
        }

    }
  

有什么错误? $user->save() 对这两项操作都有效

1 个答案:

答案 0 :(得分:0)

有必要在web.php中为

更改服务器设置。
transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => '****',
                'password' => '****',
                'port' => '465',
                'encryption' => 'ssl',
                ],

信件已形成,但没有发送。