2013-04-18 166 views
2

我正在試圖添加附件到我起草的電子郵件,但似乎無法使其正常工作。我試圖按照例子herehere,但沒有成功。使用PHP撰寫附件的IMAP電子郵件

這裏是我能到目前爲止做:

  1. 連接到Exchange服務器
  2. 打開郵箱
  3. 草案的HTML電子郵件
  4. 追加電子郵件到郵箱,並有它正確地呈現html。 (當使用text/html作爲內容類型時)。使用其他任何東西都會將html顯示爲明文。

作爲附加的註釋,正在起草後,將郵件附加到郵箱的Exchange 2010服務器上並且被看作然後通過Outlook 2010中

下面是我的郵件類和代碼發送草案電子郵件。

mailer.php

<?php 

class mailer 
{ 
    const USER  = 'user'; 
    const PASSWORD = 'pass'; 
    const MAILBOX = '{conn}DRAFTS'; 

    // STRING ORDER: $content-type . $from . $to . $cc . $subject . "\r\n\r\n" . $message 
    public $message; 
    public $imap_conn; 
    public $boundary; 

    function __construct() 
    { 
     // Open the message property so we can start appending strings. 
     $this->message = ''; 

     // Open the IMAP connection 
     $this->imap_conn = imap_open(self::MAILBOX,self::USER,self::PASSWORD); 
    } 

    public function content_type($string_type) 
    { 
     $this->message .= "Content-type:{$string_type}\r\n"; 
    } 

    public function from($string_from) 
    { 
     $this->message .= "From:{$string_from}\r\n"; 
    } 

    public function to($string_to) 
    { 
     $this->message .= "To:{$string_to}\r\n"; 
    } 

    public function cc($string_cc) 
    { 
     $this->message .= "Cc:{$string_cc}\r\n"; 
    } 

    public function mime($float_mime_version) 
    { 
     $this->message .= "MIME-Version:{$float_mime_version}\r\n"; 
    } 

    public function subject($string_subject) 
    { 
     $this->message .= "Subject:{$string_subject}\r\n\r\n"; 
    } 

    public function message($string_message) 
    { 
     $this->message .= "{$string_message}\r\n"; 
    } 

    public function set_boundary($string_boundary) 
    { 
     $this->boundary = $string_boundary; 
    } 

    public function append() 
    { 
     imap_append($this->imap_conn,self::MAILBOX,$this->message,"\\Draft"); 
     imap_close($this->imap_conn); 
    } 
} 

?> 

守則草案

// A random hash used for the boundary 
$rh = md5(date('c',time())); 
$data = chunk_split(base64_encode('Testing')); 


$m = new mailer; 
$m->set_boundary('--PHP-mixed-' . $rh); 
$m->content_type('multipart/mixed; boundary="' . $m->boundary . '"'); 
$m->mime('1.0'); 
$m->from('[email protected]'); 
$m->to('[email protected]'); 
$m->cc('[email protected],[email protected]'); 
$m->subject('A new email'); 
$m->message(" 
    {$m->boundary} 
    Content-Type: text/html; charset=\"utf-8\" 
    Content-Transfer-Encoding: base64 

    Testing my <b>HTML</b> 
    </br> 
    {$m->boundary} 
    Content-Type: application/octet-stream; name=\"test.txt\" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment; filename=\"test.txt\" 

    {$data} 
    {$m->boundary}-- 
    ")); 
$m->append(); 

消息之前,其附加

Content-type:multipart/mixed; boundary="--PHP-mixed-b408f941593cf92b5a8bd365abb4e64f" 
MIME-Version:1.0 
From:[email protected] 
To:[email protected] 
Cc:[email protected] 
Subject:New Message 


      --PHP-mixed-b408f941593cf92b5a8bd365abb4e64f 
      Content-Type: text/html; charset="utf-8" 
      Content-Transfer-Encoding: "base64" 

      My <b>html</b> content 



      --PHP-mixed-b408f941593cf92b5a8bd365abb4e64f 
      Content-Type: application/octet-stream; name="test.txt" 
      Content-Transfer-Encoding: base64 
      Content-Disposition: attachment; filename="test.txt" 



      --PHP-mixed-b408f941593cf92b5a8bd365abb4e64f-- 

回答

0

問題:如何將附件添加到通過IMAP用PHP起草的電子郵件?

答:總之,這個問題發生在兩個地方。

  1. 錯誤放置換行符(\ r \ n)。單個換行符應該在每個標題後面,並且在每個節的結尾標題後面有兩個換行符。見圖1.

  2. 不合理的界限。邊界應以 - 開頭,後跟一個唯一的字符串。消息的結尾邊界應該開始和結束 - 參見圖2

圖1

Content-Type: text/html\r\n 
Content-Encoding: base64\r\n\r\n 
"Message text here."\r\n 

圖2

--unique 
Content-Type: text/html\r\n 
Content-Encoding: base64\r\n\r\n 
HTML stuff here.\r\n 
--unique 
Content-Type: application/octet-stream\r\n 
Content-Disposition: attachment; filename=\"logs.csv\"\r\n 
Content-Transfer-Encoding: base64\r\n\r\n 
Attachment(s)\r\n 
--unique-- 

我打算在深度寫做多因爲它花了我很長的時間纔得到它的非常

0

這將有助於對您的調試一些更多的信息爲止。文件是否被成功讀取?有什麼東西讓它進入郵箱嗎?這也可以幫助我在最終追加之前看到完整信息的輸出。

我不知道這是否會解決它,但從我的成功代碼和第二個示例中,您的消息應該有一個額外的文件名屬性,值包裹在引號中。

Content-Type: application/octet-stream; name="test.txt" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="test.txt" 
+0

添加雙引號和附加前的消息文本。正在讀取文件。 – Chad

+0

再次嗨 - 你是否從這封郵件中刪除了該文件在SO上發佈?至少我會包括其中的一部分,以便我們可以驗證。這是否使郵箱很好?您也可以嘗試從消息代碼中刪除選項卡。 – Jordan

+0

我已經從使用一個文件切換到只編碼和分塊一個字符串,除了在編寫的字符串被打印時在電子郵件中打印編碼的字符串之外沒有任何改變。另外,我修改了消息文本,所以沒有不必要的空白。仍然沒有運氣。 – Chad