php5.5 Complete steps for sending mail using PHPMailer 5. 2

  • 2021-11-02 00:12:42
  • OfStack

Preface

These days, 1 has been made big by the mail sending function. As a small white, it is inevitable to encounter pits. Today, I finally got phpmailer done. Let's sum up 1

PHPMailer-A full-featured email creation and transfer class for PHP.

In an PHP environment, you can use PHPMailer to create and send messages.

The latest version (20181012) is PHPMailer 6.0. 5, which is not compatible with environments below php 5.5. Since I need to maintain the php 5.3 project, I need to switch to PHPMailer 5.2 to send mail.

Download address: https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2. 24

The following words are not much to say, let's take a look at the detailed introduction

Basic use

After downloading and decompressing. Create a new test demo.


<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->SMTPDebug = 3;        // Enable verbose debug output

$mail->isSMTP();          // Set mailer to use SMTP
$mail->Host = 'smtp.exmail.qq.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true;        // Enable SMTP authentication
$mail->Username = 'xxx@qq.com';     // SMTP username
$mail->Password = 'yourpassword';       // SMTP password
$mail->SMTPSecure = 'ssl';       // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;         // TCP port to connect to

$mail->setFrom('fromWho@qq.com', 'Mailer');
$mail->addAddress('toWhom@qq.com', 'Ryan Miao');  // Add a recipient
$mail->addAddress('ellen@example.com');    // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true);         // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
 echo 'Message could not be sent.';
 echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
 echo 'Message has been sent';
}

Open SMTPDebug to view logs

`0` No output
`1` Commands
`2` Data and commands
`3` As 2 plus connection status
`4` Low-level data output

Error messages are saved in the $mail->ErrorInfo Object.

Save as mail. php, command line execution


php mail.php

You can see the log and the mail was sent successfully.

Summarize


Related articles: