The content of this article is about the email sending function of yii2, which has certain reference value. Now I share it with you. Friends in need can refer to it.
First of all, to implement the email function, we need to understand yii2 There is the concept of email class
yii2 In order to solve our email sending problem, we provide us with the swiftMailer extension.
So what is swiftMailer?
Swift is a PHP function library that uses entirely object-oriented coding for sending e-mails. Swift does not rely on PHP's mail() function, because using it to send multiple emails will occupy higher server resources. Swift sends email faster and more efficiently by connecting directly to an SMTP server or MTA.
First configure our mailer
component
//主要是对组件中各种必要的发送邮箱的属性进行配置'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', 'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.qq.com', //每种邮箱的host配置不一样 'username' => '11150****1@qq.com', 'password' => '*****',//密码不是指的登录密码 'port' => '465', 'encryption' => 'ssl', ], ],
$mailer = \Yii::$app->mailer->compose('seekpass',['html'=>'html','adminuser'=>$post['Admin']['adminuser'],'token'=>$_SERVER['HTTP_HOST'].Url::toRoute(['manage/emailchangepass'])."×tamp=".$time."&token=".$token."&adminuser=".$adminuser]); $mailer ->setFrom("1115007981@qq.com")//设置发件人,虽然写了一遍但还是要写 ->setTo("1115007981@qq.com")//设置收件人 ->setSubject("黑势力科技")//摘要 ->send(); //如发送成功,则返回一个bool类型的值
1. The compose() method in the mailer component
compose($view = null, array $params = [])
$view value is the email that needs to be loaded Template, by default in common/Mailer (determined by the viewPath attribute in the mailer configuration)
It is worth mentioning that When the keys of the value are html and
text, it means loading our html block template and text block template respectively.
//例如:<P>尊敬的管理员<?=$adminuser;?></P><p> 你好</p><p>你的重置密码连接为:</p><a href="http://<?=$token?>">http://<?=$token?></a><p>请在5分钟之内重置密码,否则密码想会失效</p> //token 为我们在compose中传入的值
//主要是对组件中各种必要的发送邮箱的属性进行配置'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', 'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.qq.com', //每种邮箱的host配置不一样 'username' => '11150****1@qq.com', 'password' => '*****',//密码不是指的登录密码 'port' => '465', 'encryption' => 'ssl', ], ],
$mailer = \Yii::$app->mailer->compose('seekpass',['html'=>'html','adminuser'=>$post['Admin']['adminuser'],'token'=>$_SERVER['HTTP_HOST'].Url::toRoute(['manage/emailchangepass'])."×tamp=".$time."&token=".$token."&adminuser=".$adminuser]); $mailer ->setFrom("1115007981@qq.com")//设置发件人,虽然写了一遍但还是要写 ->setTo("1115007981@qq.com")//设置收件人 ->setSubject("黑势力科技")//摘要 ->send(); //如发送成功,则返回一个bool类型的值
Here we need to talk about several methods.
1. The compose() method in the mailer componentcompose($view = null, array $params = [])
$params can contain various parameters that we need to load into the template, such as our 'token', 'adminuser' above ' and other parameter values.
It is worth mentioning that
When the keys of the value are
and text, it means loading our html block template and text block template respectively. Create our email template in view
We open our common/views/mail/layout and we can see the mailer component The basic template has been written for us, so we only need to write some simple html code, and then call the layout.
//例如:<P>尊敬的管理员<?=$adminuser;?></P><p> 你好</p><p>你的重置密码连接为:</p><a href="http://<?=$token?>">http://<?=$token?></a><p>请在5分钟之内重置密码,否则密码想会失效</p> //token 为我们在compose中传入的值
This is the function of sending emails in yii2
Related recommendations:
Yii2 Forgot password operation based on email verification yii2 resetful authorization verificationThe above is the detailed content of yii2 send email function. For more information, please follow other related articles on the PHP Chinese website!