PHPMailer in PHP for instance analysis and phpmailer for instance analysis

Source: Internet
Author: User
Tags ssl connection

PHPMailer in PHP for instance analysis and phpmailer for instance analysis

The following uses QQ mail as an example to describe PHPMaIiler in the following four aspects:

PHPMailer introduction Step 1: Enable the QQ mailbox to send mail Step 2: Enable PHP to use the QQ mailbox to send mail Step 3: write the mail sending code ThinkPHP use PHPMailer to send mail

PHPMailer Introduction

It can run on any platform. It supports SMTP verification. Multiple recipients, cc addresses, hidden addresses, and reply addresses are specified during mail sending. Note: added support for CC and BCC only in smtp mode on the win platform. Multiple email encodings are supported, including 8bit, base64, binary, and quoted-printable. custom email header information, this is similar to sending header information through the header function in php. you can insert images into the mail body by making the mail body HTMl content. The compatible SMTP servers tested include Sendmail, qmail, Postfix, Imail, and Exchange.

Step 1: Enable QQ mail to send emails

Our mailbox could have been used to send emails, but to send emails on our website, we need to set up our QQ mailbox, because our website now exists as a third-party client, we need to use the SMTP server for sending. We recommend that you enable the preceding two items here!

Go to QQ mail-> click Settings-> click Account

When you click Enable, it will prompt:

After completing the preceding steps, you will get an authorization code. You can copy the authorization code and use it later. (If you enable the two authorization codes, you will get two authorization codes, must be the latest !).

Step 2: Enable PHP to send emails using QQ mail

PHPMailer requires PHP socket extension support, while PHPMailer requires ssl encryption when linking qq domain name mailbox. PHP openssl extension support is also required. You can use phpinfo to check whether the extension is enabled.

If not, go to the PHP installation directory and find the php. ini file to enable two extensions.

Step 3: Write the email sending code

The index.html code is as follows:

<! Doctype html> 

Encapsulate a public method (written in the functions. php file ):

/*** Sending method ** @ param $ to: Recipient $ title: title $ content: email content * @ return bool true: sent successfully false: failed to send */function sendMail ($ to, $ title, $ content) {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 an email $ mail-> isSMTP (); // for smtp authentication, the value must be true $ mail-> SMTPAuth = true; // the address of the server connecting the qq domain name mailbox $ mail-> Host = 'smtp .qq.com '; // set logon authentication using ssl encryption $ mail-> SMTPSecure = 'ssl '; // set the remote server port number for ssl connection to the smtp server. The default value is 25, however, it seems that the new host is no longer available. Optional values: 465 or 587 $ mail-> Port = 465; // set the host domain of the sender. Optional values: localhost, we recommend that you use your domain name $ mail-> Hostname = 'HTTP: // www.lsgogroup.com '; // set the encoding of the sent email. Optional. GB2312. I like UTF-8. It is said that utf8 will garbled characters in some clients. $ mail-> CharSet = 'utf-8 '; // set the sender's name (nickname) to any content, and display the sender's name before the recipient's email address $ mail-> FromName = 'sender's name (nickname )'; // enter the QQ number in string format for the smtp Logon account $ mail-> Username = '2017 @ qq.com '; // use the generated authorization code for the smtp logon Password (the latest authorization code just saved) $ mail-> Password = 'latest authorization Code '; // set the sender's email address. Enter the aforementioned "sender's email address" $ mail-> From = '2017 @ qq.com '; // whether the email body is html encoded. Note that a method is no longer a property of true or false $ mail-> isHTML (true ); // set the recipient's email address. This method has two parameters. The first parameter is the recipient's email address. The second parameter is the nickname set for this address. Different email addresses are automatically processed and changed. Here, the second parameter is used. not significant $ mail-> addAddress ($, 'Dear customer'); // Add multiple recipients to call the method multiple times. // $ mail-> addAddress ('xxx @ 163.com ', 'Dear customer '); // Add the Subject of the email $ mail-> Subject = $ title; // set isHTML to true at the top of the email body, it can be a complete html string $ mail-> Body = $ content; $ status = $ mail-> send (); // judgment and prompt 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 successfully! ";} Else {echo" failed to send the email! ";}?>

If you are using a QQ enterprise mailbox, the server address that links to the qq domain name mailbox is different from the smtp login password:

// Link the server address of the qq domain name mailbox $ mail-> Host = 'smtp .exmail.qq.com '; // smtp logon password (logon password of QQ enterprise mail) $ mail-> Password = 'logon password ';

ThinkPHP uses PHPMailer to send emails

Decompress PHPMailer to ThinkPHPLibraryVendor.

Create function. php In the Common folder

/*** Email sending function * @ param $ to: Recipient $ title: title $ content: email content * @ return bool true: sent successfully false: failed to send */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'); // smtp server name $ 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 address (that is, your email address) $ mail-> FromName = C ('mail _ fromname'); // sender's name $ MAIL-> AddAddress ($ to, "dear customer "); $ mail-> WordWrap = 50; // set the length of each line. $ mail-> IsHTML (C ('mail _ ISHTML ')); // whether the email is in HTML format $ mail-> CharSet = C ('mail _ charset'); // set the email encoding $ MAIL-> Subject = $ title; // Email Subject $ mail-> Body = $ content; // email content $ mail-> AltBody = "hello "; // The email body does not support alternate display of HTML return ($ mail-> Send ());}

 

Add configuration file config. php

// Configure the MAIL sending server 'mail _ host' => 'smtp .qq.com ', // The smtp server name 'mail _ SMTPAUTH' => true, // enable smtp authentication 'mail _ username' => '2017 @ qq.com ', // your mailbox name 'mail _ from' => '2017 @ qq.com ', // sender address 'mail _ fromname' => '1970 @ qq.com ', // sender name 'mail _ password' => 'xxxxxx, // email password 'mail _ charset' => 'utf-8', // set the email encoding 'mail _ ISHTML '=> TRUE, // whether to use an HTML-format email 'mail _ port' => '000000', // set the remote server PORT number for ssl connection to the smtp server 'mail _ SECURE '=> 'ssl ', // set logon authentication using ssl encryption

 

Finally, use PHPMailer to send emails.

<! Doctype html> 
Public function add () {if (sendMail ($ _ POST ['mail'], $ _ POST ['title'], $ _ POST ['content']) echo "sent successfully"; else echo "failed to 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.