首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

ECMall/ECShop 系統整合 PHPMailer 6.x 實現 SMTP SSL/TLS 加密郵件發送(精簡版)

     編輯:PHP 相關知識

核心改造四步走

1. 升級 PHPMailer 庫

composer require phpmailer/phpmailer
# 或手動覆蓋 lib/phpmailer/ 目錄為 6.x 版本

2. 擴展 Mailer 基類構造函數(傳遞 SMTPSecure)

// lib/mail.lib.php & lib/mail_queue.lib.php
class Mailer {
  function __construct($from, $email, $protocol, $host='', $port='', $user='', $pass='', $SMTPSecure=false) {
    $this->Mailer($from, $email, $protocol, $host, $port, $user, $pass, $SMTPSecure);
  }
  function Mailer($from, $email, $protocol, $host='', $port='', $user='', $pass='', $SMTPSecure=false) {
    // ... 原有代碼 ...
    $this->mail->SMTPSecure = $SMTPSecure; // 'ssl' | 'tls' | false
    if ($SMTPSecure === 'ssl' && !$port) $this->mail->Port = 465;
    elseif ($SMTPSecure === 'tls' && !$port) $this->mail->Port = 587;
  }
}

3. 入口函數透傳配置

// lib/global.lib.php function get_mailer()
$secure = Conf::get('email_ssl'); // 讀取後台配置
$mailer = new Mailer($sender, $from, $protocol, $host, $port, $username, $password, $secure);

4. 後台界面新增加密選項

// templates/setting.email_setting.html

  郵件服務器加密方式:
  {html_radios name="email_ssl" options=$email_ssl checked=$setting.email_ssl}
  


// app/setting.app.php
$this->assign('email_ssl', [0=>'不加密', 1=>'SSL', 2=>'TLS']);
// POST 保存 $data['email_ssl'] = $_POST['email_ssl'];
// 測試發送同步傳遞 $email_ssl 參數

常用郵件服務商配置速查

服務商SMTP加密端口認證
QQ/騰訊企業郵smtp.qq.com / smtp.exmail.qq.comSSL465授權碼
Gmailsmtp.gmail.comTLS587應用專用密碼
Outlook/365smtp.office365.comTLS587應用專用密碼
163/126smtp.163.comSSL465/994客戶端授權碼
阿里雲企業郵smtp.mxhichina.comSSL465授權碼

驗證清單

  1. PHP extension_loaded('openssl') 為 true。
  2. 服務器防火牆放行 465/587 出站。
  3. 後台正確填寫:加密方式、端口、授權碼(非登入密碼)。
  4. 點擊「測試發送」收到郵件即成功。

總結

PHPMailer 6.x 原生支援 SMTPSecure 屬性,ECMall/ECShop 只需「升級庫 → 擴展構造函數 → 透傳配置 → 界面配置」四步即可支援現代 SMTP SSL/TLS。核心邏輯零修改,僅在參數傳遞鏈路補齊 $SMTPSecure,風險極低、兼容性強。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved