How PHP sends Mail

Source: Internet
Author: User
Tags openssl version ssl connection using git

as a PHP starter developer, there's always the need to add a way to automatically send mail using your own domain name as the sender's email address. Used to send a verification code , notification information, and so on to the user. We introduce php send mail

Introduction of Phpmailer:

Advantages:

Can be run on top of any platform

Support for SMTP authentication

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

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

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

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.

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

Flexible Debug Support

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 mail

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, because we will use the SMTP server to send, here is recommended to put the first two items open! 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):

/* Send mail method * @param $to: Recipient $title: Title $content: Message content * @return bool true: Send succeeded false: Send failed */function sendMail ($to, $title, $     Content) {//Introducing Phpmailer's core file using require_once contains warning require_once ("phpmailer/class.phpmailer.php") to avoid duplicate definitions of phpmailer classes;    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 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;    Set SMTP Helo message header This optional content arbitrary//$mail->helo = ' Hello smtp.qq.com Server ';    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 name (nickname) any content that appears in theThe sender's name before the sender's email address of the recipient message $mail->fromname = ' topic.alibabacloud.com ';    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 that you saved) $mail->password = ' Sqyofzbqlfkntbncl ';    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 a property of true or False $mail->ishtml (true); To set a recipient's mailbox address this 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 argument here is not very meaningful $mail->addaddress ($to, ' LSGO online notification ')    ;    Adding multiple recipients calls the method multiple times//$mail->addaddress (' xxx@163.com ', ' LSGO online notification ');    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 such as: Use the file_get_contents function to read the local HTML file $mail->body = $content;    Add an attachment to the message the method also has two parameters the first parameter is the directory where the attachment is stored (either relative to the directory, or the absolute directory) The second parameter is the name of the attachment in the message attachment//$mail->addattachment ('./d.jpg ', ' mm.jpg ');    The same method can be called multiple times to upload several attachments//$mail->addattachment ('./jlib-1.1.0.js ', ' jlib.js ');    $status = $mail->send ();  Simple judgment and hint information if ($status) {      return true;    }else{return false; }}

Next, you can call the public method test to send the message

Phprequire_once ("./functions.php"); $flag = SendMail (' 456789@qq.com ', ' online notification ', ' congratulations on your successful joining topic.alibabacloud.com! if ($flag) {    echo] sent the message successfully! ";} else{    echo "failed to send mail! ";}? >

The above is the use of phpmailer, hope can help everyone.

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.