Yii2 emails are sent to yii2 if the returned result is successful but the email cannot be received.
Preface
This article describes how to solve Yii2's problem of sending a successful email but failing to receive the email. For your reference, let's take a look at the details below:
I used the yii mail sending function just now. Although the result is returned successfully, I cannot receive the email.
The configuration file code is as follows:
'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', ], ],],
Controller code:
Public $ modelClass = 'common \ models \ user'; public function actions () {$ actions = parent: actions (); // disable the "create" operation and use the unset method written in the Controller ($ 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) {$ fromNam E = $ 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 ('email sending configuration')-> setTextBody ($ body) // publish plain text-> send (); if ($ mail) {return ['name' => ['fromname' => $ fromName, 'frombady' => $ fromBady, 'toname' => $ toName, 'body' => $ body,], 'message' => 'occurs ['. $ toName. '] the email is successful! ', 'Code' => 0, 'status' => 200,];} else {return ['name' => 'error ', 'message' => 'occurs '. $ toName. 'The email failed! ', 'Code' => 0, 'status' => 402,] ;}}
However, you will find that the data is returned successfully, but you have not received the email.
Solution:
This is what you should
'useFileTransport' => true
Change 'useFileTransport' => false
,
And your email password is a qq authorization code (go to settings in your email address> View account)
Through the above efforts, you will successfully receive emails!
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.