How to solve problems when sending emails in Yii2

PHP中文网
Release: 2023-03-10 21:54:02
Original
1816 people have browsed it

I just used the yii email sending function. Although the result was returned successfully, I could not 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', 
        ], 
    ],
],
Copy after login

Controller code:

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,
    	];
    }
}
Copy after login

But you will find that the data return is successful but you do not receive the email

You should change
'useFileTransport' => true to 'useFileTransport' => false,
And your email password is the qq authorization code ( Go to Settings->Account View in your mailbox)

Through the above efforts, you will successfully receive the email!

The above is the detailed content of How to solve problems when sending emails in Yii2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!