【问题标题】:Sent mails with phpmailer don't go to "Sent" IMAP folder使用 phpmailer 发送的邮件不会转到“已发送”IMAP 文件夹
【发布时间】:2012-01-23 14:13:52
【问题描述】:

在我的 CRM 在线系统中,我使用 IMAP 协议控制传入邮件。 现在我正在使用 phpmailer 和 SMTP 协议发送邮件。 一切都很好,但我有一件奇怪的事情。如何使用 phpmailer 脚本将邮件发送到“已发送”IMAP 文件夹?

【问题讨论】:

    标签: php smtp imap phpmailer


    【解决方案1】:

    现在 PHPMailer 中有一个 getSentMIMEMessage 方法,它返回整个 MIME 字符串

    $mail = new PHPMailer();
    //code to handle phpmailer
    $result = $mail->Send();
    if ($result) {
      $mail_string = $mail->getSentMIMEMessage();
      imap_append($ImapStream, $folder, $mail_string, "\\Seen");
    }
    

    【讨论】:

    【解决方案2】:

    我找到了更简单的方法来做到这一点。 PHPmailer 将电子邮件准备为字符串 - 您所要做的就是将其放入正确的 IMAP 文件夹中。

    我用这段代码扩展了 phpmailer 类(因为 vars 受到保护,我无法访问它们):

    class PHPMailer_mine extends PHPMailer {
    public function get_mail_string() {
        return $this->MIMEHeader.$this->MIMEBody;
    }}
    

    PHP 代码:

    $mail= new PHPMailer_mine();
    //code to handle phpmailer
    $result=$mail->Send();
    if ($result) {
        $mail_string=$mail->get_mail_string();
        imap_append($ImapStream, $folder, $mail_string, "\\Seen");
    }
    

    效果很好。

    【讨论】:

    • 你能举例说明$ImapStream 和$folder 的值吗?我无法弄清楚 $folder 的正确值应该是多少,尤其是当我没有对邮件服务器的文件级访问权限时(我正在使用 Gmail)。
    【解决方案3】:

    嗯,这很困难,但可以做到。

    看看imap-append函数。
    通过连接到 IMAP 流资源,您可以使用 imap-append() 将邮件附加到 IMAP 帐户的已发送文件夹。

    但是通读 cmets 会告诉你完成它有点乏味,但肯定不是不可能 - 你可能需要自己编写一些代码,因为 phpmailer 不支持开箱即用(并且会很可能太耗时而无法实现而不是自己制作)。

    【讨论】:

    • 呃,真的吗?不过,我认为这是一个解决方案,尽管可以解决,所以我会给你一个这些美味的 +1。
    • 感谢您的回复 - 我会试试这个。
    • @DaveRandom 我同意你的观点,这并不漂亮,但可以解决 OP 的问题;虽然我不明白需要...
    【解决方案4】:
    • 您需要通过 IMAP 主机中继发送的邮件
    • IMAP 主机需要支持该功能(很少有人支持)

    如果这两点中的任何一个不正确,简短的回答是“你不能”。简而言之,这实际上取决于邮件提供商,而不是您的代码。

    尽管我讨厌 M$,但 Exchange 是他们真正做对的地方 - 如果您使用的是 Exchange 服务器,所有这一切都会为您处理。

    【讨论】:

      【解决方案5】:

      这很好用: Php Manual

      if (!$mail->send()) {
      //echo "Mailer Error: " . $mail->ErrorInfo;
      } else{
      
      //echo "Message sent!";
      //Section 2: IMAP
      //Uncomment these to save your message in the 'Sent Mail' folder.
      if (save_mail($mail)) {
      echo "Message saved!";
      }
      }
      
      //function
      function save_mail($mail)
      {
      $providerMail = 'Gmail';
      $providerMailSentFolder = 'Sent Mail';//You can change 'Sent Mail' to any folder
      $providerMailImap = 'imap.gmail.com';//imap.one.com
      $path = "{".$providerMailImap.":993/imap/ssl}[".$providerMail."]/".$providerMailSentFolder;
      //Tell your server to open an IMAP connection 
      //using the same username and password as you used for SMTP
      $imapStream = imap_open($path, $mail->Username, $mail->Password);
      $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
      imap_close($imapStream);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-20
        • 2019-03-26
        • 2012-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-21
        • 1970-01-01
        相关资源
        最近更新 更多