Solve Yii2 email sending problem (the result returned successfully but failed to receive the email), yii2 email sent
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) {$ 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 ('mail 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.
This is what you should
'Usefiletransport '=> true to '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!