發現PHPMailer又作了較大的更新,之前發送郵件的方法已不太適用,恰好要作一個實驗,須要搭建個環境,這裏使用Gmail進行測試,現記錄下來。php
講道理這個版本應該是6.0.1,Github傳送地址: PHPMailerhtml
基本要求的內容跟以前的文章是同樣的: phpmailer 發送郵件(一) git
1、基本要求github
能夠寫一個簡單的語句測試一下:info.phpweb
<?php phpinfo(); ?>
2、PHPmailer apache
新版的PHPMailer跟以前的最大不一樣在於須要使用composer, ubuntu 下composer的安裝可參考:ubuntu 安裝 php Composerjson
安裝完成以後,咱們須要使用composer來下載PHPMailer。ubuntu
咱們先切換到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; } ?>
訪問站點,點擊執行mailer.php:
等待一會(可能會比較慢,取決於你的網絡情況),咱們能夠看到郵件被成功的發送出去了,下面是發送郵件的日誌信息和相關截圖。
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
能夠看到郵件發送成功。
另外,上面代碼中給出的郵箱都是有效的郵箱地址,沒有打碼,只是爲了讓各位能更清楚的看到效果,請改爲本身的郵箱,否則本身也看不到是否發送成功,別瞎幾把給我發郵件^_^。