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

PHP 發送帶附件郵件類別:基於 MIME 標準的原生實作

     編輯:PHP 綜合知識

不依賴 PHPMailer 等第三方庫,直接使用 PHP mail() 函數配合標準 MIME 多部分訊息格式,即可實現帶附件郵件發送。本文提供 CMailFile 類別,手工組裝 multipart/mixed 訊息結構,支援 Base64 編碼附件、自訂 MIME 類型、下載檔名。

CMailFile 類別程式碼

<?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 "郵件已送出";

MIME 訊息結構說明

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==_--

局限性與改進方向

此類別適合簡單內部通知、無法安裝 Composer/擴充套件的受限環境。生產系統建議使用 PHPMailer 或 Swift Mailer。完整原始碼可至本站下載。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
限制改進
僅支援單一附件修改 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 一次載入