【问题标题】:How to save an Email attachment as an Email message?如何将电子邮件附件保存为电子邮件?
【发布时间】:2011-10-19 20:35:21
【问题描述】:

在使用 php 处理电子邮件时,我还是个新手。

基本上我需要读取 pop3 邮箱并将邮件保存到数据库并将附件保存到文件夹。然后将相同的邮件(稍作改动)邮寄给另一个电子邮件收件人。

我以为我已经完成了这一切,然后我意识到当附件是另一封邮件时,我只收到正文作为文本(在 base64_decode 之后)。

是否可以将附加的电子邮件保存到文件中,然后将其作为附件发送到另一封邮件中?

我们将不胜感激。

下面是一些代码:

// This is the IMAP connection: imap_open("{"."$host:$port/pop3$ssl"."}$folder",$user,$pass,OP_SILENT);
$mailConnection = $this->Maildata->mail_login($host,$port,$user,$pass,$folder,$ssl);

// This retrieves the list of mails in the current folder
$messageList = $this->Maildata->mail_list($mailConnection,$message);
foreach ($messageList as $messageId => $messageListData) {

    // This reads the mail object into an array
    $mailMessage = $this->Maildata->mail_mime_to_array($mailConnection,$messageId,true);

    // This loops through each mail
    foreach ($mailMessage as $tmpId => $tmpData) {
        // Excluded save to database since it is unrelated

        // This checks if item is an attachment and saves it.
        if (isset($tmpData['is_attachment']) && $tmpData['is_attachment'] === true) {
            $tmpName = date('Y-m-d H:i:s', strtotime('now'));
            $this->Maildata->mail_attachment_save($tmpData['data'], $tmpName .$tmpData['name']);
        }
    }
}

附:这一切都有效,但imap_fetchbody($mailConnection , $messageId, $prefix); 似乎没有发送我需要的所有数据。

【问题讨论】:

  • 请向我们展示您的代码。说它是“另一条消息”是不正确的,您使用的是什么 MIME 解析器?
  • 这可能会有所帮助:php.net/manual/en/book.imap.php 尽管上面写着“IMAP”,但我相信它也可以与 POP 一起使用。
  • @DaveRandom 我正在使用 IMAP 连接到邮箱。我会添加一些代码,但我不相信它会有所帮助。
  • @Nightwolf 您使用哪些库/函数来执行此操作?
  • @DaveRandom 我使用的代码是对http://www.php.net/manual/en/intro.imap.php 的唯一评论。但是我需要纠正代码中的简单缺陷。

标签: php


【解决方案1】:

是的。

您可以将附加的邮件信息保存为文件,但它不会采用 Microsoft Office Outlook 能够理解的格式。有趣的是,如果您将文件保存为 .eml,那么 windows live mail 将完美地处理邮件。

没有 webmail 服务像 Outlook 和 windows live mail 那样处理附加的邮件项目,而 gmail 完全忽略它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2021-06-26
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2012-05-06
    • 2019-06-05
    相关资源
    最近更新 更多