Php 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:
Copy codeThe Code is as follows:
<? Php
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 = "$ 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