PHPMailer ThinkPHP realizes automatic mail sending function

  • 2021-10-15 09:59:03
  • OfStack

In this article, we share the specific code of PHPMailer ThinkPHP automatically sending mail for your reference. The specific contents are as follows

1. Download the PHPMailer class package and place it in the Vendor directory of ThinkPHP, which is the third party class library directory of ThinkPHP

2. Then in the project directory under the common folder, in common. PHP (ThinkPHP this file will be automatically referenced, if not, create a new common. php) in the new call to send mail function:


/********************************Email**********************************/

// Definition of postal method 
function s_mail($sendto, $title, $response) {
  // Class for importing function package class.phpmailer.php
  vendor ( "PHPMailer.class#phpmailer" );

  //  Parameter description ( Mailbox address sent to ,  Mail Subject ,  Mail content ,  The name of the recipient )
  // Fill in the mail acceptance address to send to the administrator, please change it to the correct address 
  $sendto_mail = $sendto;
  // Mail Subject 
  $subject = $title;
  // Contents of opinions 
  $body = $response;
  // Send Mail 
  smtp_mail ( $sendto_mail, $subject, $body );

}

// The following definitions 1 Functions for sending mail , Has passed the test. 
//$sendto_email : Mail address 
//$subject : Mail Subject 
//$body: Message body content 
//$sendto_name The name of the recipient and the name of the sender. 1 It can be saved. 
function smtp_mail($sendto_email, $subject = null, $body = null, $sendto_name = null) {
  // New 1 Mail sending class objects 
  $mail = new PHPMailer ();
  // send via SMTP
  $mail->IsSMTP ();
  // SMTP  Mail server address, which needs to be replaced with the mail server address of the mailbox where the mail is sent 
  $mail->Host = "smtp.qq.com";
  // Mail server verification open 
  $mail->SMTPAuth = true;
  // SMTP The user name of this mailbox on the server, and some only need @ Some of the previous parts need full names. Please replace it with the correct mailbox user name 
  $mail->Username = "xxxx@qq.com";
  // SMTP The password of this mailbox on the server, please replace it with the correct password 
  $mail->Password = "xxxx";
  // SMTP The mailbox on the server to send this message, please replace it with the correct mailbox  , And $mail->Username  Is the corresponding value of. 
  $mail->From = "xxxx@qq.com";
  //  The name of the real sender and other information, which can be filled in here as required 
  $mail->FromName = "[".date('Y-m-d H:i:s',time ())."] Demand system mail ";
  //  Specify the character set here! 
  $mail->CharSet = "utf-8";
  $mail->Encoding = base64;
  //  Recipient mailbox and name 
  $mail->AddAddress ( $sendto_email, $sendto_name );
  // This 1 Items are set according to needs 
  $mail->AddReplyTo ( 'xxxx@qq.com', "admin" );
  // set word wrap
  //$mail->WordWrap = 50;
  //  Attachment processing 
  //$mail->AddAttachment("/var/tmp/file.tar.gz");
  //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
  //  Send  HTML Mail 
  $mail->IsHTML ( false );
  //  Mail Subject 
  $mail->Subject = $subject;
  //  Mail content 
  $mail->Body = $body;
  $mail->AltBody = "text/html";

  if (! $mail->Send ()) {
    return 0;
  } else {
    return 1;
  }
}

Related articles: