Use the yii 2 framework to send emails. The specific steps are as follows:
1. Open the email configuration in 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. Add the SiteController.php controller file
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. View file
The output of the view file is the content of the email
<?php $resetLink = Yii::$app->urlManager->createAbsoluteUrl(['site/reset-password', 'token' => $token]); ?> <div> <h5>密码重置服务</h5> <a href="<?=$resetLink?>">点击重置密码</a> </div>
4. Visit http://127.0. 0.1/base/web/index.php?r=site/send-mail
If success appears, the sending is successful. If the confirmation email is not received, the pop3 service has been enabled
Recommended tutorial: yii framework
The above is the detailed content of Send email using yii 2 framework. For more information, please follow other related articles on the PHP Chinese website!