Skip to content

Instantly share code, notes, and snippets.

@DavidRockin
Created August 2, 2014 21:53
Show Gist options
  • Save DavidRockin/b4867fd0b5bb687f5af1 to your computer and use it in GitHub Desktop.
Save DavidRockin/b4867fd0b5bb687f5af1 to your computer and use it in GitHub Desktop.
PHPMailer save emails to IMAP folder
<?php
include("Mailer.php");
$mail = new Mailer();
$mail->isSMTP(); // Tell PHPMailer we're going to use SMTP
$mail->Host = 'mail.localhost'; // Set SMTP host
$mail->Username = 'noreply@localhost'; // Set the our email address
$mail->Password = 'YourPassword'; // Set email address password
$mail->From = "john.doe@example.com"; // The sender's email
$mail->FromName = "John Doe"; // The sender's name
$mail->addAddress("recipient@localhost"); // Recipient's email
$mail->addReplyTo($mail->From, $mail->FromName); // Add a reply to
$mail->Subject = "Mail for you!"; // Message subject
$mail->Body = "Your message contents..."; // Message contents
if($mail->send()) { // Attempt to send the email
$mail->copyToFolder(); // Will save into inbox
$mail->copyToFolder("Sent"); // Will save into Sent folder
} else {
echo "An error occurred, failed to send the email";
}
<?php
class Mailer extends PHPMailer {
/**
* Save email to a folder (via IMAP)
*
* This function will open an IMAP stream using the email
* credentials previously specified, and will save the email
* to a specified folder. Parameter is the folder name (ie, Sent)
* if nothing was specified it will be saved in the inbox.
*
* @author David Tkachuk <http://davidrockin.com/>
*/
public function copyToFolder($folderPath = null) {
$message = $this->MIMEHeader . $this->MIMEBody;
$path = "INBOX" . (isset($folderPath) && !is_null($folderPath) ? ".".$folderPath : ""); // Location to save the email
$imapStream = imap_open("{" . $this->Host . "}" . $path , $this->Username, $this->Password);
imap_append($imapStream, "{" . $this->Host . "}" . $path, $message);
imap_close($imapStream);
}
}
@wjentner
Copy link

Thank you for your input! I did something similar without extending the PHPMailer class.

$imapStream = imap_open("{localhost:143/novalidate-cert}Sent", $username, $password);
imap_append($imapStream, "{localhost:143}Sent", $mail->getSentMIMEMessage(), "\\Seen");
imap_close($imapStream);

@jicao
Copy link

jicao commented Jul 20, 2016

I know that this is a bit "old" but I had some trouble with this

<b>Notice</b>: Unknown: rsh to IMAP server timed out (errflg=1) in <b>Unknown</b> on line <b>0</b>

And i fixed it like this :

public function copyToFolder($folderPath = null) {
$message = $this->MIMEHeader . $this->MIMEBody;
$path = "INBOX" . (isset($folderPath) && !is_null($folderPath) ? ".".$folderPath : ""); // Location to save the email
$imapStream = imap_open("{" . $this->Host . "/tls/novalidate-cert/norsh/service=imap/user=" . $this->Username . "}" . $path , $this->Username, $this->Password);
imap_append($imapStream, "{" . $this->Host . "}" . $path, $message);
imap_close($imapStream);
}

I had to add /tls/novalidate-cert/norsh/service=imap/user=" . $this->Username . "

Hope this will help someone :)

By the way, thanks for the function !

@gillesbs
Copy link

gillesbs commented Jul 2, 2018

Thank you very much for this handy code!

A few remarks on what I had to change in order to get it working:

  1. class Mailer extends \PHPMailer\PHPmailer\PHPMailer {}
  2. $message = $this->getSentMIMEMessage();
  3. Don't forget to add the IMAP port and needed flags for your server, so $imapStream = imap_open("{[host]:[port]/[flags]}" . $path , $this->Username, $this->Password);, otherwise you'll not be able to connect to your server.
  4. On my e-mail server, the "sent" inbox was not "INBOX.Sent" but just "Sent". You can check the name of your inboxes using the imap_list() function. So I used $path = (isset($folderPath) && !is_null($folderPath) ? $folderPath : "");
  5. If you want to mark the e-mails you put in the inbox as read, you can do this: imap_append($imapStream, "{" . $this->Host . "}" . $path, $message ."\r\n","\\Seen");

Good day and have fun coding! ;)

@xninedesign
Copy link

With PHPMailer v5.2.27 mails go to a "Sent" folder without a subject. But recipient receives email to INBOX with subject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment