Home >Backend Development >PHP Tutorial >PHP implements QQ mailbox sending method through PHPMailer class library
In fact, the PHP email sending function is not necessarily used in every project, but in fact, every complete project will generally include a PHP email sending function.
The general email sending function is commonly used for registration activation, password retrieval, message reply, etc. And this is also a headache for many people. After all, this function cannot be achieved by a few lines of code, and it cannot be written by you using time and energy. This function is actually a finished product. What we need now is how to use it. So let's explain how to use the PHPMailer class library to implement the PHP mailbox sending function.
First, let’s talk about how to use your own domain name as the email address for sending emails?
In the PHP environment, the function mail( ), but this function requires the server to support sendmail or a mail sending server that does not require relay must be set up. But now it is almost impossible to find a mail sending relay that does not require authentication, so using the mail function often fails. send email. So is PHP unable to send emails? The answer is no. If you are familiar with the POP3/SMTP protocol and combine it with the socket function, you can write an efficient and stable email sending program, but this workaround is equivalent to using POP3/SMTP to write a POP3/SMTP The client is still difficult for ordinary developers.
Fortunately, there are abundant Internet resources now, and PHPMailer was born at this time.
What are the advantages of PHPMailer?
Can run on any platform
Support SMTP verification
Specify multiple recipients, CC address, BCC address and reply address when sending mail; Note: Add CC and BCC are only supported in SMTP mode under the win platform
Supports multiple email encodings including: 8bit, base64, binary and quoted-printable
Supports redundant SMTP servers, that is, you can specify the main SMTP server address or only the backup The smtp server
supports emails with attachments, and you can add attachments in any format to emails --- of course your server must have enough bandwidth to support
Customize email header information, which is similar to sending header information through the header function in PHP Similar
Supports making the email body into HTMl content, then you can insert pictures into the email body
Flexible debug support
Tested and compatible SMTP servers include: Sendmail, qmail, Postfix, Imail, Exchange, etc.
How to download the PHPMailer code package?
PHPMailer project address: https://github.com/PHPMailer/PHPMailer Use the git command to clone it locally, or directly click "Download ZIP" at the bottom right of the project page to get it Complete PHPMailer code package.
How to use PHPMailer with the "domain name mailbox" under qq mailbox to use your own domain name as an email address to send emails?
====
The following is divided into two parts to explain how to use your own domain name to open qq mailbox for free Use PHPMailer in the "Domain Name Mailbox" under the PHP environment and cooperate with the newly opened "Domain Name Mailbox" to automatically send emails.
====
The first part: Opening the "domain name mailbox" under qq mailbox
Since opening the "domain name mailbox" is only an understanding of the operation method, this part will only be briefly discussed.
Enter QQ mailbox, click "Settings" in the upper left corner of the page, then click the "Account" tab under "Mailbox Settings", drag the scroll bar, you will see "Domain Name Mailbox", click "Manage Domain Name Mailbox" " to enter the page for adding a domain name mailbox; or directly click http://domain.mail.qq.com/
Click "Create a domain name mailbox" and follow the page prompts to add your own domain name and verify it. PS: If you have successfully added "domain name mailbox" under your qq mailbox, then after entering qq mailbox, there will be the text "domain name mailbox" after the "Settings" button on the upper left side of the page to add your own exclusive email address to the domain name mailbox, click Go to "Member Management" in the "Domain Name Mailbox" that has been approved, click "Add Member", enter your favorite mailbox name under your domain name, and then add a qq number (the qq number will serve as the user of the newly added mailbox. The qq account needs to open a qq mailbox and not close the digital account)
The qq mailbox of the qq number you just entered will receive a notification email to open the domain name mailbox. Click "Accept this email account" in the email to use The sender's email address for sending emails with PHPMailer is now set. For convenience of description, this email address is called the "sender's email address".
In order to ensure the security of the login password of the QQ account, please set an "independent password" for the QQ account. Set it in the QQ mailbox "Settings" - "Account" - "Account Security". The password set there, For the convenience of description in this article, it is called "independent password".
Now the "Domain Name Mailbox" under QQ Mailbox has been opened and the email address of the sender will be sent using PHPMailer later. Here is a list of several key information that PHPMailer will need to use later: QQ number, the QQ number added in the third step above, "independent password", "sender's email address", sender's name...
Part 2: Use PHPMailer to write the code for sending emails
PHPMailer requires PHP's socket extension support, and PHPMailer requires SSL encryption when linking to qq domain name mailbox (qq mailbox has recently been restricted, and new domain name mailboxes are no longer available) Allow ordinary linking through port 25 of the SMTP protocol, and only allow port 465 or 587 of SSL [One of my domain name mailboxes opened during the beta period can still connect to port 25. It may be a security policy that restricts new connections. Open port 25 of the domain name email account]). PHP must be supported by openssl. You can check phpinfo. If the following two items are present, you can use it. You don’t need to worry about the openssl version number. PHP in many virtual hosts does not support openssl. Extended, then you may be in tragedy.
Things to note here, before that, we must enable openssl in the php environment
This part is only a code example, there are some comments in the code to explain and serve as inspiration.
The downloaded PHPMailer is decompressed and slimmed down. It only needs four files: class.phpmailer.php, class.pop3.php, class.smtp.php and PHPMailerAutoload.php. The language folder is optional, that Mainly used for display information during debugging. Take a look at the file size of class.phpmailer.php. It’s over 110kb, which is quite scary.
<?php //引入PHPMailer的核心文件 使用require_once包含避免出现PHPMailer类重复定义的警告 require_once("phpmailer/class.phpmailer.php"); //示例化PHPMailer核心类 $mail = new PHPMailer(); //是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式 $mail->SMTPDebug = 1; //使用smtp鉴权方式发送邮件,当然你可以选择pop方式 sendmail方式等 本文不做详解 //可以参考http://phpmailer.github.io/PHPMailer/当中的详细介绍 $mail->isSMTP(); //smtp需要鉴权 这个必须是true $mail->SMTPAuth=true; //链接qq域名邮箱的服务器地址 $mail->Host = 'smtp.qq.com'; //设置使用ssl加密方式登录鉴权 $mail->SMTPSecure = 'ssl'; //设置ssl连接smtp服务器的远程服务器端口号 可选465或587 $mail->Port = 465; //设置smtp的helo消息头 这个可有可无 内容任意 $mail->Helo = 'Hello smtp.qq.com Server'; //设置发件人的主机域 可有可无 默认为localhost 内容任意,建议使用你的域名 $mail->Hostname = 'jjonline.cn'; //设置发送的邮件的编码 可选GB2312 我喜欢utf-8 据说utf8在某些客户端收信下会乱码 $mail->CharSet = 'UTF-8'; //设置发件人姓名(昵称) 任意内容,显示在收件人邮件的发件人邮箱地址前的发件人姓名 $mail->FromName = '晶晶在线'; //smtp登录的账号 这里填入字符串格式的qq号即可 $mail->Username ='888888'; //smtp登录的密码 这里填入“独立密码” 若为设置“独立密码”则填入登录qq的密码 建议设置“独立密码” $mail->Password = 'xxxxxxx'; //设置发件人邮箱地址 这里填入上述提到的“发件人邮箱” $mail->From = 'register@jjonline.cn'; //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false $mail->isHTML(true); //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 //这里第二个参数的意义不大 $mail->addAddress('xxx@qq.com','晶晶在线用户'); //添加多个收件人 则多次调用方法即可 $mail->addAddress('xxx@163.com','晶晶在线用户'); //添加该邮件的主题 $mail->Subject = 'PHPMailer发送邮件的示例'; //添加邮件正文 上方将isHTML设置成了true,则可以是完整的html字符串 //如:使用file_get_contents函数读取本地的html文件 $mail->Body = "这是一个PHPMailer发送邮件的一个测试用例"; //为该邮件添加附件 该方法也有两个参数 第一个参数为附件存放的目录(相对目录、或绝对目录均可) 第二参数为在邮件附件中该附件的名称 $mail->addAttachment('./d.jpg','mm.jpg'); //同样该方法可以多次调用 上传多个附件 $mail->addAttachment('./Jlib-1.1.0.js','Jlib.js'); //发送命令 返回布尔值 //PS:经过测试,要是收件人不存在,若不出现错误依然返回true 也就是说在发送之前 自己需要些方法实现检测该邮箱是否真实有效 $status = $mail->send(); //简单的判断与提示信息 if($status) { echo '发送邮件成功'; }else{ echo '发送邮件失败,错误信息未:'.$mail->ErrorInfo; } ?>
Special reminder: The above code snippet is for reference only. If you want to integrate it into the website system, you can encapsulate the code snippet into a function or class library for calling; in addition, PHPMailer supports adding CC and BCC under the win32 platform. . Because I use centos on my server, I have not tested it.