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

时间:2015-02-07 11:51:36

标签: email header mime

最近我遇到了--作为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="hudixt@gmail.com"; // Enter the mail to which you want to sent the attachement
$your_name = "Hudixt"; // Your name
$from_mail = "info@wpbrisk.com"; // 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);
        ?>

您能告诉我这个--在标题中的含义以及为什么它应该是唯一的。

2 个答案:

答案 0 :(得分:1)

对我来说似乎是一个MIME边界......

基本上,当您在电子邮件中有多种类型的内容时,使用ID分隔不同的部分。然后每个部分都以&#39; - &#39;开头。接着是边界。来自RFC:

因此,典型的multipart 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,附件(假设为word文件)将是application / msword。每个新的内容类型部分将由&#39; - &#39;分隔。 。 $边界。边界被选择为唯一的并且与其他消息部分不同,因此它不会出现在它们中的任何一个内部 - 当应用程序解释电子邮件内容找到$ boundary时,必须安全地假设&#39;这是一个预定的边界,而不仅仅是信息中的侥幸。

希望有所帮助!

答案 1 :(得分:1)

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

根据RFC#2046,这些只能是唯一的&#34;足够的&#34;。如果要将电子邮件封装到另一个电子邮件中,则边界不能相互推断,即它们必须是不同的。因此,尽可能让它们成为独一无二的是一个好主意#34;

  

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

相关问题