Table of Contents
1. Turn on the SMTP service
2. Install phpmailer
3. Send email function
4. Test effect

Send via PHP email

May 02, 2018 am 09:36 AM
php send Mail

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to download, install and register the Hong Kong Digital Currency Exchange app 2025 How to download, install and register the Hong Kong Digital Currency Exchange app 2025 Apr 30, 2025 pm 07:18 PM

The download, installation and registration process of the Hong Kong Digital Currency Exchange app is very simple. Users can quickly obtain and use this app through the official app download link provided in this article. This article will introduce in detail how to download, install and register the Hong Kong Digital Currency Exchange app to ensure that every user can complete the operation smoothly.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

What are the three giants in the currency circle? Top 10 Recommended Virtual Currency Main Exchange APPs What are the three giants in the currency circle? Top 10 Recommended Virtual Currency Main Exchange APPs Apr 30, 2025 pm 06:27 PM

In the currency circle, the so-called Big Three usually refers to the three most influential and widely used cryptocurrencies. These cryptocurrencies have a significant role in the market and have performed well in terms of transaction volume and market capitalization. At the same time, the mainstream virtual currency exchange APP is also an important tool for investors and traders to conduct cryptocurrency trading. This article will introduce in detail the three giants in the currency circle and the top ten mainstream virtual currency exchange APPs recommended.

How to download the Hong Kong Digital Currency Exchange app? The top ten digital currency exchange apps are included How to download the Hong Kong Digital Currency Exchange app? The top ten digital currency exchange apps are included Apr 30, 2025 pm 07:12 PM

The methods to download the Hong Kong Digital Currency Exchange APP include: 1. Select a compliant platform, such as OSL, HashKey or Binance HK, etc.; 2. Download through official channels, iOS users download on the App Store, Android users download through Google Play or official website; 3. Register and verify their identity, use Hong Kong mobile phone number or email address to upload identity and address certificates; 4. Set security measures, enable two-factor authentication and regularly check account activities.

Top 10 Digital Currency Trading Apps Rankings: Top 10 Safe and Easy Virtual Digital Currency Exchanges Top 10 Digital Currency Trading Apps Rankings: Top 10 Safe and Easy Virtual Digital Currency Exchanges Apr 30, 2025 pm 04:24 PM

The rankings of the top ten digital currency trading apps are: 1. Binance, 2. OKX, 3. Coinbase, 4. Kraken, 5. Huobi, 6. KuCoin, 7. Bitfinex, 8. Bittrex, 9. Gemini, 10. Poloniex. These exchanges provide a safe and reliable trading environment and a variety of trading methods. Users can operate on each platform through the steps of registration, email verification, KYC identity verification, deposit and start trading.

How to set the rotation effect of HTML elements How to set the rotation effect of HTML elements Apr 30, 2025 pm 02:42 PM

How to set the rotation effect of an element in HTML? It can be achieved using CSS and JavaScript. 1. The transform property of CSS is used for static rotation, such as rotate(45deg). 2. JavaScript can dynamically control rotation, which is implemented by changing the transform attribute.

How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China May 08, 2025 pm 10:51 PM

In the cryptocurrency market, choosing a reliable trading platform is crucial. As a world-renowned digital asset exchange, the OK trading platform has attracted a large number of novice users in mainland China. This guide will introduce in detail how to register and use it on the OK trading platform to help novice users get started quickly.

See all articles