Drafting IMAP emails with an attachment using PHP -
i'm trying add attachment emails draft cannot seem work. i've tried follow examples here , here without success.
here's i'm able far:
- connect exchange server
- open mailbox
- draft html email
- append email mailbox , have render html correctly. (when using text/html content-type). using else displays html plaintext.
as additional note, after being drafted, emails appended mailbox on exchange 2010 server , viewed sent via outlook 2010.
below mailer class , code draft email.
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 message property can start appending strings. $this->message = ''; // open 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); } } ?> draft code
// random hash used 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('from@mail.com'); $m->to('to@mail.com'); $m->cc('cc1@mail.com,cc2@mail.com'); $m->subject('a new email'); $m->message(" {$m->boundary} content-type: text/html; charset=\"utf-8\" content-transfer-encoding: base64 testing <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(); the message before appended
content-type:multipart/mixed; boundary="--php-mixed-b408f941593cf92b5a8bd365abb4e64f" mime-version:1.0 from:from@mail.com to:to@mail.com cc:cc1@mail.com subject:new message --php-mixed-b408f941593cf92b5a8bd365abb4e64f content-type: text/html; charset="utf-8" content-transfer-encoding: "base64" <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--
it helpful have more information on debugging far. file being read successfully? make mailbox? me see output of full message before final append.
i not know if solve it, successful code , second example, message should have filename property, , values wrapped in quotes.
content-type: application/octet-stream; name="test.txt" content-transfer-encoding: base64 content-disposition: attachment; filename="test.txt"
Comments
Post a Comment