PHP using Phpmailer with QQ mailbox Implementation email

Source: Internet
Author: User
Tags openssl version using git

Because of the project needs, to achieve to our site users to send mail, so there is this blog. The following is a combination of online examples of my own practice. Hope to be of help to everyone.

Introduction of Phpmailer:

Advantages:

    1. Can be run on top of any platform

    2. Support for SMTP authentication

    3. Specify multiple recipients, CC address, send address and reply address when sending mail; Note: Add cc, BCC only under the win platform SMTP mode support

    4. Supports multiple message encodings including: 8bit,base64,binary and Quoted-printable

    5. Support for redundant SMTP servers, that is, you can specify the primary SMTP server address and can only specify the backup SMTP server

    6. Supports mail with attachments, and can add attachments in any format to your message-your server, of course, has a large enough bandwidth to support it.

    7. Custom message header information, which is similar to sending header information in PHP via the header function
      Support to make the message body HTML content, you can insert the diagram in the message body
      Chip

    8. Flexible Debug Support

    9. Tested and compatible SMTP servers include: Sendmail,qmail,postfix,imail,exchange, etc.

Phpmailer's acquisition:

Phpmailer Project Address: Https://github.com/PHPMailer/PHPMailer clone to local using git command, or click "Download ZIP" directly at the bottom right of the project page You can get the full Phpmailer code package and unzip it locally.

Step one: So that our QQ mailbox can send mailthe approach and ingredients of the plate surface

How can I send a message here? In fact, our mailbox can send mail, but to achieve in our website to send mail, it is necessary to set up our QQ mailbox, because at this time our site is now as a third-party client exists.

    1. As we are going to use the SMTP server to send, it is recommended to open the previous two! When you click Open, it will prompt:

When you have completed the above steps, you will get an authorization code, you can copy it, we will use it later (open two words will get two authorization code, with the last authorization code!) Or click the Generate authorization code below to get a new authorization code, be sure to the latest! )。

Step two: Enable our PHP to use QQ mailbox to send mail

Phpmailer need PHP Socket extension support, and Phpmailer link QQ domain name mailbox need SSL encryption method, solid PHP also have OpenSSL support, can view phpinfo, the following two items are present can be used, Where the OpenSSL version number and so on, many virtual host PHP does not support the OpenSSL extension, then you may be tragic.

Step three: Phpmailer to do some processing

Since we have a lot of files in the Phpmailer folder that we downloaded, we don't need to waste the memory. We can thin this folder, here I only saved the following several files: class.phpmailer.php, class.phpmaileroauth.php, class.pop3.php, class.smtp.php, phpmailerautoload.php.

Step four: Write code to send mail

Here I am directly encapsulating a public method (written in the functions.php file):

12345678910111213141516171819202122232425262728293031323334353637383940 /*发送邮件方法 *@param $to:接收者 $title:标题 $content:邮件内容 *@return bool true:发送成功 false:发送失败 */functionsendMail($to,$title,$content){    //引入PHPMailer的核心文件 使用require_once包含避免出现PHPMailer类重复定义的警告    require_once("phpmailer/class.phpmailer.php");     require_once("phpmailer/class.smtp.php");    //实例化PHPMailer核心类    $mailnewPHPMailer();    //是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式    $mail->SMTPDebug = 1;    //使用smtp鉴权方式发送邮件    $mail->isSMTP();    //smtp需要鉴权 这个必须是true    $mail->SMTPAuth=true;    //链接qq域名邮箱的服务器地址    $mail->Host = ‘smtp.qq.com‘;    //设置使用ssl加密方式登录鉴权    $mail->SMTPSecure = ‘ssl‘;    //设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587    $mail->Port = 465;    //设置smtp的helo消息头 这个可有可无 内容任意    // $mail->Helo = ‘Hello smtp.qq.com Server‘;    //设置发件人的主机域 可有可无 默认为localhost 内容任意,建议使用你的域名    $mail->Hostname = ‘http://www.lsgogroup.com‘;    //设置发送的邮件的编码 可选GB2312 我喜欢utf-8 据说utf8在某些客户端收信下会乱码    $mail->CharSet = ‘UTF-8‘;    //设置发件人姓名(昵称) 任意内容,显示在收件人邮件的发件人邮箱地址前的发件人姓名    $mail->FromName = ‘LSGO实验室‘;    //smtp登录的账号 这里填入字符串格式的qq号即可    $mail->Username =‘[email protected]‘;    //smtp登录的密码 使用生成的授权码(就刚才叫你保存的最新的授权码)    $mail->Password = ‘sqyofzbqlfkntbncl‘;    //设置发件人邮箱地址 这里填入上述提到的“发件人邮箱”    $mail->From = ‘[email protected]‘;    //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false    $mail->isHTML(true);     //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大    $mail->addAddress($to,‘lsgo在线通知‘);    //添加多个收件人 则多次调用方法即可    // $mail->addAddress(‘[email protected]‘,‘lsgo在线通知‘);    //添加该邮件的主题    $mail->Subject = $title;    //添加邮件正文 上方将isHTML设置成了true,则可以是完整的html字符串 如:使用file_get_contents函数读取本地的html文件    $mail->Body = $content;    //为该邮件添加附件 该方法也有两个参数 第一个参数为附件存放的目录(相对目录、或绝对目录均可) 第二参数为在邮件附件中该附件的名称    // $mail->addAttachment(‘./d.jpg‘,‘mm.jpg‘);    //同样该方法可以多次调用 上传多个附件    // $mail->addAttachment(‘./Jlib-1.1.0.js‘,‘Jlib.js‘);    $status$mail->send();    //简单的判断与提示信息    if($status) {        returntrue;    }else{        returnfalse;    }}

OK, now we call this public method to test, I want to send an email informing him to join Lsgo lab

123 <?phprequire_once("./functions.php");$flag= sendMail(‘[email protected]‘,‘lsgo在线通知‘,‘恭喜你成功加入LSGO实验室,开启你的学习之旅吧!‘);if($flag){    echo"发送邮件成功!";}else{    echo "发送邮件失败!";}?>

Pro-Test success!!!

PHP using Phpmailer with QQ mailbox Implementation email

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.