swiftmailer 组件的配置-谁能详解下? [ 2.0 版本 ]
邮件类 在配置文件设置过了,
控制器中的代码是
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)好多配置
sinobill 补充于 2018-04-12 14:09
我自己找到问题错误了,两个问号是php7才出的新特性,我的本地php版本才5.4,升级下PHP版本试试看
其实两个问题??是php7新推出的表达式,
c = a ?? b;
表示如果a非空,则c = a,
如果a为空,则 c = b;
sinobill 补充于 2018-04-12 17:23
经过验证,就是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;
sinobill 补充于 2018-04-12 21:08
并且发送邮件的方法可以参考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']
]
],
共 2 个回答
-
/** * 发送邮件 * * @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 ()); } }
-
'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' => [ '@' => '发送人名称' ] ], ],
共 1 条回复
sinobill
注册时间:2017-10-26
最后登录:2020-04-03
在线时长:15小时35分
最后登录:2020-04-03
在线时长:15小时35分
- 粉丝2
- 金钱115
- 威望0
- 积分265