Notes; Appending PHPMailer Email to Sent or Any Folder with IMAP

These are my notes for getting ‘imap_append‘ to work with PHPMailer. This is not necessarily the best solution, but here’s mine for those interested.



I have a script that uses PHPMailer to send out HTML formatted emails with SMTP, through an HTML form. Emails were not saved to the sent folder with this set up, so I wanted to add a feature to append all the sent emails to a specific folder.

Also, append a different email to a second folder if there was an error. And the error message will be left unread while saved email is appended in read state.


Change imap server, folder, username & password fields. The folder is probably in the format “{imapserver}folder” (more on that below).

if(!$mail->send()) {
$stream = imap_open("{}", "login", "password");
imap_append($stream, "{}Sent Error"
, "From:\r\n"
. "To:\r\n"
. "Subject: Sent Error\r\n"
. "Content-Type: text/html;\r\n\tcharset=\"utf-8\"\r\n"
. "Content-Transfer-Encoding: 8bit \r\n"
. "\r\n"
. "There has been an error (see below).<br /><br />\r\n"
. "<font color=\"red\">" . $mail->ErrorInfo . "</font>"
. "<br /><br />Message below:<br />\r\n"
. $message
} else {
$stream = imap_open("{}", "login", "password");
imap_append($stream, "{}Sent OK"
, "From:\r\n"
. "To: " . $_POST['email'] . "\r\n"
. "Subject: Sent Successfull\r\n"
. "Content-Type: text/html;\r\n\tcharset=\"utf-8\"\r\n"
. "Content-Transfer-Encoding: 8bit \r\n"
. "\r\n"
. $message
, "\Seen"

PS. Find the different IMAP server connection settings here.


Usual advice is something like, “$mail->getSentMIMEMessage()” to get raw message for mail output. But I haven’t used it, on “PHP 5.4.45” I got this error;

Call to undefined method PHPMailer::getSentMIMEMessage()

I wasn’t sure what the correct name of the folder of my email sent folder was, so this was the work around. Create an imap_getmailboxes.php file;

Change imap server, username & password fields.

 $mbox = imap_open("{}", "username", "password", OP_HALFOPEN)
       or die("can't connect: " . imap_last_error());
$list = imap_getmailboxes($mbox, "{}", "*");
 if (is_array($list)) {
     foreach ($list as $key => $val) {
         echo "($key) ";
         echo imap_utf7_decode($val->name) . ",";
         echo "'" . $val->delimiter . "',";
         echo $val->attributes . "<br />\n";
 } else {
     echo "imap_getmailboxes failed: " . imap_last_error() . "\n";

Run php imap_getmailboxes.php, if you get;

PHP Fatal error: Call to undefined function imap_open() in imap_getmailboxes.php on line 2

For example (will differ depending on PHP version); apt-get install php5-imap

For me Apache was automatically restarted, if not, need to do it manually.

An easy way to check if it’s enabled is, php -m | grep imap

Here’s my example output with imap_getmailboxes;

(0) {}archive-2015,'/',64
(1) {}Calendar,'/',64
(2) {}Contacts,'/',32
(3) {}Deleted Items,'/',64
(4) {}Drafts,'/',68
(5) {}INBOX,'/',68
(6) {}Journal,'/',64
(7) {}Junk Email,'/',64
(8) {}Notes,'/',64
(9) {}Outbox,'/',64
(10) {}Sent,'/',64
(11) {}Sent Items,'/',64
(12) {}Tasks,'/',64
(13) {}Trash,'/',68

Therefore the correct folder was for example “{}Sent”.

And that’s pretty much it.

Caring is sharing.. thank you! -David

Twitter Button Facebook Button Google Button LinkedIn Button

Write a Comment