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

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

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

1、config/web.php中開啟郵箱配置

'mailer'?=>?[ 	'class'?=>?'yiiswiftmailerMailer', ????//?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、視圖文件

視圖文件的輸出就是郵件的內容

<?php $resetLink = Yii::$app->urlManager-&gt;createAbsoluteUrl(['site/reset-password',?'token'?=&gt;?$token]); ?&gt; <div> 	<h5>密碼重置服務</h5> 	<a>"&gt;點擊重置密碼</a> </div>

4、訪問 http://127.0.0.1/base/web/index.php?r=site/send-mail

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

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

推薦教程:yii框架

? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享