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条)

报告相同问题?

悬赏问题

  • ¥50 大区域的遥感影像匹配 怎么做啊
  • ¥15 求解答:pytorch跑yolov8神经网络受挫
  • ¥20 Js代码报错问题不知道怎么解决
  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤