《PHP实战:解决Yii2邮件发送结果返回成功,但接收不到邮件的问题》要点:
本文介绍了PHP实战:解决Yii2邮件发送结果返回成功,但接收不到邮件的问题,希望对您有用。如果有疑问,可以联系我们。
前言PHP教程
本文主要介绍的是关于解决Yii2邮件发送返回成功但接收不到邮件问题的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:PHP教程
刚刚用了一下yii邮件发送功能,虽然结果返回成功,但接收不到邮件.PHP教程
'components' => [ 'db' => [ 'class' => 'yii\db\Connection','dsn' => 'MysqL:host=localhost;dbname=yiidemo','username' => 'root','password' => 'root','charset' => 'utf8',],'mailer' => [ 'class' => 'yii\swiftmailer\Mailer','viewPath' => '@common/mail',// send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true,'transport' => [ 'class' => 'Swift_SmtpTransport','host' => 'smtp.qq.com','username' => '********@qq.com','password' => '********','port' => '465','encryption' => 'ssl',
public $modelClass = 'common\models\User'; public function actions() { $actions = parent::actions(); // 禁用"create" 操作后可用自己在控制器中写的方法 unset($actions['update'],$actions['create'],$actions['delete']); //$actions['index']['prepareDataProvider'] = [$this,'prepareDataProvider']; return $actions; } 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,$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.'的邮件失败!','status' => 402,]; } }
这是你应该把'useFileTransport' => true
改成 'useFileTransport' => false
,
PHP教程
并且你的邮箱密码是qq授权码(到你邮箱中的设置->账户 查看)PHP教程
总结PHP教程
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程之家的支持.
PHP教程