MIME语法
根据RFC 2046(BNF语法,有些简化),这是多部分消息体的结构:
multipart-body := [preamble CRLF]
dash-boundary CRLF
body-part *encapsulation
close-delimiter
[CRLF epilogue]
dash-boundary := "--" boundary
body-part := MIME-part-headers [CRLF *OCTET]
encapsulation := delimiter
CRLF body-part
delimiter := CRLF dash-boundary
close-delimiter := delimiter "--"
还要注意消息中的每一行
必须
以
CRLF
-成对(例如。
"\r\n"
)以及
不能
超过1000个字符(包括
CRLF公司
). 这通常不是问题
text/plain
或
text/html
但如果有疑问,请使用
chunk_split (base64_encode ())
上
body-part
并添加
Content-transfer-encoding: base64
该部分的标题。
代码
下面的代码不言自明:
$first_name =$register_data['first_name'];
$to =$register_data['email'];
$from ='support@mysite.example';
$subject = 'Verify email address to activate account';
$email_code =$register_data['email_code'];
$boundary = md5(uniqid()); /* Unlikely to match any of body parts. */
$headers = "From: $from\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/alternative; boundary=\"$boundary\"\r\n";
$text_msg = <<<EOF
Your text message.
EOF;
$html_msg = <<<EOF
<html><title>Title</title><p>Your HTML message.
EOF;
$body = "--$boundary\r\n" /* dash-boundary */
. "Content-Type: text/plain\r\n"
. "\r\n"
. $text_msg
. "\r\n--$boundary\r\n" /* delimiter */
. "Content-Type: text/html\r\n"
. "\r\n"
. $html_msg
. "\r\n--$boundary--\r\n"; /* close-delimiter */
mail ($to, $subject, $body, $headers);
工具书类
有关详细信息,请参阅: