Detailed introduction and usage of phpmailer

  • 2020-05-27 04:37:43
  • OfStack

First, you need to download PHPMailer package phpmailer. http:. / / phpmailer sourceforge. net /
Second, make sure your server system already supports socket, via phpinfo(); Check to see if sockets is supported (socket is part of the PHP extension), and 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 the parameters, and call the member function.

Example 1, make it a function to call easily


<?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: regular mail authentication is not required  @ The domain name     
        $mail->Password = "mailPassword"; // SMTP password    
        $mail->From = "yourmail@yourdomain.com";      //  Sender email     
        $mail->FromName =  " The administrator ";  //  The sender     

        $mail->CharSet = "GB2312";   //  Specify character set 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  Line feed -     
        //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment  The attachment     
        //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    
        $mail->IsHTML(true);  // send as HTML    
        //  Email subject     
        $mail->Subject = $subject;    
        //  Email content     
        $mail->Body = "   
    <html><head>   
    <meta http-equiv="Content-Language" content="zh-cn">   
    <meta http-equiv="Content-Type" content="text/html; charset=GB2312">   
    </head>   
    <body>   
    I love php .    
    </body>   
    </html>   
    ";                                                                          
        $mail->AltBody ="text/html";    
        if(!$mail->Send())    
        {    
            echo " Email sent incorrectly  <p>";    
            echo " Email error message : " . $mail->ErrorInfo;    
            exit;    
        }    
        else {    
            echo "$user_name  Email sent successfully !<br />";    
        }    
    }    
    //  Parameters that ( Sent to the ,  Email subject ,  Email content ,  Additional information ,  The user name )    
    smtp_mail("yourmail@yourdomain.com", " Welcome to use phpmailer ! ", "NULL", "yourdomain.com", "username");    
    ?> 

Note:

1. Character set for message, $mail- > CharSet = "GB2312"; // the character set is specified here! I'm just going to specify GB2312 here because that's how Outlook normally displays the subject line. I've tried to set it to utf-8 but it shows scrambled code under Outlook.
2. If you are sending an html email, specify it as well
3. If you want to use it for group email, remember to modify the include file function, such as:
require("phpmailer/class.phpmailer.php");
Instead of
require_once("phpmailer/class.phpmailer.php");
Otherwise a class redefinition is generated.

In my opinion, to use phpmailer, first of all, you need to have a mail server. The mail function of PHP is not specified, but SMTP set by PHP should be used.

In this case, you need to specify the administrator and password of the mail server.

PHPMailer is also a powerful mail class

Main features of PHPMailer:

Support email s/mime encrypted digital signature
Support multiple email TOs, CCs, BCCs and REPLY-TOs
It works on any server platform, so you don't have to worry about the WIN platform not being able to send mail
Supports text /HTML mail
You can embed an image image
Support for email clients that do not support HTML reading
Powerful send mail debugging function debug
Custom mail header
Redundant SMTP server support
Support 8bit, base64, binary, and quoted-printable encoder
Word wrap
Support multiple attachment sending function
Support for SMTP server validation
Successfully tested on Sendmail, qmail, Postfix, Gmail, Imail, Exchange and other platforms
The download file provided includes detailed documentation and sample instructions, so don't worry about getting started!
PHPMailer is very small, simple, convenient and fast
The above information is translated from phpmailer website by Jiucool, please indicate!

The use of PHPMailer (for example, gmail smtp is used to send emails, but sendmail pop is also supported) :
First of all to http: / / phpmailer. worxware. com/download the latest version of the package
Once the download is complete, go to class.phpmailer.php and class.smtp.php and put them in your own directory!
And then create a new php file here: phpmail_jiucool.php
phpmail_jiucool.php reads as follows:
I directly wrote the email sending module as a function postmail_jiucool_com(), which can be directly called when you use it. The function content is:

 
function postmail_jiucool_com($to,$subject = "",$body = ""){ 
//Author:Jiucool WebSite: https://www.ofstack.com 
//$to  Represents the recipient address  $subject  Represents the subject line  $body Represents the body of the message  
//error_reporting(E_ALL); 
error_reporting(E_STRICT); 
date_default_timezone_set("Asia/Shanghai");// Set time zone east 8 area  
require_once('class.phpmailer.php'); 
include("class.smtp.php"); 
$mail = new PHPMailer(); //new1 a PHPMailer Object out  
$body = eregi_replace("[\]",'',$body); // Filter the content as necessary  
$mail->CharSet ="UTF-8";// Set the email encoding, by default ISO-8859-1 , if you send Chinese this must be set, otherwise garbled  
$mail->IsSMTP(); //  Set to use SMTP service  
$mail->SMTPDebug = 1; //  To enable the SMTP debugging  
// 1 = errors and messages 
// 2 = messages only 
$mail->SMTPAuth = true; //  To enable the  SMTP  Validation functions  
$mail->SMTPSecure = "ssl"; //  Security protocols  
$mail->Host = "smtp.googlemail.com"; // SMTP  The server  
$mail->Port = 465; // SMTP The port number of the server  
$mail->Username = "SMTP Server username "; // SMTP Server username  
$mail->Password = "SMTP Server password "; // SMTP Server password  
$mail->SetFrom(' Sender address, e.g admin#jiucool.com # Switch to @', ' Sender name '); 
$mail->AddReplyTo(" Email reply address , Such as admin#jiucool.com # Switch to @"," The name of the person who replied 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! "; 
} 
} 


Related articles: