我正在試圖添加附件到我起草的電子郵件,但似乎無法使其正常工作。我試圖按照例子here和here,但沒有成功。使用PHP撰寫附件的IMAP電子郵件
這裏是我能到目前爲止做:
- 連接到Exchange服務器
- 打開郵箱
- 草案的HTML電子郵件
- 追加電子郵件到郵箱,並有它正確地呈現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--
添加雙引號和附加前的消息文本。正在讀取文件。 – Chad
再次嗨 - 你是否從這封郵件中刪除了該文件在SO上發佈?至少我會包括其中的一部分,以便我們可以驗證。這是否使郵箱很好?您也可以嘗試從消息代碼中刪除選項卡。 – Jordan
我已經從使用一個文件切換到只編碼和分塊一個字符串,除了在編寫的字符串被打印時在電子郵件中打印編碼的字符串之外沒有任何改變。另外,我修改了消息文本,所以沒有不必要的空白。仍然沒有運氣。 – Chad