<?php
namespace PHPMailer\PHPMailer;
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
require 'PHPMailer-master/src/Exception.php';
//包下載地址https://github.com/PHPMailer/PHPMailer
//參數(shù)
$subject = '測(cè)試附件發(fā)送';
$content = '郵件內(nèi)容';
$attachments = 'test.zip';//附件壓縮后的地址
/*
//壓縮文件 https://www.php.cn/faq/556515.html
$attachments_file_url = '楓葉_1.jpg';
//打包附件
$zip = new \ZipArchive();
if ($zip->open($attachments, \ZipArchive::CREATE) === TRUE) {
// 添加文件到壓縮包
$zip->addFile($attachments_file_url);
// 關(guān)閉壓縮包
$zip->close();
}
*/
//壓縮文件夾 https://www.php.cn/faq/582443.html
if(! file_exists($attachments)){
// 修改ZIP文件名編碼
$attachments = iconv('UTF-8', 'GBK', $attachments);
$zip = new \ZipArchive();
if ($zip->open($attachments, \ZipArchive::CREATE) === TRUE) {
$zip->addGlob('測(cè)試的附件/*');//將文件夾內(nèi)的文件寫入(壓縮)
$zip->close();
}
}
$mail = new PHPMailer();
$mail->IsSMTP(); // 啟用SMTP
$mail->Host="smtp.qq.com"; //smtp服務(wù)器的名稱(這里以QQ郵箱為例)
$mail->SMTPSecure = "ssl"; //目前規(guī)定必須使用ssl,非ssl的協(xié)議已經(jīng)不支持了
$mail-> Port = 465; //端口號(hào)
#$mail->SMTPDebug = 2; //用于debug PHPMailer信息 : 會(huì)顯示執(zhí)行信息(包含錯(cuò)誤信息)
$mail->SMTPAuth = true; //啟用smtp認(rèn)證
$mail->Username = "59673467@qq.com"; //你的郵箱名
$mail->Password = "*******" ; //郵箱授權(quán)碼,注意是授權(quán)碼,不是登錄密碼 指南:https://jingyan.baidu.com/article/4f34706e0a35a9a286b56d46.html
$mail->From = "59673467@qq.com"; //發(fā)件人地址(也就是你的郵箱地址)
$mail->FromName = "service"; //發(fā)件人姓名
$mail->AddAddress('592291622@qq.com'); //收件人地址
$mail->WordWrap = 50; //設(shè)置每行字符長(zhǎng)度
$mail->IsHTML(true); // 是否HTML格式郵件
$mail->CharSet="utf-8"; //設(shè)置郵件編碼
$mail->AddAttachment($attachments);// 發(fā)送附件
$mail->Subject = $subject; //郵件主題
$mail->Body = $content; //郵件內(nèi)容
if ($mail->send()) {
echo '郵件發(fā)送成功!';
} else {
echo '郵件發(fā)送失敗!';
}
我的目錄結(jié)構(gòu)

image.png