dsgm5631 2013-04-18 16:40
浏览 46
已采纳

使用PHP使用附件起草IMAP电子邮件

I'm trying to add an attachment to emails that I draft but cannot seem to get it to work. I've tried to follow the examples here and here but without success.

Here's what I'm able to do so far:

  1. Connect to the Exchange server
  2. Open the mailbox
  3. Draft an html email
  4. Append the email to the mailbox and have it render the html correctly. (when using text/html as the content-type). Using anything else displays the html as plaintext.

As an additional note, after being drafted, the emails are appended to a mailbox on an Exchange 2010 server and are viewed then sent via Outlook 2010.

Below is my mailer class and the code to draft the email.

mailer.php

<?php

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

    // STRING ORDER: $content-type . $from . $to . $cc . $subject . "

" . $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}
";
    }

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

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

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

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

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

";
    }

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

    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

// 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('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 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();

The message before its 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"

            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--
  • 写回答

2条回答 默认 最新

  • du532861657 2013-04-22 20:49
    关注

    Question: How do I add an attachment to an email drafted with PHP via IMAP?

    Answer: In short, the issue was in two places.

    1. Improper placement of newlines ( ). A single newline should be after each header and two newlines after each ending header of a section. See Figure 1.

    2. Improper boundaries. Boundaries should start with -- followed by a unique string. The ending boundary of the message should begin and end with -- See Figure 2

    Figure 1

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

    Figure 2

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

    I plan on doing a more in depth write-up on this as it took me a very long time to get it right.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 思科校园网的组建,sos!
  • ¥15 主要进行描述非满管状态下,管路的摩阻系数是怎么变化的,在管路长度方向上是怎么分布的(标签-matlab)
  • ¥15 multisim设计求解如图
  • ¥15 动态多目标优化,求解定义的问题的POF和POS公式
  • ¥30 实验 :UML2.0 行为建模
  • ¥15 设计一种优化调度算法并结合论文中的案例给出调度计划
  • ¥15 py文件打包exe报错TypeError:expected str,bytes or os.PathLike object,not_io.BytesIO
  • ¥15 Java编写学籍信息管理系统
  • ¥15 IPXE网刻客户机找不到服务端
  • ¥15 小贝360-4 配二个 华772S 设置WⅰFi5G 连接