简体   繁体   English

PHPMailer 如何在 PHP 中发送后将 email 保存在 Outlook 上的已发送项目文件夹中

[英]PHPMailer how to save an email in Sent items folder on Outlook after being sent in PHP

I am using PHPMailer to send emails notification from our internal stmp server in the company.我正在使用PHPMailer从公司内部的 stmp 服务器发送电子邮件通知。 The emails are sent successfully but not stored in the sent Items folder on Outlook .电子邮件已成功发送,但未存储在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}"; }


?>

The imap_open function returns the following error: imap_open function 返回以下错误:

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

I tried updating $path to我尝试将$path更新为

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

But still getting the same error.但仍然得到同样的错误。 Any suggestions please how i should declare my path variable to point to Sent Items folder in Outlook ?有什么建议请我应该如何声明我的路径变量以指向Outlook中的已发送邮件文件夹? Thank you.谢谢你。

I'd guess the problem is that you are trying to use IMAP on port 25, which is inbound SMTP's port.我猜问题是您试图在端口 25 上使用 IMAP,这是入站 SMTP 的端口。 You should be using port 143 for IMAP, or 993 if you want TLS encryption on it, which is what the IMAP upload example provided with PHPMailer does.您应该为 IMAP 使用端口 143,或者如果您希望对其进行 TLS 加密,则使用 993,这就是PHPMailer 提供的 IMAP 上传示例所做的。

I can't tell you what IMAP paths to use – you would need to refer to Outlook's docs for more on that.我无法告诉您要使用哪些 IMAP 路径——您需要参考 Outlook 的文档以了解更多信息。

The latest versions of Exchange (such as those used by Office 365) already save messages sent through SMTP in the user's Sent Items folder.最新版本的 Exchange(例如 Office 365 使用的那些)已经将通过 SMTP 发送的邮件保存在用户的已发送邮件文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM