Created
August 2, 2014 21:53
-
-
Save DavidRockin/b4867fd0b5bb687f5af1 to your computer and use it in GitHub Desktop.
PHPMailer save emails to IMAP folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
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
Thank you very much for this handy code!
A few remarks on what I had to change in order to get it working:
class Mailer extends \PHPMailer\PHPmailer\PHPMailer {}
$message = $this->getSentMIMEMessage();
$imapStream = imap_open("{[host]:[port]/[flags]}" . $path , $this->Username, $this->Password);
, otherwise you'll not be able to connect to your server.imap_list()
function. So I used$path = (isset($folderPath) && !is_null($folderPath) ? $folderPath : "");
imap_append($imapStream, "{" . $this->Host . "}" . $path, $message ."\r\n","\\Seen");
Good day and have fun coding! ;)