1

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   = '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(): 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/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 ? Thank you.

DevTN
  • 511
  • 2
  • 9
  • 35

2 Answers2

1

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • i tried your suggestion but still getting the same error – DevTN May 20 '20 at 16:36
  • OK, so investigate *why* this is happening, don't just observe that it is - for example try telnetting to the port, read what the Outlook docs say. We can't test it for you. – Synchro May 20 '20 at 17:39
  • Great, but post what you have tried and what your results were so that we can eliminate causes. So what happened when you tried to use telnet or openssl s_client to that IMAP port? – Synchro May 20 '20 at 17:49
  • telnet returns could not open to the host on port xxx: connect failed – DevTN May 20 '20 at 21:54
  • Well there’s your answer; your network does not permit access to that, so PHPMailer can’t get to it either. I suggest you open a support ticket with your host. – Synchro May 21 '20 at 06:57
0

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.

416E64726577
  • 2,214
  • 2
  • 23
  • 47
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I am using office 365 and it's not the case. The receivers get the email in their inbox but the email is not saved on my sent Items folder in outlook – DevTN May 20 '20 at 22:06
  • yes i added myself on CC the email comes to my inbox and but doesn't show up in Sent Items – DevTN May 21 '20 at 06:54
  • 1
    CC would have nothing to do with it. What maters is the user specified in the FROM SMTP command and From/Sender MIME headers. – Dmitry Streblechenko May 21 '20 at 07:53