PHPMailer usage tutorial (PHPMailer sends mail instance analysis) _ php instance

Source: Internet
Author: User
Php provides the mail () function, but it is not easy to use. PHPMailer is a good email sending tool. next we will introduce it in detail, if you need to know, you can refer to php, which provides the mail () function, but it is not easy to use. PHPMailer is a good email sending tool and is very easy to use!

Use PHPMailer to send emails:

The code is as follows:


Header ("content-type: text/html; charset = utf-8 ");
Ini_set ("magic_quotes_runtime", 0 );
Require 'class. phpmailer. php ';
Try {
$ Mail = new PHPMailer (true );
$ Mail-> IsSMTP ();
$ Mail-> CharSet = 'utf-8'; // It is important to set the character encoding of the email. Otherwise, Chinese characters are garbled.
$ Mail-> SMTPAuth = true; // enable authentication
$ Mail-> Port = 25;
$ Mail-> Host = "smtp.163.com ";
$ Mail-> Username = "phpddt1990@163.com ";
$ Mail-> Password = "this is the Password ";
// $ Mail-> IsSendmail (); // comment out if no sendmail component is available; otherwise, the error message "cocould not execute:/var/qmail/bin/sendmail" appears.
$ Mail-> AddReplyTo ("phpddt1990@163.com", "mckee"); // Reply to address
$ Mail-> From = "phpddt1990@163.com ";
$ Mail-> FromName = "www.phpddt.com ";
$ To = "987044391@qq.com ";
$ Mail-> AddAddress ($ );
$ Mail-> Subject = "phpmailer test title ";
$ Mail-> Body = "phpmail demo this is the test content for phpmailer by php Dianping (www.phpddt.com ";
$ Mail-> AltBody = "To view the message, please use an HTML compatible email viewer! "; // When the email does not support html, it can be omitted.
$ Mail-> WordWrap = 80; // you can specify the length of each string.
// $ Mail-> AddAttachment ("f:/test.png"); // you can add attachments.
$ Mail-> IsHTML (true );
$ Mail-> Send ();
Echo 'email sent ';
} Catch (phpmailerException $ e ){
Echo "failed to send the email:". $ e-> errorMessage ();
}
?>


Open my QQ email and you will see:

The test was successful:

As you can see, PHPMailer supports html format sending, and supports sending images and attachments! It has been tested to be compatible with various SMTP servers!

If an error occurs when adding an attachment:

That is because (set_magic_quotes_runtime () has been disabled. This feature has been completely removed from PHP6.

You can comment out or delete the wrong row, or add the @ symbol before set_magic_quotes_runtime ().
Or configure error_reporting = E_ALL &~ E_NOTICE &~ E_DEPRECATED

PHPMailer and test file download: phpmailer.rar

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.