Yii2 emails are sent to yii2 if the returned result is successful but the email cannot be received.

Source: Internet
Author: User

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' => trueChange '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.

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.