Yii2郵件發送出現問題怎麼解決

PHP中文网
發布: 2023-03-10 21:54:02
原創
1825 人瀏覽過

剛剛用了一下yii郵件發送功能,雖然結果回傳成功,但接收不到郵件。設定檔代碼如下:

'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', 
        ], 
    ],
],
登入後複製

控制器程式碼:

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,
    	];
    }
}
登入後複製

但你會發現資料回傳是成功的但你卻沒收到郵件

這是你應該把
'useFileTransport' => true 改成'useFileTransport' => false,
並且你的郵件密碼是qq授權碼(到你郵箱中的設定->帳號查看)

透過以上努力你會成功接收郵件!

以上是Yii2郵件發送出現問題怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板