Send via PHP email

不言
Release: 2023-03-25 07:50:02
Original
2256 people have browsed it

This article mainly introduces the mailbox sending in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it.

In the project, when the user changes the password, a verification code needs to be sent. I went to the user's mailbox, so I took a look and recorded it here.

1. Turn on the SMTP service

The test is using your own qq mailbox. First, you need to turn on the SMTP service of the mailbox. After turning it on, you must remember the authorization code given to you. The authorization code must be the latest Yes, mine has been turned on, and I have secretly memorized the authorization code.

2. Install phpmailer

Use composer and run it in the project directory

composer require phpmailer/phpmailer

After execution, you can see the phpmailer directory in the vendor directory. This is a php function package for sending emails, which is very convenient.

3. Send email function

Send via PHP email

1 /** 
2  * 发送邮件方法 
3  * @param string $to:接收者邮箱地址 
4  * @param string $title:邮件的标题 
5  * @param string $content:邮件内容 
6  * @return boolean  true:发送成功 false:发送失败 
7  */ 
8 function sendMail($to,$title,$content){ 
9     
10     
11     //实例化PHPMailer核心类
12     $mail = new \PHPMailer();
13     //是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
14     $mail->SMTPDebug = 1;
15     //使用smtp鉴权方式发送邮件
16     $mail->isSMTP();
17     //smtp需要鉴权 这个必须是true
18     $mail->SMTPAuth=true;
19     //链接qq域名邮箱的服务器地址
20     $mail->Host = 'smtp.qq.com';
21     //设置使用ssl加密方式登录鉴权
22     $mail->SMTPSecure = 'ssl';
23     //设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587
24     $mail->Port = 465;
25     //设置smtp的helo消息头 这个可有可无 内容任意
26     $mail->Helo = 'Hello smtp.qq.com Server';
27     //设置发件人的主机域 可有可无 默认为localhost 内容任意,建议使用你的域名
28     $mail->Hostname = 'localhost';
29     //设置发送的邮件的编码 可选GB2312 我喜欢utf-8 据说utf8在某些客户端收信下会乱码
30     $mail->CharSet = 'UTF-8';
31     //设置发件人姓名(昵称) 任意内容,显示在收件人邮件的发件人邮箱地址前的发件人姓名
32     $mail->FromName = '天下第一帅';
33     //smtp登录的账号 这里填入字符串格式的qq号即可
34     $mail->Username ='';
35     //smtp登录的密码 使用生成的授权码 你的最新的授权码
36     $mail->Password = '';
37     //设置发件人邮箱地址 这里填入上述提到的“发件人邮箱”
38     $mail->From = '';
39     //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false
40     $mail->isHTML(true);
41     //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
42     $mail->addAddress($to,'测试通知');
43     //添加多个收件人 则多次调用方法即可
44     // $mail->addAddress('xxx@qq.com','lsgo在线通知');
45     //添加该邮件的主题
46     $mail->Subject = $title;
47     //添加邮件正文 上方将isHTML设置成了true,则可以是完整的html字符串 如:使用file_get_contents函数读取本地的html文件
48     $mail->Body = $content;
49 
50     //为该邮件添加附件 该方法也有两个参数 第一个参数为附件存放的目录(相对目录、或绝对目录均可) 第二参数为在邮件附件中该附件的名称
51     // $mail->addAttachment('./d.jpg','mm.jpg');
52     //同样该方法可以多次调用 上传多个附件
53     // $mail->addAttachment('./Jlib-1.1.0.js','Jlib.js');
54 
55     $status = $mail->send();
56 
57     //简单的判断与提示信息
58     if($status) {
59         return true;
60     }else{
61         return false;
62     }
63 }
Copy after login

Send via PHP email

4. Test effect

In the controller, write a method to test whether the function works well.


1  public function test(){
2         sendMail('email address','邮件的标题','发送成功');
3     }
Copy after login

Related recommendations:

php two-dimensional array sorting



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

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!