2015-02-07 19 views
0

最近我遇到了--作爲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

回答

1

這些「--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

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。每個新的內容類型部分將用' - '分隔。 $邊界。邊界被選擇爲唯一的並且與其他消息部分不同,以使其不出現在它們中的任何一箇中 - 當應用程序解釋電子郵件內容時發現$邊界,假設這是預期的邊界並且不是隻是消息中的僥倖。

希望有幫助!

相關問題