首頁  >  文章  >  php框架  >  利用yii 2框架發送電子郵件

利用yii 2框架發送電子郵件

王林
王林轉載
2021-03-01 10:31:033026瀏覽

利用yii 2框架發送電子郵件

利用yii 2框架傳送電子郵件,具體步驟如下所示:

#1、config/web.php中開啟郵箱設定

'mailer' => [
	'class' => 'yii\swiftmailer\Mailer',
    // 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' => false,//true表示只生成文件不发
    'transport' => [
    	'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.qq.com',  //每种邮箱的host配置不一样
        'username' => 'xxxxx@qq.com',//改成自己的邮箱
        'password' => 'xxxxxxxx',//改成自己的邮箱token
        'port' => '465',
        'encryption' => 'ssl',
	],
    'messageConfig'=>[
    	'charset'=>'UTF-8',
        'from'=>['xxxxx@qq.com'=>'YiiAdmin']//邮件显示名称
	],
],

2、SiteController.php控制器檔案新增

public function actionSendMail(){
	$mail= Yii::$app->mailer->compose('reset-password',['token'=>'xxxxxx']);
	// 渲染一个视图作为邮件模板 文件路径mail/reset-password.php,注意,不在view中
	$mail->setTo('xxxxx@hotmail.com');//要发送到的邮箱地址
	$mail->setSubject("邮件测试【重置密码】");//邮件标题
	if($mail->send())
		echo "success";
	else
		echo "failse";
	die();
}

3、檢視檔案

#檢視檔案的輸出就是郵件的內容

urlManager->createAbsoluteUrl(['site/reset-password', 'token' => $token]);
?>
密码重置服务
点击重置密码

4、存取http://127.0. 0.1/base/web/index.php?r=site/send-mail

出現success則傳送成功,若未收到確認信箱已開啟pop3服務

利用yii 2框架發送電子郵件

推薦教學:yii框架

以上是利用yii 2框架發送電子郵件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除