Detailed introduction and application of Phpmailer _php skills

Source: Internet
Author: User
Tags base64 format message set time word wrap

First, you need to download the Phpmailer file package Phpmailer. http://phpmailer.sourceforge.net/
Second, verify that your server system has the support socket, through phpinfo (), to see if the support sockets (the socket is part of the PHP extension), if it appears as "enabled", that is supported.
Third, unzip the file to your Web server directory and invoke the class.
First contains the class.phpmailer.php, then creates the object, sets the parameters, and invokes the member function.

Example 1, making functions easy to call

Copy Code code as follows:

<?php
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: Normal mail authentication does not require @ domain name
$mail->password = "MailPassword"; SMTP Password
$mail->from = "yourmail@yourdomain.com"; Sender's mailbox
$mail->fromname = "Administrator"; Sender

$mail->charset = "GB2312"; This specifies the character set!
$mail->encoding = "base64";
$mail->addaddress ($sendto _email, "username"); Recipient mailboxes and Names
$mail->addreplyto ("yourmail@yourdomain.com", "yourdomain.com");
$mail->wordwrap = 50; Set word wrap line-wrapping words
$mail->addattachment ("/var/tmp/file.tar.gz"); Attachment Accessories
$mail->addattachment ("/tmp/image.jpg", "new.jpg");
$mail->ishtml (TRUE); Send As HTML
Message subject
$mail->subject = $subject;
Message content
$mail->body = "
<meta http-equiv= "Content-language" content= "ZH-CN" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
I love PHP.
</body>
";
$mail->altbody = "text/html";
if (! $mail->send ())
{
echo "message sent incorrectly <p>";
echo "Message error message:". $mail->errorinfo;
Exit
}
else {
echo "$user _name Mail sent successfully!<br/>";
}
}
Parameter description (send to, mail subject, message content, additional information, user name)
Smtp_mail ("yourmail@yourdomain.com", "Welcome to use phpmailer! "," NULL "," yourdomain.com "," username ");
?>

Attention:

1. The character set of the message, $mail->charset = "GB2312"; This specifies the character set! I only specify GB2312 here because Outlook displays the subject of the message correctly, I tried to set it to utf-8 but it appears garbled in Outlook.
2. If you are sending a message in HTML format, then remember to also specify
3. If you want to use it for mass mailing, remember to modify the Include file function, such as:
Require ("phpmailer/class.phpmailer.php");
To
Require_once ("phpmailer/class.phpmailer.php");
Otherwise, a redefinition of the class is generated.

Personally think to use Phpmailer, first of all, need to have a mail server, PHP mail function is not specified, should be used in PHP settings smtp.

and you need to specify it here, and you need to specify the administrator and password for the mail server.

Phpmailer is also a powerful message class

Phpmailer features:

Digitally signing support for message S/MIME encryption
Support multiple TOs for mail CCs, BCCs and Reply-tos
can work on any server platform, so don't worry about the win platform.
Support Text/html format message
can embed image Image
Support for message clients that do not support HTML reading
powerful send-mail debugging features debug
Custom mail header
Redundant SMTP server support
Support 8bit, base64, Binary, and Quoted-printable encoding
Text wrapping
Support for multiple attachment forwarding
Support SMTP server Authentication
in SendMail, QMail, Postfix, Gmail, Imail, Exchange platforms Try a successful
The download file provided includes a detailed description of the document and an example description, so don't worry about the difficult problem!
Phpmailer is very small, simple, convenient, fast
The above information by Jiucool translation from Phpmailer official website, reproduced please specify! Use of the

Phpmailer (for example, use Gmail SMTP to send mail, and of course support SendMail POPs):
First to http://phpmailer.worxware.com/ Download the latest version of the package
when the download is complete, find class.phpmailer.php, class.smtp.php two classes to put in their own directory!
then create a new PHP file here named: phpmail_jiucool.php
phpmail_jiucool.php The contents are as follows:
I write the email module directly as a function postmail_jiucool_com () , you can call the function directly when you use it, the function content is:

Copy Code code as follows:

function postmail_jiucool_com ($to, $subject = "", $body = "") {
Author:jiucool website:http://www.jb51.net
$to represents the recipient address $subject represents the message header $body the message body
Error_reporting (E_all);
Error_reporting (e_strict);
Date_default_timezone_set ("Asia/shanghai");/set time zone East eight area
Require_once (' class.phpmailer.php ');
Include ("class.smtp.php");
$mail = new Phpmailer (); New a Phpmailer object out
$body = eregi_replace ("[\]", ", $body); Filter the content of the message as necessary
$mail->charset = "UTF-8";//Set message encoding, default iso-8859-1, if you have to set this item in Chinese, otherwise garbled
$mail->issmtp (); Set up to use the SMTP service
$mail->smtpdebug = 1; To enable the SMTP debugging feature
1 = errors and messages
2 = messages Only
$mail->smtpauth = true; To enable the SMTP authentication feature
$mail->smtpsecure = "SSL"; Security protocols
$mail->host = "smtp.googlemail.com"; SMTP Server
$mail->port = 465; Port number of the SMTP server
$mail->username = "SMTP Server user name"; SMTP Server user name
$mail->password = "SMTP server password"; SMTP server password
$mail->setfrom (' sender address, such as admin#jiucool.com #换成 @ ', ' Name of Sender ');
$mail->addreplyto ("Mail reply address, such as admin#jiucool.com #换成 @", "Mail reply person's name");
$mail->subject = $subject;
$mail->altbody = "To view", compatible email viewer! with an HTML -From www.jiucool.com "; Optional, comment out and test
$mail->msghtml ($body);
$address = $to;
$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, Mail sent success!" ";
}
}

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.