Home  >  Article  >  Backend Development  >  yii2 send email function

yii2 send email function

不言
不言Original
2018-04-19 14:45:132417browse

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.

Configuration environment
  1. 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',
                ],
            ],
# in our main.php
  1. ##Call this component in the controller or model.

  2. $mailer = \Yii::$app->mailer->compose('seekpass',['html'=>'html','adminuser'=>$post['Admin']['adminuser'],'token'=>$_SERVER['HTTP_HOST'].Url::toRoute(['manage/emailchangepass'])."&timestamp=".$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 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)

$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 html and text, it means loading our html block template and text block template respectively.

  1. 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.

We write what we want in the mail folder The html format of sending files

//例如:<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

First of all, to implement the email function, we need to understand the concept of email classes in yii2

yii2 In order to solve our email sending problem, the swiftMailer extension is provided for us.

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.

Configuration environment

First configure our
    mailer
  1. component

    //主要是对组件中各种必要的发送邮箱的属性进行配置&#39;mailer&#39; => [                &#39;class&#39; => &#39;yii\swiftmailer\Mailer&#39;,                &#39;viewPath&#39; => &#39;@common/mail&#39;,                &#39;useFileTransport&#39; =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
                    &#39;transport&#39; => [                    &#39;class&#39; => &#39;Swift_SmtpTransport&#39;,                    &#39;host&#39; => &#39;smtp.qq.com&#39;,  //每种邮箱的host配置不一样
                        &#39;username&#39; => &#39;11150****1@qq.com&#39;,                    &#39;password&#39; => &#39;*****&#39;,//密码不是指的登录密码
                        &#39;port&#39; => &#39;465&#39;,                    &#39;encryption&#39; => &#39;ssl&#39;,
                    ],
                ],
    # in our main.php

  2. ##Call this component in the controller or model.
  1. $mailer = \Yii::$app->mailer->compose(&#39;seekpass&#39;,[&#39;html&#39;=>&#39;html&#39;,&#39;adminuser&#39;=>$post[&#39;Admin&#39;][&#39;adminuser&#39;],&#39;token&#39;=>$_SERVER[&#39;HTTP_HOST&#39;].Url::toRoute([&#39;manage/emailchangepass&#39;])."&timestamp=".$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 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)

$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

html

and text, it means loading our html block template and text block template respectively. Create our email template in view

  1. 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.

  2. We write what we want in the mail folder HTML format of sending files
//例如:<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 verification

The above is the detailed content of yii2 send email function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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