什么是 - 在电子邮件标题中?

问题描述:

最近我遇到了--作为php的mail()函数的头文件。我试图谷歌它,但没有发现任何结果,所以我张贴这个问题。在尝试编辑我的代码时,我发现这两个--后的id应该是唯一的。我的php脚本用附件发送电子邮件。什么是 - 在电子邮件标题中?

<?php 

$file = "http://wpbrisk.com/download/email/email/doc1.pdf"; // Specify the Url of the attachment here 

$filename = "doc1.pdf"; // Specify the name of the file here 

$to="[email protected]"; // Enter the mail to which you want to sent the attachement 
$your_name = "Hudixt"; // Your name 
$from_mail = "[email protected]"; // Your email 

$subject = "This is a mail with attachment."; // Subject 
$message = "<div id='hello'>World</div>"; // message 

// Get the content of the file 
    $content = file_get_contents($file); 

    $content = base64_encode($content); 
    $uid = md5(uniqid(time())); 

    $header = "From: ".$from_name." <".$from_mail.">\r\n"; 

    $header .= "MIME-Version: 1.0\r\n"; 
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
    $header .= "This is a multi-part message in MIME format.\r\n"; 
    $header .= "--".$uid."\r\n"; 


    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $header .= $message."\r\n\r\n"; 
    $header .= "--".$uid."\r\n"; 

    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here 

    $header .= "Content-Transfer-Encoding: base64\r\n"; 
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; 
    $header .= $content."\r\n\r\n"; 
    $header .= "--".$uid."--"; 
    mail($mailto, $subject, "", $header); 
     ?> 

您能否告诉我这是什么--意味着在标题和为什么它应该是唯一的。

+0

请发布所有相关代码和您正在讨论的上下文。我想这是关于MIME的部分边界,对吧? – MrTux 2015-02-07 11:52:43

+0

双短划线是以某种格式发表评论的,也许就是这样 – MightyPork 2015-02-07 11:53:22

+0

对我来说看起来像一些边界 – PeeHaa 2015-02-07 11:53:50

这些“--ID--”行是MIME部分边界(请参阅https://en.wikipedia.org/wiki/MIME)。它们在https://tools.ietf.org/html/rfc2046中描述。它们用于分隔消息的不同部分。

根据RFC#2046,这些只有唯一的“足够”。如果你想把一封电子邮件封装到另一封电子邮件中,则边界不得彼此推断,即它们必须是不同的。所以这是一个好主意,让他们作为唯一“成为可能,需要”:

这意味着它是至关重要的组成代理能够 选择和指定,它 不包含一个唯一的边界参数值作为前缀的封闭式多部分 的边界参数值。 (https://tools.ietf.org/html/rfc2046#section-5.1

似乎是一个MIME边界给我...

基本上,当你有多个类型的电子邮件消息的内容,不同的部分使用的是ID分开。然后在每个部分之前加上“ - ”,然后是边界。从RFC:

因此,典型的多部分的Content-Type首部字段可能是这样的:

Content-Type: multipart/mixed; 
     boundary=gc0p4Jq0M2Yt08jU534c0p 

这表明实体由几部分组成,每一个本身与该是语法上等同于一个结构的RFC 822的消息,不同的是首标区可能完全是空的,并且这些部件各自在前面由线 --gc0p4Jq0M2Yt08jU534c0p

更多信息参见http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

要回答你最后一个问题,当你有一个带有文本和附件的电子邮件时,整个消息将具有Content-Type:multipart/mixed,文本将是text/plain,附件(假设一个文件)将会是application/msword。每个新的内容类型部分将用' - '分隔。 $边界。边界被选择为唯一的并且与其他消息部分不同,以使其不出现在它们中的任何一个中 - 当应用程序解释电子邮件内容时发现$边界,假设这是预期的边界并且不是只是消息中的侥幸。

希望有帮助!