PHP phpmailer Implementation of PHP e-mail Function example

Source: Internet
Author: User
Tags php send mail
This article is mainly for you to introduce the Phpmailer implementation of PHP e-mail function, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the Phpmailer implementation of the PHP e-mail function of the specific code for your reference, the specific content as follows

The first step: open the URL download phpmailer,phpmailer need PHP sockets extension support, and login QQ mailbox SMTP server must be encrypted by SSL, PHP also contains OpenSSL support.

Step Two: use the phpinfo () function to view the socket and OpenSSL extension information (WAMP server has enabled the extension by default).

If OpenSSL is not turned on, open the php.ini file.

First check the php.ini, if Extension=php_openssl.dll exists, remove the previous comment '; ' If it exists, add extension=php_openssl.dll if there is no such line.

Phpmailer Core File

Step Three: QQ Mailbox Settings

All major mailboxes support the SMTP protocol, but not all mailboxes are turned on by default, and you can open them manually in the settings of your mailbox.

Third-party services after providing the account and password, you can log in to the SMTP server, through which to control the way the mail transfer.

Fourth Step: turn on the SMTP service

Select the IMAP/SMTP service and click on the service to open

Fifth Step: Verify the Secret warranty

Send SMS "Configure mail client" to 1069-0700-69

Sixth step: Get the Authorization code

SMTP Server Authentication password, need safekeeping (PS: password directly without space)

Seventh Step: php Send mail

Basic code

The following code demonstrates how Phpmailer is used, and note the configuration process for the Phpmailer instance.

Introduction of Phpmailer's core document require_once ("phpmailer/class.phpmailer.php"); Require_once ("phpmailer/class.smtp.php"); Instantiate the Phpmailer core class $mail = new Phpmailer ();//Whether to enable debug for SMTP debugging development environment It is recommended to turn on the production environment comments off can be turned off by default Debug debug mode $mail->smtpdebug = 1 ;//Use SMTP authentication to send mail $mail->issmtp ();//SMTP requires authentication this must be True$mail->smtpauth = true;//link QQ domain Mailbox server address $mail->host = ' smtp.qq.com ';//set to use SSL encryption to login authentication $mail->smtpsecure = ' SSL ';//Set SSL to connect the SMTP server to the remote server port number $mail->port = 465;// Set the encoding of the sent message $mail->charset = ' UTF-8 ';//Set the sender's nickname to display the sender's name before the sender's email address of the recipient message $mail->fromname = ' sender nickname ';//SMTP login account QQ mailbox can $mail->username = ' 12345678@qq.com ';//SMTP login password using the generated authorization code $mail->password = ' ********** ';//Set the sender's email address with the login account $mail->from = ' 12345678@qq.com ';//whether the message body is HTML-encoded note here is a method $mail->ishtml (true);//Set the recipient's mailbox address $mail-> AddAddress (' 87654321@qq.com ');//Add multiple recipients by calling the method multiple times $mail->addaddress (' 87654321@163.com ');//Add the subject of the message $mail-> Subject = ' mail subject ';//Add message body $mail->body = ' 

I use the code in thinkphp5.0

/*** Mail Send * @param $to recipient * @param string $subject message header * @param string $content message content (content after HTML template rendered) * @throws exception* @thr oWS phpmailerexception*/function send_email ($to, $subject = ", $content =") {Vendor (' Phpmailer.  Phpmailerautoload ');//require_once ' vendor/phpmailer/phpmailerautoload.php ';  $mail = new Phpmailer;  $arr = db (' config ')->where (' Inc_type ', ' SMTP ')->select ();  $config = convert_arr_kv ($arr, ' name ', ' value '); $mail->charset = ' UTF-8 '; Set the message encoding, default iso-8859-1, if the text must be set, otherwise garbled $mail->issmtp ();//enable SMTP debugging//0 = off (for production use)//1 = CL   Ient messages//2 = client and server messages $mail->smtpdebug = 0;//Debug Output format//$mail->debugoutput = ' html ';//SMTP server     $mail->host = $config [' smtp_server '];//Port-likely to IS, 465 or 587 $mail->port = $config [' Smtp_port ']; if ($mail->port = = = 465) $mail->smtpsecure = ' SSL ';//Use security protocol//whether to using SMTP authentication $mail->smtpauth = true;//Send mailbox $mail->username = $config [' Smtp_user '];//password $mail->password = $config [' smtp_pwd '];//set who the message was to be sent from $mail->setfrom ($config [' SMTP _user '], $config [' email_id ']);//reply to address//$mail->addreplyto (' replyto@example.com ', ' first Last ');//Receive Mail party if (is_    Array ($to)) {foreach ($to as $v) {$mail->addaddress ($v);  }}else{$mail->addaddress ($to); } $mail->ishtml (TRUE);//Send As html//title $mail->subject = $subject;//html content conversion $mail->msghtml ($content);//r Eplace the plain text body with one created manually//$mail->altbody = ' This is a plain-text message body ';//Add attachment//$mai L->addattachment (' images/phpmailer_mini.png ');//send the message, check for errors return $mail->send ();}

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.