PHPMailer 发送邮件(二)

时间:2022-09-24 17:30:33

  发现PHPMailer又做了较大的更新,以前发送邮件的方法已不太适用,刚好要做一个实验,需要搭建个环境,这里使用Gmail进行测试,现记录下来。

  讲道理这个版本应该是6.0.1,Github传送地址: PHPMailer

  基本要求的内容跟之前的文章是一样的: phpmailer 发送邮件(一)   

一、基本要求

  • Web访问正常(apache可以正常访问)
  • php 设置正确(Sockets Support、OpenSSL support 两项设置为enable)
  • gmail设置, 允许不够安全的应用:设置为已启用

可以写一个简单的语句测试一下:info.php

<?php
phpinfo();
?>

PHPMailer 发送邮件(二)

PHPMailer 发送邮件(二)

二、PHPmailer 

  新版的PHPMailer跟之前的最大不同在于需要使用composer, ubuntu 下composer的安装可参考:ubuntu 安装 php Composer

  安装完成之后,我们需要使用composer来下载PHPMailer。

  我们先切换到web跟目录,创建一个phpmailer的目录,切换到该目录,然后使用命令下载文件:composer require phpmailer/phpmailer, 这个过程会耗费点时间。

lz@starnight:/var/www/html$ pwd
/var/www/html
lz@starnight
:/var/www/html$ ls
hello
.html index.html info.php phpmailer
lz@starnight
:/var/www/html$ cd phpmailer/
lz@starnight
:/var/www/html/phpmailer$ composer require phpmailer/phpmailer

  下载完成之后,我们可以看到目录下多了一些文件,我们创建一个新的文件mailer.php用来发送文件。

lz@starnight:/var/www/html/phpmailer$ pwd
/var/www/html/phpmailer
lz@starnight
:/var/www/html/phpmailer$ ls
composer
.json composer.lock mailer.php vendor
lz@starnight
:/var/www/html/phpmailer$ ls vendor/
autoload
.php composer phpmailer

  mailer.php的内容如下:

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'starnightcyber@gmail.com'; // SMTP username
$mail->Password = 'your password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to

//Recipients

$mail->setFrom('starnightcyber@gmail.com', 'starnightcyber');
$mail->addAddress('starnight_cyber@foxmail.com'); // Name is optional
$mail->addAddress('zl15@foxmail.com');

//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name

//Content

$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Test mail.';
$mail->Body = 'Hello, this is a test mail using phpmailer';
$mail->AltBody = 'Hello, this is a test mail using phpmailer';

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

?>

  访问站点:
PHPMailer 发送邮件(二)

  等待一会(可能会比较慢,取决于你的网络状况),我们可以看到邮件被成功的发送出去了,下面是发送邮件的日志信息和相关截图。

PHPMailer 发送邮件(二)PHPMailer 发送邮件(二)
2017-11-08 03:37:47 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP d74sm5796226pfe.167 - gsmtp
2017-11-08 03:37:47 CLIENT -> SERVER: EHLO 192.168.0.8
2017-11-08 03:37:47 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [210.45.123.80]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-11-08 03:37:47 CLIENT -> SERVER: STARTTLS
2017-11-08 03:37:47 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-11-08 03:37:47 CLIENT -> SERVER: EHLO 192.168.0.8
2017-11-08 03:37:48 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [210.45.123.80]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-11-08 03:37:48 CLIENT -> SERVER: AUTH LOGIN
2017-11-08 03:37:48 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2017-11-08 03:37:48 CLIENT -> SERVER: c3Rhcm5pZ2h0Y3liZXJAZ21haWwuY29t
2017-11-08 03:37:48 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2017-11-08 03:37:48 CLIENT -> SERVER: MTIzQHdheW5lJmx6OTMj
2017-11-08 03:37:49 SERVER -> CLIENT: 235 2.7.0 Accepted
2017-11-08 03:37:49 CLIENT -> SERVER: MAIL FROM:<starnightcyber@gmail.com>
2017-11-08 03:37:49 SERVER -> CLIENT: 250 2.1.0 OK d74sm5796226pfe.167 - gsmtp
2017-11-08 03:37:49 CLIENT -> SERVER: RCPT TO:<starnight_cyber@foxmail.com>
2017-11-08 03:37:49 SERVER -> CLIENT: 250 2.1.5 OK d74sm5796226pfe.167 - gsmtp
2017-11-08 03:37:49 CLIENT -> SERVER: RCPT TO:<zl15@foxmail.com>
2017-11-08 03:37:49 SERVER -> CLIENT: 250 2.1.5 OK d74sm5796226pfe.167 - gsmtp
2017-11-08 03:37:49 CLIENT -> SERVER: DATA
2017-11-08 03:37:50 SERVER -> CLIENT: 354 Go ahead d74sm5796226pfe.167 - gsmtp
2017-11-08 03:37:50 CLIENT -> SERVER: Date: Wed, 8 Nov 2017 11:37:42 +0800
2017-11-08 03:37:50 CLIENT -> SERVER: To: starnight_cyber@foxmail.com, zl15@foxmail.com
2017-11-08 03:37:50 CLIENT -> SERVER: From: starnightcyber <starnightcyber@gmail.com>
2017-11-08 03:37:50 CLIENT -> SERVER: Subject: Test mail.
2017-11-08 03:37:50 CLIENT -> SERVER: Message-ID: <pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI@192.168.0.8>
2017-11-08 03:37:50 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.1 (https://github.com/PHPMailer/PHPMailer)
2017-11-08 03:37:50 CLIENT -> SERVER: MIME-Version: 1.0
2017-11-08 03:37:50 CLIENT -> SERVER: Content-Type: multipart/alternative;
2017-11-08 03:37:50 CLIENT -> SERVER: boundary="b1_pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI"
2017-11-08 03:37:50 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2017-11-08 03:37:50 CLIENT -> SERVER:
2017-11-08 03:37:50 CLIENT -> SERVER: This is a multi-part message in MIME format.
2017-11-08 03:37:50 CLIENT -> SERVER: --b1_pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI
2017-11-08 03:37:50 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2017-11-08 03:37:50 CLIENT -> SERVER:
2017-11-08 03:37:50 CLIENT -> SERVER: Hello, this is a test mail using phpmailer
2017-11-08 03:37:50 CLIENT -> SERVER:
2017-11-08 03:37:50 CLIENT -> SERVER: --b1_pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI
2017-11-08 03:37:50 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2017-11-08 03:37:50 CLIENT -> SERVER:
2017-11-08 03:37:50 CLIENT -> SERVER: Hello, this is a test mail using phpmailer
2017-11-08 03:37:50 CLIENT -> SERVER:
2017-11-08 03:37:50 CLIENT -> SERVER:
2017-11-08 03:37:50 CLIENT -> SERVER: --b1_pN05Ke4mC7wGzpD3bWrXrwz35Nsih2r1VkCMLVlI--
2017-11-08 03:37:50 CLIENT -> SERVER:
2017-11-08 03:37:50 CLIENT -> SERVER: .
2017-11-08 03:37:52 SERVER -> CLIENT: 250 2.0.0 OK 1510112272 d74sm5796226pfe.167 - gsmtp
2017-11-08 03:37:52 CLIENT -> SERVER: QUIT
2017-11-08 03:37:52 SERVER -> CLIENT: 221 2.0.0 closing connection d74sm5796226pfe.167 - gsmtp
Message has been sent
phpmailer send mail log

 

PHPMailer 发送邮件(二)

  可以看到邮件发送成功。

  另外,上面代码中给出的邮箱都是有效的邮箱地址,没有打码,只是为了让各位能更清楚的看到效果,请改成自己的邮箱,不然自己也看不到是否发送成功,别瞎几把给我发邮件^_^。