-
-
Save DavidRockin/b4867fd0b5bb687f5af1 to your computer and use it in GitHub Desktop.
| <?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 = "[email protected]"; // 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); | |
| } | |
| } | |
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 !
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();- 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. - 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 : ""); - 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! ;)
With PHPMailer v5.2.27 mails go to a "Sent" folder without a subject. But recipient receives email to INBOX with subject.
Thank you for your input! I did something similar without extending the PHPMailer class.