This function is how you take a sent message in your mail shell and place a copy of it in the remote mail server's sent folder.
It is however not intuitive and I struggled for a couple hours so I'm placing these notes here to spare others the aggravation. Some of the errors I encountered:
- Can't append to mailbox with such a name
- Internal date not correctly formatted
The second/folder parameter is not the string you might think it is (e.g. "Sent", "Inbox.Sent", etc). It is the connection information used by imap_open() which doesn't make sense as the connection is already open! Whatever, here is a basic example addressing those three errors:
<?php
$server = '{mail.example.com:993/ssl/imap}INBOX.Sent';
$mail_connection_folder = imap_open($server, $user, $pass);
if ($mail_connection)
{
$result = imap_append($mail_connection, $server, $message_string_raw, '\\Seen', date('d-M-Y H:i:s O'));
}
?>
I had been using the PHP Pear Mail extension which did a fantastic job with DMARC, SPF, DKIM, etc. However it's not well maintained and I couldn't figure out if it returns the email message string. The PHPMailer library (https://github.com/PHPMailer/PHPMailer) does return the message string:
<?php
//Skip to key parts:
$result = $mail->send();
if ($result)
{
$message_string_raw = $mail->getSentMIMEMessage();
}
else {/*error handling*/}
?>
Hopefully this will spare some folks a lot of aggravation.