【问题标题】:PHPMailer how to save an email in Sent items folder on Outlook after being sent in PHPPHPMailer如何在PHP中发送后将电子邮件保存在Outlook上的已发送邮件文件夹中
【发布时间】:2020-09-06 23:51:39
【问题描述】:

我正在使用PHPMailer 从公司内部的 stmp 服务器发送电子邮件通知。电子邮件已成功发送,但未存储在 Outlook 上的已发送邮件文件夹中。

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/vendor/autoload.php';


$mail = new PHPMailer(true);

try { 

    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                     
    $mail->isSMTP();                                            
    $mail->Host       = 'smtpinternal.xxxx.com';  
    $mail->SMTPAuth = false;
    $mail->SMTPAutoTLS = false; 
    $mail->Username   = 'xxxxnoreply@xxxx.com';                     
    $mail->Password   = 'xxxx';                               
    $mail->Port       = 25; 
    $mail->setFrom('xxxxnoreply@xxxx.com', 'xxxx_NoReply');

    $distributionLists = explode(',', $items['Distribution List']);
    foreach ($distributionLists as $distributionList) {    
        if (!empty($distributionList) && strpos( $distributionList, '@' ) !== false ) { 
            $mail->addAddress(trim($distributionList)); } 
    }

    $contacts = explode(',', $items['Contact']);
    foreach ($contacts as $contact) {
        if (!empty($contact) && strpos( $contact, '@' ) !== false ) { 
            $mail->addCC(trim($contact)); } 
    }

    $mail->isHTML(true);                                  
    $mail->Subject = 'Invoice Report';
    $mail->Body = "Dear Client, <br><br> Please find below today's invoices <br><br>". $table ."<br> Please contact your representative on Email address : ". $items['Contact'] ."<br><br> Best regards. <br><br> Please do not reply to this message, as it has been sent by a system email address.";
    $mail->send();

  $mail_string = $mail->getSentMIMEMessage();
  $path = "{".$mail->Host.":25/imap/ssl/novalidate-cert}";
  $imapStream = imap_open($path, $mail->Username, $mail->Password);
    imap_append($ImapStream, $path, $mail_string, "\\Seen");



} catch (Exception $e) { 
  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }


?>

imap_open 函数返回以下错误:

imap_open(): Couldn't open stream {smtpinternal.xxxx.com:25/imap/ssl/novalidate-cert}

我尝试将$path 更新为

{"imap.smtpinternal.xxxx.com:25/imap/ssl/novalidate-cert}{smtpinternal.xxxx.com:25/imap/ssl/authuser=xxxxnoreply@xxxx.com}

但仍然得到同样的错误。有什么建议请我应该如何声明我的路径变量以指向 Outlook 中的 Sent Items 文件夹?谢谢。

【问题讨论】:

    标签: php outlook phpmailer imap imap-open


    【解决方案1】:

    最新版本的 Exchange(例如 Office 365 使用的那些)已经将通过 SMTP 发送的邮件保存在用户的“已发送邮件”文件夹中。

    【讨论】:

    • 我使用的是office 365,但事实并非如此。收件人在收件箱中收到电子邮件,但电子邮件未保存在 Outlook 中我的已发送邮件文件夹中
    • 是的,我在 CC 上添加了自己,电子邮件进入我的收件箱,但未显示在已发送邮件中
    • CC 与此无关。重要的是 FROM SMTP 命令和 From/Sender MIME 标头中指定的用户。
    【解决方案2】:

    我猜问题是您试图在端口 25 上使用 IMAP,这是入站 SMTP 的端口。您应该为 IMAP 使用端口 143,或者如果您想要对其进行 TLS 加密,则应使用 993,这就是 the IMAP upload example provided with PHPMailer 所做的。

    我无法告诉您要使用哪些 IMAP 路径 - 您需要参考 Outlook 的文档以了解更多信息。

    【讨论】:

    • 我尝试了你的建议,但仍然遇到同样的错误
    • 好的,所以调查为什么会发生这种情况,不要只是观察它 - 例如尝试远程登录到端口,阅读 Outlook 文档所说的内容。我们无法为您测试。
    • 很好,但请发布您尝试过的内容和结果,以便我们消除原因。那么当您尝试使用 telnet 或 openssl s_client 到该 IMAP 端口时发生了什么?
    • telnet 返回无法在端口 xxx 上打开主机:连接失败
    • 这就是你的答案;您的网络不允许访问它,因此 PHPMailer 也无法访问它。我建议您向您的主机开一张支持票。
    猜你喜欢
    • 2014-11-05
    • 2016-10-02
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多