Solve Yii2 email sending problem (the result returned successfully but failed to receive the email), yii2 email sent

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.