yii2.0自带email
大部分框架都有自带的email邮件发送类,yii的邮件发送也很简单,代码如下:
1 修改配置文件,普通版在(config/web.php)。高级版默认配置在/common/config/main-local.php
2
3 'components' => [
'mailer' => [
4 'class' => 'yii\swiftmailer\Mailer',
5 'useFileTransport' => false, //false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
6 'transport' => [
7 'class' => 'Swift_SmtpTransport',
8 'host' => 'smtp.163.com', //每种邮箱的host配置不一样
9 'username' => '18903181076@163.com', //发件人邮箱
10 'password' => 'xgslagfpomsxuseq', //授权码
11 'port' => '25',
12 'encryption' => 'tls',
13 ],
14 'messageConfig'=>[
15 'charset'=>'UTF-8',
16 'from'=>['18903181076@163.com'=>'nickname'] //发件人昵称
17 ],
18 ],
],
19
20
21 控制器发送邮件(自定义消息)
22
23
24 $mail = Yii::$app->mailer->compose();
25 $mail->setTo('****@qq.com'); //接收人邮箱
26 $mail->setSubject("test"); //邮件标题
27 $mail->setHtmlBody("发送内容发送内容发送内容"); //发送内容(可写HTML代码)
28 if ($mail->send()){
29 echo "成功";
30 }else{
31 echo "失败";
32 }
33
34
35 如果发送页面
36
37
38 $mail = Yii::$app->mailer->compose("email"); //在mail文件夹下创建email页面文件,在文件内编辑内容
39 $mail->setTo('****@qq.com'); //接收人邮箱
40 $mail->setSubject("test"); //邮件标题
41 if ($mail->send()){
42 echo "成功";
43 }else{
44 echo "失败";
45 }