Phpmailer details and usage _ PHP Tutorial

Source: Internet
Author: User
Tags word wrap
Phpmailer details and usage. First, you need to download PHPMailer file package phpmailer.phpmailer.sourceforge.net second, confirm that your server system has supported socket, through phpinfo (); check whether sockets is supported (first, you 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! ";
}
}

Example. http://phpmailer.sourceforge.net/second, check whether your server system supports socket, via phpinfo (); check whether sockets (...

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.