2010-03-17 61 views
2

下面的$ header是通過PHP的mail($to,$subject,$content,$header)命令發送的。郵件到達,似乎有一個附件。但是郵件文本和文件一樣是空的。我認爲這與行間距有關,但我看不出問題所在。我試圖把內容(在邊界之間)放在$contents而不是附加到$header。它沒有什麼區別。有什麼想法嗎?此郵件標題文本有什麼問題?

From: [email protected] 
Reply-To: [email protected] 
X-Mailer: PHP 5.3.1 
MIME-Version: 1.0 
Content-Type: multipart/mixed; 
    boundary="7425195ade9d89bc7492cf520bf9f33a" 

--7425195ade9d89bc7492cf520bf9f33a 
Content-type:text/plain; charset=iso-8859-1 
Content-Transfer-Encoding: 7bit 

this is a test message. 

--7425195ade9d89bc7492cf520bf9f33a 
Content-Type: application/pdf; name="test.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="test.pdf" 

JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k 
ZT4+CnN0cmVhbQp4nE2PT0vEMBDFadVdO4p/v8AcUyFjMmma5CqIIF5cctt6WnFBqLD1+4Np1nY3 
c3lvfm+GyQ4VaUY11iQ2PTyuHG5/Ibdx9fIvhi3swJMZX24c602PTzENegwUbNAO4xcoCsG5xuWE 
. 
... the rest of the file 
. 
MDAwMDA2MDYgMDAwMDAgbiAKMDAwMDAwMDcwNyAwMDAwMCBuIAowMDAwMDAxMDY4IDAwMDAwIG4g 
CjAwMDAwMDA2NDcgMDAwMDAgbiAKMDAwMDAwMDY3NyAwMDAwMCBuIAowMDAwMDAxMjg2IDAwMDAw 
IG4gCjAwMDAwMDA5MzIgMDAwMDAgbiAKdHJhaWxlcgo8PCAvU2l6ZSAxNCAvUm9vdCAxIDAgUiAv 
SW5mbyAyIDAgUgovSUQgWzxEMURDN0E2OUUzN0QzNjI1MDUyMEFFMjU0MTMxNTQwQz48RDFEQzdB 
NjlFMzdEMzYyNTA1MjBBRTI1NDEzMTU0MEM+XQo+PgpzdGFydHhyZWYKNDY5MwolJUVPRgo= 


--7425195ade9d89bc7492cf520bf9f33a-- 

$header結束時不換行

回答

1

我使用下面的代碼來發送消息帶有附件以及html和文本的消息部分。我記得很久以後才與之對抗,但不幸的是我不記得哪些部分是我的問題。也許看着它會幫助你。我已經改變了一些不相關的變量,並希望更容易閱讀。隨意問問是否有些奇怪或不清楚,我會盡力解釋。

public function sendEmail($htmlString) 
{ 
    $random_hash = md5(date('r', time())); 

    $message = "--mixed-$random_hash\n"; 
    $message .= 'Content-Type: multipart/alternative; boundary="alt-' . $random_hash . '"' . "\n\n"; 
    $message .= 'MIME-Version: 1.0' . "\n"; 
    $message .= '--alt-' . $random_hash . "\n"; 
    $message .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n"; 
    $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n"; 

    // text message 
    $message .= strip_tags($htmlString) . "\n\n"; 

    $message .= '--alt-' . $random_hash . "\n"; 
    $message .= 'Content-Type: text/html; charset="iso-8859-1"' . "\n"; 
    $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n"; 

    // html message 
    $message .= $htmlString . "\n\n"; 

    $message .= '--alt-' . $random_hash . '--' . "\n"; 

    // graph image 
    if($this->selectedGraph != 0) 
    { 
     $graphString = $this->getGraph(); // image attachment 
     $graphString = chunk_split(base64_encode($graphString)); 

     $linkID = 'graph-' . $userInfo['FirmID'] . $random_hash . '-image'; 

     $message .= '--mixed-' . $random_hash . "\n"; 
     $message .= 'MIME-Version: 1.0' . "\n"; 
     $message .= 'Content-Transfer-Encoding: base64' . "\n"; 
     $message .= 'Content-ID: ' . $linkID . "\n"; 
     $message .= 'Content-Type: image/gif; name="graph.gif"' . "\n"; 
     $message .= 'Content-Disposition: attachment' . "\n\n"; 

     $message .= $graphString; 
     $message .= '--mixed-' . $random_hash . '--' . "\n"; 

    } 
    else 
    { 
     $message .= '--mixed-' . $random_hash . '--' . "\n"; 
    } 


    $headers = 'From: ' . $this->from. "\r\nReply-To: " . $this->replyto; 
    $headers .= "\r\nContent-Type: multipart/related; boundary=\"mixed-" . $random_hash . "\"\r\nMIME-Version: 1.0"; 

    $flags = '-f ' . BOUNCED_EMAIL_ADDRESS; 

    return mail($userInfo['Email'], $this->subject, $message, $headers, $flags); 

} 
+0

根據您的示例,我將行結束符從\ r \ n更改爲\ n。這解決了它。 TX! – dnagirl 2010-03-17 16:30:28

+0

行結束將是我的建議,但我遲到了:)正確的格式是\ r \ n,但根據我的經驗,有一些電子郵件服務器程序,試圖自己修復它,並最終打破信息。 – 2010-03-17 17:17:30

0

這些事情發生在從零開始構建MIME郵件時。有幾個php模塊可以用來生成酷兼容的mime消息。 Mail_Mime應該做的伎倆。