Phpmailer usage, a friend may wish to refer to the first, need to download PHPMailer file package phpmailer. http://phpmailer.sourceforge.net/
Second, check whether your server system supports socket. Use phpinfo () to check whether sockets is supported (socket is part of PHP Extension). If it appears as "enabled ", it is supported.
Third, extract the file to your web server directory and call the class.
First, class. phpmailer. php is included. Then, an object is created, parameters are set, and member functions are called.
Example 1: Make the function easy to call
The Code is as follows:
Require ("phpmailer/class. phpmailer. php ");
Function smtp_mail ($ sendto_email, $ subject, $ body, $ extra_hdrs, $ user_name ){
$ Mail = new PHPMailer ();
$ Mail-> IsSMTP (); // send via SMTP
$ Mail-> Host = "200.162.244.66"; // SMTP servers
$ Mail-> SMTPAuth = true; // turn on SMTP authentication
$ Mail-> Username = "yourmail"; // SMTP username Note: @ domain name is not required for normal mail authentication.
$ Mail-> Password = "mailPassword"; // SMTP password
$ Mail-> From = "yourmail@yourdomain.com"; // sender's mailbox
$ Mail-> FromName = "Administrator"; // sender
$ Mail-> CharSet = "GB2312"; // the character set is specified here!
$ Mail-> Encoding = "base64 ";
$ Mail-> AddAddress ($ sendto_email, "username"); // recipient's email address and name
$ Mail-> AddReplyTo ("yourmail@yourdomain.com", "yourdomain.com ");
// $ Mail-> WordWrap = 50; // set word wrap
// $ Mail-> AddAttachment ("/var/tmp/file.tar.gz"); // attachment
// $ Mail-> AddAttachment ("/tmp/image.jpg", "new.jpg ");
$ Mail-> IsHTML (true); // send as HTML
// Email Subject
$ Mail-> Subject = $ subject;
// Email content
$ Mail-> Body ="
I love php.
";
$ Mail-> AltBody = "text/html ";
If (! $ Mail-> Send ())
{
Echo "email sending Error
";
Echo "email error message:". $ mail-> ErrorInfo;
Exit;
}
Else {
Echo "$ user_name email sent successfully!
";
}
}
// Parameter description (sent to, subject, content, additional information, user name)
Smtp_mail ("yourmail@yourdomain.com", "Welcome to phpmailer! "," NULL "," yourdomain.com "," username ");
?>
Note:
1. Set the character set of the email, $ mail-> CharSet = "GB2312"; // specify the character set here! Here I only specify it as GB2312 because Outlook can display the mail subject normally, I tried to set it to UTF-8 but garbled characters are displayed in Outlook.
2. If you want to send an html-format email, remember to specify
3. If you want to use it for mass mailing, remember to modify the include file function, such:
Require ("phpmailer/class. phpmailer. php ");
Change
Require_once ("phpmailer/class. phpmailer. php ");
Otherwise, the class will be redefined.
I personally think that phpmailer should be used. First, a mail server is required. The mail function of PHP is not specified. It should be the SMTP set by PHP.
You must specify the email server administrator and password.
PHPMailer is also a powerful mail class
Main features of PHPMailer:
Supports mail s/mime-encrypted digital signatures
Supports email to multiple TOs, CCs, BCCs and REPLY-TOs
Can work on any server platform, so you don't have to worry about the problem that Windows cannot send emails.
Supports text/HTML emails
Image embedding
The email client does not support HTML reading.
Powerful debugging for sending emails
Custom Email header
Support for redundant SMTP servers
Supports 8-bit, base64, binary, and quoted-printable encoding.
Text wrap
Supports sending multiple attachments
Support for SMTP server Verification
Tests on Sendmail, qmail, Postfix, Gmail, Imail, and Exchange platforms are successful.
The downloaded files include detailed instructions and examples, so you don't have to worry about getting started!
PHPMailer is very small, simple, convenient, and fast
The above materials are translated by Jiucool from the phpmailer official website. For more information, see!
PHPMailer usage (Here we use gmail smtp to send emails as an example. Of course, other methods such as sendmail pop are also supported ):
First go to http://phpmailer.worxware.com/download the latest version of the package
After the download is complete, find the class. phpmailer. php and class. smtp. php classes in your own directory!
Create a new PHP file named phpmail_jiucool.php.
Phpmail_jiucool.php contains the following content:
I directly write the mail sending module as a function postmail_jiucool_com (). You can directly call this function when using it. The function content is:
The Code is as follows:
Function postmail_jiucool_com ($ to, $ subject = "", $ body = ""){
// Author: Jiucool WebSite: http://www.jb51.net
// $ To indicates the recipient address $ subject indicates the mail title $ body indicates the mail body
// Error_reporting (E_ALL );
Error_reporting (E_STRICT );
Date_default_timezone_set ("Asia/Shanghai"); // set the time zone UTC + 8.
Require_once ('class. phpmailer. php ');
Include ("class. smtp. php ");
$ Mail = new PHPMailer (); // a new PHPMailer object is displayed.
$ Body = eregi_replace ("[\]", '', $ body); // filter the email content.
$ Mail-> CharSet = "UTF-8"; // set the mail encoding, the default ISO-8859-1, this must be set if you send Chinese, otherwise garbled
$ Mail-> IsSMTP (); // you can specify the SMTP service.
$ Mail-> SMTPDebug = 1; // enable the SMTP debugging Function
// 1 = errors and messages
// 2 = messages only
$ Mail-> SMTPAuth = true; // enable SMTP Verification
$ Mail-> SMTPSecure = "ssl"; // Security Protocol
$ Mail-> Host = "smtp.googlemail.com"; // SMTP Server
$ Mail-> Port = 465; // SMTP server Port number
$ Mail-> Username = "SMTP Server Username"; // SMTP Server Username
$ Mail-> Password = "SMTP server Password"; // SMTP server Password
$ Mail-> SetFrom ('sender address, for example, admin # jiucool.com # Replace with @ ', 'sender name ');
$ Mail-> AddReplyTo ("email reply address, such as admin # jiucool.com # changed to @", "email reply Recipient Name ");
$ Mail-> Subject = $ subject;
$ Mail-> AltBody = "To view the message, please use an HTML compatible email viewer! -From www.jiucool.com "; // optional, comment out and test
$ Mail-> MsgHTML ($ body );
$ Address = $;
$ Mail-> AddAddress ($ address, "Recipient Name ");
// $ Mail-> AddAttachment ("images/phpmailer.gif"); // attachment
// $ Mail-> AddAttachment ("images/phpmailer_mini.gif"); // attachment
If (! $ Mail-> Send ()){
Echo "Mailer Error:". $ mail-> ErrorInfo;
} Else {
Echo "Message sent! Congratulations, the email is sent successfully! ";
}
}