PHPMailer & 透過Gmail發信

PHPMailer 是一套功能強大的的Mail套件
詳細資料可查看PHPMailer GitHub: https://github.com/PHPMailer/PHPMailer

下載整包程式碼
wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
解壓縮
unzip master.zip

而直接複製GitHub的Sample Code最常見的錯誤就是找不到 autoload.php

Warning: require(vendor/autoload.php): failed to open stream: No such file or directory 
Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/share/php') 

因為GitHub裡的Sample code是走Composer路線的,Composer是PHP管理套件的工具
而 autoload.php 要跑過 Composer 才會出現
Composer & autoload.php 的關係,後續有機會再另開文章解說
會出錯多半是沒 Composer 直接拿 Composer 的語法下去用,所以 GitHub官網有這段說明

Alternatively, if you're not using Composer, copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration and load each class file manually:
<?php 
    use PHPMailer\PHPMailer\PHPMailer; 
    use PHPMailer\PHPMailer\Exception; 

    require 'path/to/PHPMailer/src/Exception.php'; 
    require 'path/to/PHPMailer/src/PHPMailer.php'; 
    require 'path/to/PHPMailer/src/SMTP.php';
If you're not using the SMTP class explicitly (you're probably not), you don't need a use line for the SMTP class.

所以這邊修改一下不用 Composer 做法就可以直上了
如果不是用Google的SMTP,也可以視情況修改參數

<?php
//移除Composer語法

// 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\SMTP;
 //use PHPMailer\PHPMailer\Exception;
 // Load Composer's autoloader
 //require 'vendor/autoload.php';

//新增
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//設定檔案路徑
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

//建立物件                                                                
$mail = new PHPMailer(true);

try {
    //Server settings
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER;  // Enable verbose debug output
    $mail->SMTPDebug = 0; // DEBUG訊息
    $mail->isSMTP(); // 使用SMTP
    $mail->Host = 'smtp.gmail.com'; // SMTP server 位址
    $mail->SMTPAuth = true;  // 開啟SMTP驗證
    $mail->Username = 'Gmail帳號'; // SMTP 帳號
    $mail->Password = 'Gmail密碼(*註1)'; // SMTP 密碼
    //$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
    $mail->SMTPSecure = "ssl"; // Gmail要透過SSL連線
    $mail->Port       = 465; // SMTP TCP port 

    //設定收件人資料
    $mail->setFrom('from@example.com', 'Mailer'); // 寄件人(透過Gmail發送會顯示Gmail帳號為寄件者)
    $mail->addAddress('apple@example.com', 'Apple User'); // 收件人會顯示 Apple User<apple@example.com>(*註2)
    // $mail->addAddress('banana@example.com'); // 名字非必填
    $mail->addReplyTo('info@example.com', 'Information'); //回信的收件人
    $mail->addCC('cc@example.com'); //副本
    $mail->addBCC('bcc@example.com'); //密件副本

    // 附件
    $mail->addAttachment('/var/tmp/file.tar.gz'); // 附件 (*註3) 
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // 插入附件可更改檔名

    // 信件內容
    $mail->isHTML(true); // 設定為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'; // 對方若不支援HTML的信件內容

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
*註1
若要讓透過Gmail發信,Gmail帳號也需要設定
如果未開啟兩步驟驗證,需要開啟低安全性應用程式存取權:
https://support.google.com/accounts/answer/6010255?hl=zh-Hant
若已經開啟兩步驟驗證,則要設定應用程式密碼(建議):
https://support.google.com/accounts/answer/185833?hl=zh-Hant
如果有使用G Suite,還可以使用relay(有限發送數量)
https://support.google.com/a/answer/2956491?hl=zh-Hant
*註2
若要發送給多人:
$mailto = array(
             'user1@example.com' => 'user1',
             'user2@example.com' => 'user2',
             'user3@example.com' => 'user3',
);
if(is_array($mailto)){
    foreach ($mailto as $email => $name) {
        $mail->AddAddress("$email","$name");
    }
}else{
    $mail->AddAddress("$mailto");
}
另外的做法就是直接疊上去
 $mail->addAddress('user1@example.com', 'user1'); 
 $mail->addAddress('user2@example.com', 'user2'); 
 $mail->addAddress('user3@example.com', 'user3'); 
*註3
附件如果要插入多個檔案:
$files = array($file1,$file2,$file3,$file4,$file5);
$mail->AddAttachment("$files");