您好,登錄后才能下訂單哦!
小編給大家分享一下Yii2郵件發送結果返回成功但接收不到郵件怎么辦,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
配置文件代碼如下:
'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,$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授權碼(到你郵箱中的設置->賬戶 查看)
通過以上努力你會成功接收郵件!
看完了這篇文章,相信你對“Yii2郵件發送結果返回成功但接收不到郵件怎么辦”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。