邮件类 在配置文件设置过了,
控制器中的代码是
public function actionRequestPasswordReset()
{
$model = new PasswordResetRequestForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail()) {
Yii::$app->session->setFlash('success', '请检查您的邮箱');
return $this->goHome();
} else {
Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
}
}
return $this->render('requestPasswordResetToken', [
'model' => $model,
]);
}
运行报错
in D:\newWWW\sinocd.cn\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mime\SimpleMessage.php at line 496
这个文件是原版文件,没有更改过,请问 哪里出问题呢 是不是配置文件写的不对?该怎么写?
看看下面这个配置文件
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' =>false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.163.com',
'username' => '888888@163.com',
'password' => '888888',
'port' => '465',
'encryption' => 'ssl'
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['88888888@163.com'=>'8888']
]
]
敏感信息用8代替了。另外谁能详解下这个组件(swiftmailer\swiftmailer)的配置,这个框架(YII)好多配置
我自己找到问题错误了,两个问号是php7才出的新特性,我的本地php版本才5.4,升级下PHP版本试试看
其实两个问题??是php7新推出的表达式,
c = a ?? b;
表示如果a非空,则c = a,
如果a为空,则 c = b;
经过验证,就是php版本的问题,升级到PHP7.0.以后版本,发送邮件没有任何问题。
D:\newWWW\sinocd.cn\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mime\SimpleMessage.php at line 496
其实两个问题??是php7新推出的表达式,
c = a ?? b;
表示如果a非空,则c = a,
如果a为空,则 c = b;
并且发送邮件的方法可以参考frontend\models\PasswordResetRequestForm模型中的sendEmail方法,
定义viewpath可以容易的给邮件定义模板,容易群发和按需要发送
邮件正确的配置是这样的:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' =>false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.163.com',
'username' => '888888@163.com',
'password' => '88888',
'port' => '465',
'encryption' => 'ssl'
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['88888@163.com'=>'8888']
]
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.exmail.qq.com',
'username' => '@',
'password' => '',
'port' => '465',
'encryption' => 'SSL',
],
'messageConfig' => [
'charset' => 'UTF-8',
'from' => [ '@' => '发送人名称' ]
],
],
/**
* 发送邮件
*
* @param $email 邮箱地址
* @param $subject 邮件主题
* @param $content 邮件内容
* @param int $isHtml 邮件内容是否是html, 1-html, 0-text
*
* @return bool
* @throws \Exception
*/
public static function sendMail ( $email, $subject, $content, $isHtml = 1 ) {
$mail = Yii::$app->mailer->compose ();
$mail->setTo ( $email );
$mail->setSubject ( $subject );
if ( $isHtml == 1 ) {
$mail->setHtmlBody ( $content );
} else {
$mail->setTextBody ( $content );
}
try {
if ( $mail->send () ) {
return true;
} else {
return false;
}
}catch (\Exception $e) {
throw new \Exception($e->getMessage ());
}
}