phpsmtpphpmailerimapbluehost

Bluehost: PHPMailer save sent message in Sent Items


I'm trying to send an email with the help of PHPMailer, everything works fine except now I want to have a copy of the send mail in Sent Items.

The website is currently hosted in Bluehost. I tried following the example of PHPMailer - GMail but I'm stuck on which path should I specify.

From the example of PHPMailer - GMail the path for Sent Items is:

{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail

I don't know what path should I specify. Everything works fine in my code, only the path for the sent item is missing.

<?php
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$username = 'john@mydomain.com';
$password = 'password';

function save_mail( $mail ) {
    //path or folder for sent items
    $path = "{imap.mydomain.com:993/imap/ssl}[...]/..."; //what exactly is the path for my sent item.

    //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 );
    return $result;
}

function send(){
    $mail = new PHPMailer(true);
    $mail->isSMTP();
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->Host = 'mail.mydomain.com';
    $mail->Port = 465;
    $mail->SMTPSecure = 'ssl';
    $mail->SMTPAuth = true;
    $mail->Username = $username;
    $mail->Password = $password;
    $mail->setFrom( $username, 'John Doe' );
    $mail->addAddress( 'someone@example.com', 'David Doe' );
    $mail->Subject = 'TEST SUBJECT';
    $mail->msgHTML('<b>TEST</b>');
    $mail->AltBody = 'TEST';

    if ( !$mail->send() ) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
        if (save_mail($mail)) {
            echo "Message saved!";
        }
    }
}

?>

Solution

  • Thanks to Max for the idea of imap_list function.

    This gives me the list of a path for my mail directories.

      $host = '{imap.mydomain.com/ssl/novalidate-cert}';
    
      $mail = 'support@mydomain.com';
      $pass = 'password';
    
    
      $mbox = imap_open($host, $mail, $pass, OP_HALFOPEN)
          or die("can't connect: " . imap_last_error());
    
    $list = imap_list($mbox, $host, "*");
    if (is_array($list)) {
        foreach ($list as $val) {
            echo imap_utf7_decode($val) . "\n";
        }
    } else {
        echo "imap_list failed: " . imap_last_error() . "\n";
    }
    
    imap_close($mbox);
    
    ?>
    

    using the {imap.mydomain.com} or {imap.mydomain.com:993/imap/ssl}gives me an error of:

    can't connect: Certificate failure for imap.mydomain.com: Server name does not match certificate: /OU=Domain Control Validated/OU=Hosted by BlueHost.Com, INC/OU=PositiveSSL Wildcard/CN=*.bluehost.com

    Certificate issue, luckily I found this question and I end up using the following host:

    {imap.mydomain.com/ssl/novalidate-cert}

    and the path I'm looking to forward the sent mail into sent items is:

    {imap.mydomain.com/ssl/novalidate-cert}INBOX.Sent