Analysis of Phpmailer e-mail method in PHP

Source: Internet
Author: User
Tags server port ssl connection
Phpmailer Introduction Step One: Make QQ mailbox can send mail step two: Enable PHP to use QQ mailbox to send mail step three: Write the sending Code thinkphp use phpmailer send mail, This article will be through the QQ mailbox as an example to explain the use of Phpmaiiler methods and skills, hope to help everyone.

Introduction of Phpmailer

Can be run on top of any platform, support SMTP authentication, specify multiple recipients, CC address, send address and reply address when sending mail; Note: Add cc, BCC only in win platform under SMTP mode support; Support for a variety of message encodings include: 8bit,base64, binary and quoted-printable; custom header information, which is similar to sending header information in PHP via the header function, allows the message body to be made into HTML content, so you can insert a picture in the message body ; Tested compatible SMTP servers include: Sendmail,qmail,postfix,imail,exchange, etc.

Step one: Enable QQ mailbox to send mail

Our mailbox can be sent mail, but to achieve in our site 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, so need to use the SMTP server to send, here is recommended to put the two front open!

Go to QQ mailbox, click Settings, click Accounts

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, must be up to date!) )。

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

Phpmailer need PHP Socket extension support, and Phpmailer link QQ domain name mailbox requires SSL encryption, and PHP's OpenSSL extension support, you can use Phpinfo to see whether to open the extension.

If not, open two extension support in the PHP installation directory to find PHP.ini.

Step three: Write code to send mail

The index.html code is as follows:


<!doctype html>



Encapsulates a common method (written in the functions.php file):


/** * Send mail method * @param $to: Recipient $title: Title $content: Message content * @return bool true: Send failed */function sendMail ($to, $title, $co  ntent) {require_once ("phpmailer/class.phpmailer.php"); Require_once ("phpmailer/class.smtp.php"); Instantiate the Phpmailer core class $mail = new Phpmailer (); Use SMTP authentication to send mail $mail->issmtp (); SMTP needs authentication This must be true $mail->smtpauth=true; The server address of the link QQ domain mailbox $mail->host = ' smtp.qq.com '; Set the login authentication using SSL encryption $mail->smtpsecure = ' SSL '; Set the SSL connection to the SMTP server's remote server port number, the previous default is 25, but now the new seems to be unavailable optional 465 or 587 $mail->port = 465; Setting the sender's host domain is optional by default to localhost content, it is recommended to use your domain name $mail->hostname = ' http://www.lsgogroup.com '; Set encoding for sent messages optional GB2312 I like Utf-8 said UTF8 in some clients will be garbled $mail->charset = ' UTF-8 '; Set the sender's name (nickname) any content that displays the sender's name before the sender's email address of the recipient's message $mail->fromname = ' sender's name (nickname) '; SMTP login account here to fill in the string format QQ number can $mail->username = ' 12345678@qq.com '; The password for the SMTP login uses the generated authorization code (just the latest authorization code saved) $mail->password = ' Latest Authorization code '; Set the sender's email address here fill in the "sender's mailbox" mentioned above $mail->from = ' 12345678@qq.com '; Whether the message body is HTML-encoded note here is a method that is no longer aThe property is True or False $mail->ishtml (true); To set the recipient's mailbox address the method has two parameters the first parameter is the recipient's mailbox address the second parameter is a different mailbox system that is set to that address. The second parameter here has little meaning $mail->addaddress ($to, ' dear Customer '); Adding multiple recipients calls the method multiple times//$mail->addaddress (' xxx@163.com ', ' Dear Customer '); Add the subject of the message $mail->subject = $title; Adding ishtml to true on top of the message body can be a complete HTML string $mail->body = $content; $status = $mail->send (); Judgment and hint information if ($status) {return true;} else{return false;}}



The index.php code is as follows:


<?phprequire_once ("./functions.php"); $to =$_post[' mail '; $title =$_post[' title ']; $content =$_post[' content '];$ Flag = SendMail ($to, $title, $content), if ($flag) {echo ' sent the message successfully! ";} else{echo "failed to send mail! ";}? >



If you use the QQ Enterprise mailbox, then the link QQ domain Mailbox server address and the SMTP login password is different:


Link QQ domain name Mailbox server address $mail->host = ' smtp.exmail.qq.com ';//smtp login password (QQ Enterprise mailbox login password) $mail->password = ' login password ';



thinkphp sending mail using Phpmailer

Phpmailer Extract to Thinkphplibraryvendor

Create a new function.php in the common folder


/** * mail Send function * @param $to: Recipient $title: Title $content: Message content * @return bool true: Send success false: Send failed */function sendMail ($to, $title, $content) {Vendor (' phpmailer.phpmailerautoload '); Vendor (' PHPMailer.class.smtp '); $mail = new Phpmailer (); Instantiate $mail->issmtp (); Enable SMTP $mail->host=c (' mail_host '); The name of the SMTP server $mail->smtpsecure = C (' mail_secure '); $mail->port = C (' Mail_port '); $mail->smtpauth = C (' Mail_smtpauth '); Enable SMTP authentication $mail->username = C (' Mail_username '); Your mailbox name $mail->password = C (' Mail_password '); email password $mail->from = C (' Mail_from '); The sender's address (that is, your email address) $mail->fromname = C (' Mail_fromname '); Sender's name $mail->addaddress ($to, "Dear Customer"); $mail->wordwrap = 50; Sets the length of characters per line $mail->ishtml (C (' mail_ishtml ')); Whether HTML-formatted mail $mail->charset=c (' Mail_charset '); Set the message encoding $mail->subject = $title; Message subject $mail->body = $content; Mail content $mail->altbody = "Hello"; The message body does not support an alternate display of HTML return ($mail->send ());} 


Add configuration file config.php


Configure mail sending server ' mail_host ' = ' smtp.qq.com ',//SMTP server name ' Mail_smtpauth ' =>true,//Enable SMTP authentication ' Mail_username ' + ' 12345678@qq.com ',//Your mailbox name ' mail_from ' = ' 12345678@qq.com ',//Sender address ' mail_fromname ' = ' 12345678@qq.com ',//Sender's name ' Mail_password ' = ' xxxxxx,//email password ' mail_charset ' + ' utf-8 ',//Set message encoding ' mail_ishtml ' =>true,//html format mail ' Mail_ Port ' = ' 465 ',//set SSL to connect the SMTP server's remote server port number ' mail_secure ' = ' SSL ',//set to use SSL encryption to login authentication


The end is to send mail using Phpmailer


<!doctype html>
Public function Add () {  if (sendMail ($_post[' mail '],$_post[' title '],$_post[' content '))   echo "sent successfully";  else   echo "Send Failed";}
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.