使用邮件发送带附件的电子邮件()
<?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.
}
内容传输编码
可用的编码是 7bit , 8bit ,二进制, quoted-printable , base64 , ietf-token 和 x-token 。在这些编码中,当标题具有多部分 Content-Type 时,Content-Transfer-Encoding 不能是 RFC 2045 第 6.4 节中所述的除 7bit , 8bit 或 binary 之外的任何其他值。
我们的示例为 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 节)。