不依賴 PHPMailer 等第三方庫,直接使用 PHP mail() 函數配合標準 MIME 多部分訊息格式,即可實現帶附件郵件發送。本文提供 CMailFile 類別,手工組裝 multipart/mixed 訊息結構,支援 Base64 編碼附件、自訂 MIME 類型、下載檔名。
<?php
class CMailFile {
var $subject, $addr_to, $text_body, $text_encoded;
var $mime_headers, $mime_boundary = "--==================_846811060==_";
var $smtp_headers;
function CMailFile($subject,$to,$from,$msg,$filename,$downfilename,$mimetype="application/octet-stream",$mime_filename=false) {
$this->subject = $subject; $this->addr_to = $to;
$this->smtp_headers = $this->write_smtpheaders($from);
$this->text_body = $this->write_body($msg);
$this->text_encoded = $this->attach_file($filename,$downfilename,$mimetype,$mime_filename);
$this->mime_headers = $this->write_mimeheaders($filename, $mime_filename);
}
function attach_file($filename,$downfilename,$mimetype,$mime_filename) {
$encoded = $this->encode_file($filename);
if ($mime_filename) $filename = $mime_filename;
$out = "--".$this->mime_boundary."\n";
$out .= "Content-type: $mimetype; name=\"$filename\";\n";
$out .= "Content-Transfer-Encoding: base64\n";
$out .= "Content-disposition: attachment; filename=\"$downfilename\"\n\n";
$out .= $encoded."\n";
$out .= "--".$this->mime_boundary."--\n";
return $out;
}
function encode_file($sourcefile) {
if (is_readable($sourcefile)) {
$fd = fopen($sourcefile, "r");
$contents = fread($fd, filesize($sourcefile));
$encoded = chunk_split(base64_encode($contents));
fclose($fd);
return $encoded;
}
return '';
}
function sendfile() {
$headers = $this->smtp_headers . $this->mime_headers;
$message = $this->text_body . $this->text_encoded;
mail($this->addr_to, $this->subject, $message, $headers);
}
function write_body($msgtext) {
$out = "--".$this->mime_boundary."\n";
$out .= "Content-Type: text/plain; charset=\"us-ascii\"\n\n";
$out .= $msgtext."\n";
return $out;
}
function write_mimeheaders($filename, $mime_filename) {
if ($mime_filename) $filename = $mime_filename;
$out = "MIME-version: 1.0\n";
$out .= "Content-type: multipart/mixed; boundary=\"$this->mime_boundary\"\n";
$out .= "Content-transfer-encoding: 7BIT\n";
$out .= "X-attachments: $filename;\n\n";
return $out;
}
function write_smtpheaders($addr_from) {
$out = "From: $addr_from\n";
$out .= "Reply-To: $addr_from\n";
$out .= "X-Mailer: PHP\n";
$out .= "X-Sender: $addr_from\n";
return $out;
}
}
?>
<?php require_once 'emailclass.php'; $subject = "測試帶附件郵件"; $to = 'user@example.com'; $from = 'system@mysite.com'; $message = "親愛的用戶,請查收附件發票。"; $filename = 'invoice.pdf'; // 伺服器實體檔案路徑 $downfilename = '發票_202310.pdf'; // 使用者下載時顯示的檔名 $mimetype = "application/pdf"; $mail = new CMailFile($subject, $to, $from, $message, $filename, $downfilename, $mimetype); $mail->sendfile(); echo "郵件已送出";
Headers: From: system@mysite.com MIME-version: 1.0 Content-type: multipart/mixed; boundary="--==================_846811060==_" Body: --==================_846811060==_ Content-Type: text/plain; charset="us-ascii" 親愛的用戶,請查收附件發票。 --==================_846811060==_ Content-type: application/pdf; name="invoice.pdf" Content-Transfer-Encoding: base64 Content-disposition: attachment; filename="發票_202310.pdf" JVBERi0xLjQKJcfs... (Base64 編碼內容) --==================_846811060==_--
| 限制 | 改進 |
|---|---|
| 僅支援單一附件 | 修改 attach_file 迭代多檔案,各段落以 boundary 分隔 |
| 純文字內容,無 HTML 支援 | 改為 multipart/alternative 內嵌 text/plain + text/html |
| 中文主題/檔名未編碼 | 使用 =?UTF-8?B?'.base64_encode($str).'?= (RFC 2047) |
| 依賴本地 sendmail,送達率低 | 整合 SMTP 發送(fsockopen 或 PHPMailer) |
| 大附件記憶體耗盡 | 分塊讀取編碼,避免 file_get_contents 一次載入 |