PHP Mailer is a very useful PHP email sending module. It can call local SMTP to send emails or remote SMTP to send emails, however, you need to pay attention to some things during use. Otherwise, the sending may fail or cannot be called at all. This article describes the problems and solutions I encountered when using this class, briefly describe the usage and precautions of PHP mailer.
First, download the phpmailer class library file. Here, you only need one resource point. : Http://download.csdn.net/source/2549515
After downloading the file, put class. phpmailer. php In a directory of your project, and write the following content where you need to send an email:
<? PHP
Require 'class. phpmailer. php ';
Try {
$ Mail = new phpmailer (true );
$ Body = file_get_contents('contents.html '); // the content of the mail is written to the contents.html page.
$ Body = preg_replace ('// \\\/', '', $ body); // strip backslashes
$ Mail-> issmtp (); // tell the class to use SMTP
$ Mail-> smtpauth = true; // enable SMTP authentication
$ Mail-> Port = 25; // set the SMTP server port
$ Mail-> host = "mail.yourdomain.com"; // remote SMTP Server
$ Mail-> username = "yourname@yourdomain.com"; // user name on the remote SMTP Server
$ Mail-> Password = "yourpassword"; // the password of the user on your remote SMTP Server
// $ Mail-> issendmail (); // tell this class to use the Sendmail component. If no Sendmail component is set up, comment out this component; otherwise
"Cocould not execute:/var/Qmail/bin/sendmail"Error prompt
$ Mail-> addreplyto ("yourname@yourdomain.com", "first last ");
$ Mail-> from = "fromname@yourdomain.com ";
$ Mail-> fromname = "first last ";
$ To = "toname@domain.com ";
$ Mail-> addaddress ($ );
$ Mail-> subject = "first phpmailer message ";
$ Mail-> altbody = "to view the message, please use an HTML compatible email viewer! "; // Optional, comment out and test
$ Mail-> wordwrap = 80; // set word wrap
$ Mail-> msghtml ($ body );
$ Mail-> ishtml (true); // send as HTML
$ Mail-> send ();
Echo 'message has been sent .';
} Catch (phpmailerexception $ e ){
Echo $ e-> errormessage ();
}
?>
Note: The above $ mail-> issendmail (); needs to be commented out, otherwise, if there is no Sendmail component, the prompt"Cocould not execute:/var/Qmail/bin/sendmail"Error!