Home > PHP Framework > YII > Send email using yii 2 framework

Send email using yii 2 framework

王林
Release: 2021-03-01 10:31:03
forward
3264 people have browsed it

Send email using yii 2 framework

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']//邮件显示名称
	],
],
Copy after login

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();
}
Copy after login

3. View file

The output of the view file is the content of the email

<?php
$resetLink = Yii::$app->urlManager->createAbsoluteUrl([&#39;site/reset-password&#39;, &#39;token&#39; => $token]);
?>
<div>
	<h5>密码重置服务</h5>
	<a href="<?=$resetLink?>">点击重置密码</a>
</div>
Copy after login

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

Send email using yii 2 framework

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template