適用於 Windows Server 安裝 Jmail 服務的環境。
<%
Dim JMail
Set JMail = Server.CreateObject("JMail.Message")
JMail.Charset = "gb2312"
JMail.From = "webjxcom@163.com"
JMail.FromName = "網站管理員"
JMail.Subject = "郵件主題"
JMail.MailServerUserName = "webjx"
JMail.MailServerPassword = "webjx"
JMail.Priority = 1
JMail.AddHeader "Originating-IP", Request.ServerVariables("REMOTE_ADDR")
JMail.AddRecipient("5ss@163.com")
JMail.HTMLBody = "<html>...</html>"
JMail.Body = "純文字備用內容"
JMail.Send("smtp.163.com")
JMail.Close()
Set JMail = Nothing
%>
無需外部擴充,純 PHP 實現 SMTP 協議,支援驗證、HTML/純文、附件、抄送/密送。
<?php
class SmtpMailer {
private $host, $port, $user, $pass, $from, $fromName;
private $socket, $timeout = 30;
public function __construct($host, $port=25, $user, $pass, $from, $fromName) {
$this->host = $host; $this->port = $port;
$this->user = $user; $this->pass = $pass;
$this->from = $from; $this->fromName = $fromName;
}
public function send($to, $subject, $htmlBody, $textBody='', $cc='', $bcc='', $attachments=array()) {
$this->connect();
$this->auth();
$this->mailFrom();
$this->rcptTo(array_merge(explode(',', $to), explode(',', $cc), explode(',', $bcc)));
$this->data($this->buildMessage($to, $subject, $htmlBody, $textBody, $cc, $bcc, $attachments));
$this->quit();
$this->disconnect();
}
// 內部方法:connect、auth(LOGIN)、mailFrom、rcptTo、data、buildMessage (MIME 組裝)、quit、disconnect
// 完整程式碼請參考原文附件
}
?>
<?php
require 'SmtpMailer.php';
$mailer = new SmtpMailer('smtp.163.com', 25, 'webjx', 'webjx', 'webjx@163.com', '網站管理員');
$mailer->send(
'user@example.com',
'測試主題',
'<h1>HTML 內容</h1>',
'純文字內容',
'cc@example.com',
'bcc@example.com',
array('/path/to/file.pdf')
);
multipart/alternative 包裝 HTML+Text,multipart/mixed 掛載附件| 現象 | 可能原因 | 解決 |
|---|---|---|
| 連線逾時 | 防火牆封鎖 25/465/587 埠 | 開放埠口、改用 587 TLS |
| 認證失敗 | 帳密錯誤、未開啟 SMTP 服務 | 確認郵箱設定、使用授權碼 |
| 進垃圾箱 | 缺 SPF/DKIM、內容觸發規則 | 配置域名認證、優化內容 |
| 中文亂碼 | 編碼不一致 | 統一 UTF-8、正確設定 Charset |