首页 > PHP教程 > php开发知识文章

php -- Yii2框架发送邮件的方法

本文主要介绍了php -- Yii2框架发送邮件的方法('useFileTransport' => true 改成 'useFileTransport' => false,),欢迎大家的学习,需要的朋友参考下。

配置文件代码如下:

// application components
'components' => [
'db' => [
'class' => 'yii/db/Connection',
'dsn' => 'mysql:host=localhost;dbname=user',
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii/swiftmailer/Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.qq.com',
'username' => '********@qq.com',
'password' => '********',
'port' => '465',
'encryption' => 'ssl',
],
],
],

控制器代码:

public function actionCreate()
{
$request = Yii::$app->request;
$params = $request->post();
if ($params) {
$fromName = $request->getBodyParam('fromName');
$fromBady = $request->getBodyParam('fromBady');
$toName = $request->getBodyParam('toName');
$body = $request->getBodyParam('body');
return $this->send($fromName, $fromBady, $toName, $body);
}
return false;
}

/** Email Send function
* @param1 $fromName
* @param1 $toName
* @param1 $body
* $return boo1ean
*/
public function send($fromName, $fromBady, $toName, $body = '')
{
$mail = Yii::$app->mailer->compose()
->setFrom([$fromName => $fromBady])
->setTo($toName)
->setSubject('邮件发送配置')
->setTextBody($body)//发布纯文字文本
->send();
if ($mail) {
return [
'name' => [
'fromName' => $fromName,
'fromBady' => $fromBady,
'toName' => $toName,
'body' => $body,
],
'message' => '发生到[' . $toName . ']的邮件成功!',
'code' => 0,
'status' => 200,
];
} else {
return [
'name' => 'Error',
'message' => '发生到' . $toName . '的邮件失败!',
'code' => 0,
'status' => 402,
];
}
}

关键的地方:

'useFileTransport' => true 改成 'useFileTransport' => false,

邮箱密码是qq授权码(到你邮箱中的设置->账户 查看)

以上就是本文php -- Yii2框架发送邮件的方法的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,谢谢大家对本站的支持。

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。