Detailed explanation of PHP mail sending class PHPMailer usage example

  • 2021-07-18 07:29:39
  • OfStack

This article illustrates the use of PHP mail sending class PHPMailer, and describes its specific operation steps in detail. Share it for your reference. The specific steps are as follows:

1. Install sendmail on the server


sudo apt-get install sendmail 

2. Start sendmail


sudo /etc/init.d/sendmail start 

3. Amendment of php. ini


[mail function] 
SMTP = localhost 
smtp_port = 25 
sendmail_from = me@example.com 

4. The Function sendMail function is as follows


<?php 
/*  Call PHPMailer Send an email  
* @param String $receiver    Recipient  
* @param String $sender     Sender  
* @param String $sender_name  If the sender name is blank, replace it with the sender address  
* @param String $subject    Mail Subject  
* @param String $content    Mail content  
* @param boolean $ishtml     Whether or not html E-mail  
* @param Array  $attachements  Annex  
* @return boolean 
*/ 
function sendMail($receiver, $sender, $sender_name, $subject, $content, $ishtml=true, $attachments=array()) { 
  include_once "class-phpmailer.php";  
 
  if(empty($receiver) || empty($sender) || empty($subject) || empty($content)){ 
    return false; 
  } 
   
  $mail = new PHPMailer();  
 
  //$mail->IsSMTP();        //  Jing smtp Send   
  //$mail->Host = "smtp.gmail.com"; // SMTP  Server  
  //$mail->Port = 465;       // SMTP  Port  
  //$mail->SMTPSecure = 'ssl';   //  Encryption mode  
  //$mail->SMTPAuth = true;     //  Open SMTP Certification  
  //$mail->Username = "username";  //  User name  
  //$mail->Password = "password";  //  Password  
 
  $mail->IsMail();         // using PHP mail() function  It is possible that the seal may not be prompted by the following users  
       
  $mail->From = $sender;      //  Sender   
  $mail->FromName = $sender_name;  //  Sender alias   
  $mail->AddReplyTo($sender);    //  Respondent  
  $mail->AddAddress($receiver);   //  Addressee   
 
  //  With html Mode sending  
  if($ishtml){ 
    $mail->IsHTML(true); 
  } 
 
  //  Send attachments  
  if($attachments){ 
    if(is_array($attachments)){ 
      $send_attachments = array(); 
 
      $tmp_attachments = array_slice($attachments,0,1); 
      if(!is_array(array_pop($tmp_attachments))){ 
        if(isset($attachments['path'])){ 
          array_push($send_attachments, $attachments);           
        }else{ 
          foreach($attachments as $attachment){ 
            array_push($send_attachments, array('path'=>$attachment)); 
          } 
        } 
      }else{ 
        $send_attachments = $attachments; 
      } 
 
      foreach($send_attachments as $attachment){ 
        $attachment['name'] = isset($attachment['name'])? $attachment['name'] : null; 
        $attachment['encoding'] = isset($attachment['encoding'])? $attachment['encoding'] : 'base64'; 
        $attachment['type'] = isset($attachment['type'])? $attachment['type'] : 'application/octet-stream'; 
        if(isset($attachment['path']) && file_exists($attachment['path'])){ 
          $mail->AddAttachment($attachment['path'],$attachment['name'],$attachment['encoding'],$attachment['type']); 
        } 
      } 
    }elseif(is_string($attachments)){ 
      if(file_exists($attachments)){ 
        $mail->AddAttachment($attachments); 
      } 
    } 
  } 
 
  $mail->Subject = $subject; //  Message header  
  $mail->Body   = $content; //  Mail capacity  
  return $mail->Send();  
} 
 
// DEMO Examples are as follows:  
$receiver = 'receiver@test.com'; 
$sender = 'sender@test.com'; 
$sender_name = 'sender name'; 
$subject = 'subjecct'; 
$content = 'content'; 
 
// 4 All kinds of formats can be used  
$attachments = 'attachment1.jpg'; 
$attachments = array('path'=>'attachment1.jpg', 'name'=>' Annex 1.jpg'); 
$attachments = array('attachment1.jpg','attachment2.jpg','attachment3.jpg'); 
$attachments = array( 
  array('path'=>'attachment1.jpg', 'name'=>' Annex 1.jpg'), 
  array('path'=>'attachment2.jpg', 'name'=>' Annex 2.jpg'), 
  array('path'=>'attachment3.jpg', 'name'=>' Annex 3.jpg'), 
); 
$flag = sendMail($receiver, $sender, $sender_name, $subject, $content, true, $attachments); 
echo $flag; 
?> 

Click here to download the source code.

I hope this article is helpful to everyone's study of PHP programming.


Related articles: