使用郵件傳送帶附件的電子郵件()

<?php

$to         = 'recipient@example.com';
$subject    = 'Email Subject';
$message    = 'This is the email message body';

$attachment = '/path/to/your/file.pdf';
$content = file_get_contents($attachment);

/* Attachment content transferred in Base64 encoding
MUST be split into chunks 76 characters in length as
specified by RFC 2045 section 6.8. By default, the
function chunk_split() uses a chunk length of 76 with
a trailing CRLF (\r\n). The 76 character requirement
does not include the carriage return and line feed */
$content = chunk_split(base64_encode($content));

/* Boundaries delimit multipart entities. As stated
in RFC 2046 section 5.1, the boundary MUST NOT occur
in any encapsulated part. Therefore, it should be
unique. As stated in the following section 5.1.1, a
boundary is defined as a line consisting of two hyphens
("--"), a parameter value, optional linear whitespace,
and a terminating CRLF. */
$prefix     = "part_"; // This is an optional prefix
/* Generate a unique boundary parameter value with our
prefix using the uniqid() function. The second parameter
makes the parameter value more unique. */
$boundary   = uniqid($prefix, true);

// headers
$headers    = implode("\r\n", [
    'From: webmaster@example.com',
    'Reply-To: webmaster@example.com',
    'X-Mailer: PHP/' . PHP_VERSION,
    'MIME-Version: 1.0',
    // boundary parameter required, must be enclosed by quotes
    'Content-Type: multipart/mixed; boundary="' . $boundary . '"',
    "Content-Transfer-Encoding: 7bit",
    "This is a MIME encoded message." // message for restricted transports
]);

// message and attachment
$message    = implode("\r\n", [ 
    "--" . $boundary, // header boundary delimiter line
    'Content-Type: text/plain; charset="iso-8859-1"',
    "Content-Transfer-Encoding: 8bit",
    $message,
    "--" . $boundary, // content boundary delimiter line
    'Content-Type: application/octet-stream; name="RenamedFile.pdf"',
    "Content-Transfer-Encoding: base64",
    "Content-Disposition: attachment",
    $content,
    "--" . $boundary . "--" // closing boundary delimiter line
]);

$result = mail($to, $subject, $message, $headers); // send the email

if ($result) {
    // Success! Redirect to a thank you page. Use the
    // POST/REDIRECT/GET pattern to prevent form resubmissions
    // when a user refreshes the page.
  
    header('Location: http://example.com/path/to/thank-you.php', true, 303);
    exit;
}
else {
    // Your mail was not sent. Check your logs to see if
    // the reason was reported there for you.
}

內容傳輸編碼

可用的編碼是 7bit8bit二進位制quoted-printablebase64ietf-tokenx-token 。在這些編碼中,當標題具有多部分 Content-Type 時,Content-Transfer-Encoding 不能是 RFC 2045 第 6.4 節中所述的除 7bit8bitbinary 之外的任何其他值。

我們的示例為 multipart 標頭選擇代表 US-ASCII 字元的 7 位編碼,因為如 RFC 2045 第 6 節所述,某些協議僅支援此編碼。然後可以在逐個部分的基礎上對邊界內的資料進行編碼(RFC 2046,第 5.1 節)。這個例子就是這樣做的。第一部分包含 text / plain 訊息,定義為 8bit,因為可能需要支援其他字元。在這種情況下,正在使用 Latin1(iso-8859-1)字符集。第二部分是附件,因此它被定義為 base64 編碼的應用程式/八位位元組流。由於 base64 將任意資料轉換為 7bit 範圍,因此可以通過受限制的傳輸傳送(RFC 2045,第 6.2 節)。