利用yii 2框架發(fā)送電子郵件,具體步驟如下所示:
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表示只生成文件不發(fā) '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');//要發(fā)送到的郵箱地址 $mail->setSubject("郵件測試【重置密碼】");//郵件標(biāo)題 if($mail->send()) echo "success"; else echo "failse"; die(); }
3、視圖文件
視圖文件的輸出就是郵件的內(nèi)容
<?php $resetLink = Yii::$app->urlManager->createAbsoluteUrl(['site/reset-password', 'token' => $token]); ?> <div> <h5>密碼重置服務(wù)</h5> <a href="<?=$resetLink?>">點擊重置密碼</a> </div>
4、訪問 http://127.0.0.1/base/web/index.php?r=site/send-mail
出現(xiàn) success則發(fā)送成功,若未收到確認(rèn)郵箱已開啟pop3服務(wù)
推薦教程:yii框架
以上就是利用yii 2框架發(fā)送電子郵件的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進(jìn)程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號