Home  >  Article  >  Backend Development  >  Detailed introduction and usage of phpmailer_PHP tutorial

Detailed introduction and usage of phpmailer_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:13:15997browse

First, you need to download the PHPMailer file package phpmailer. http://phpmailer.sourceforge.net/
Second, confirm that your server system supports sockets, and check whether sockets are supported through phpinfo(); (socket is Belongs to the PHP extension), if it appears as "enabled", it is supported.
Third, unzip the file to your web server directory and call the class.
First include class.phpmailer.php, then create the object, set parameters, and call member functions.

Example 1, make the function easy to call

Copy the code 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->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "yourmail"; // SMTP username Note: Ordinary email authentication does not require @domain name Password = "mailPassword"; // SMTP password
$mail->From = "yourmail@yourdomain.com"; // Sender's email address
$mail->FromName = "Administrator"; / / Sender

$mail->CharSet = "GB2312"; // Specify the character set here!
$mail->Encoding = "base64";
$mail->AddAddress($sendto_email,"username"); // Recipient email and name
$mail->AddReplyTo( "yourmail@yourdomain.com","yourdomain.com");
//$mail->WordWrap = 50; // set word wrap number of newline words
//$mail->AddAttachment("/ var/tmp/file.tar.gz"); // attachment 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( ))                                                                                   🎜> } }
        else {   
            echo "$user_name 邮件发送成功!
";   
        }   
    }   
    // 参数说明(发送到, 邮件主题, 邮件内容, 附加信息, Username)
smtp_mail("yourmail@yourdomain.com", "Welcome to phpmailer!", "NULL", "yourdomain.com", "username");
?>



Note:

1. Email character set setting, $mail->CharSet = "GB2312"; // Specify the character set here! Here I only specify GB2312 because this way Outlook can display the email subject normally. I have tried setting it to utf-8 but it displays garbled characters in Outlook.
2. If you are sending emails in html format, remember to also specify
3. If you want to use it to send mass emails, remember to modify the included file function, such as:
require("phpmailer/class. phpmailer.php");Change to

require_once("phpmailer/class.phpmailer.php");
Otherwise, the class will be redefined.

Personally, I think that to use phpmailer, you first need to have a mail server. PHP’s mail function is not specified. It should be the SMTP set by PHP.

You need to specify it here, and you also need to specify the administrator and password of the mail server.

PHPMailer is also a powerful email class

PHPMailer’s main features:


Supports email s/mime encrypted digital signature
Supports emails with multiple TOs, CCs, BCCs and REPLY-TOs
Can work on any server platform, so you don’t have to worry about WIN platform being unable to send emails
Supports text/HTML format emailsCan embed images
Support for email clients that do not support HTML reading
Powerful debugging function for sending emails
Customized email headers
Redundant SMTP server support
Support 8bit, base64, binary, and quoted-printable encoding
Automatic text wrapping
Support multiple attachment sending function
Support SMTP server verification function
Successfully tested on Sendmail, qmail, Postfix, Gmail, Imail, Exchange and other platforms
Provided The download file includes detailed documentation and examples, so you don’t have to worry about getting started!
PHPMailer is very small, simple, convenient and fast
The above information was translated by Jiucool from the phpmailer official website, please indicate when reprinting!

Usage of PHPMailer (here is using gmail smtp to send emails as an example. Of course, sendmail pop and other other methods are also supported):
First go to http://phpmailer.worxware.com/ to download the latest version of the program. After the package
is downloaded, find the two classes class.phpmailer.php and class.smtp.php and put them in your own directory!
Then create a new php file and name it here: phpmail_jiucool.php
The content of phpmail_jiucool.php is as follows:
I directly write the email sending module as a function postmail_jiucool_com(). You can call this function directly when using it. , the function content is:




Copy code

The code is as follows:


function postmail_jiucool_com($to,$subject = "",$body = ""){
//Author:Jiucool WebSite: http://www.jb51.net
//$ to represents the recipient address $subject represents the email title $body represents the email body
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set("Asia/Shanghai");//setting Time Zone East Eighth District
require_once('class.phpmailer.php');
include("class.smtp.php");
$mail = new PHPMailer(); //new a PHPMailer object comes out
$body = eregi_replace("[]",'',$body); //Perform necessary filtering on the email content
$mail->CharSet ="UTF-8";//Set the email Encoding, the default is ISO-8859-1, if you send Chinese, this must be set, otherwise the code will be garbled
$mail->IsSMTP(); // Set to use SMTP service
$mail->SMTPDebug = 1; // Enable SMTP debugging function
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // Enable SMTP authentication function
$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, such as admin#jiucool.com #Replace with @', 'Sender name');
$mail->AddReplyTo("Email reply address, such as admin# jiucool.com #Replace with @","Name of the person who responded to the email");
$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 = $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, the email was sent successfully! ";
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326520.htmlTechArticleFirst, you need to download the PHPMailer file package phpmailer. http://phpmailer.sourceforge.net/ Second, confirm Your server system already supports sockets, check whether sockets are supported through phpinfo();(...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn