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

3.4k views Asked by At

I am using PHPMailer to send emails notification from our internal stmp server in the company. The emails are sent successfully but not stored in the sent Items folder on 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   = '[email protected]';                     
    $mail->Password   = 'xxxx';                               
    $mail->Port       = 25; 
    $mail->setFrom('[email protected]', '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(): Couldn't open stream {smtpinternal.xxxx.com:25/imap/ssl/novalidate-cert}

I tried updating $path to

{"imap.smtpinternal.xxxx.com:25/imap/ssl/novalidate-cert} or {smtpinternal.xxxx.com:25/imap/ssl/[email protected]}

But still getting the same error. Any suggestions please how i should declare my path variable to point to Sent Items folder in Outlook ? Thank you.

3

There are 3 answers

5
Synchro On

I'd guess the problem is that you are trying to use IMAP on port 25, which is inbound SMTP's port. 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.

I can't tell you what IMAP paths to use – you would need to refer to Outlook's docs for more on that.

3
Dmitry Streblechenko On

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.

2
Максим On

$mail_string = $mail->getSentMIMEMessage();

$path = "&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-";//Отправленые

    $imapStream = imap_open("{imap.yandex.ru:993/imap/ssl}" . $path , $mail->Username, $mail->Password);
    imap_append($imapStream, "{imap.yandex.ru:993}" . $path, $mail_string);
    imap_close($imapStream);