Phpmailer: [Question] Save sent mail

Created on 11 Dec 2016  路  10Comments  路  Source: PHPMailer/PHPMailer

Is there a way to move the mail after sending over SMTP to the folder "Sent" on the mailserver?
For example, if I write an email with the mobile phone or on the PC, after sending the mail, its already in the Sent-Folder.

Most helpful comment

I have created an example on how to do this because I needed it myself.

Would you accept a pull request @Synchro?

All 10 comments

Sending mail via SMTP has no concept of folders. In fact, after sending a mail via SMTP it does not exist anymore on the sending side, unless you take special actions (like using IMAP to store a copy of that message sent via SMTP).

I suggest you take a look at IMAP and Email :)

I have created an example on how to do this because I needed it myself.

Would you accept a pull request @Synchro?

Sure, that example looks good.

I created a pull request: #1116

New pull request: #1117

@NiklasBr Thanks mate - good work bro. We need this in PHPMailer!!!

Is this pull request done? The example looks like for gmail only, how can we use it for any emails, for example, email addresses created in cpanel?

Yes, this PR is done. Make it work on any other IMAP service by changing the hostname of the server you connect to.

I am using PHPMailer 6.0.3
The email is sent correcly to the recipient, but not shown in the sent folder of sender.
I must be missing a couple of line of codes. Please help me configure it correctly.

This is my code:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';

$fromEmail='email hosted in cpanel';
$MailTo='[email protected]';
$Subject='Contact meessage from your website';
$Body='<html><head>
</head>
<body>
<p>Hello</p>
<p>There is a new contact messagefrom Github</p>
<p>Hi, We have an answer to your question.</p>
</body></html>';

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->CharSet  = 'UTF-8';
$mail->Host = 'SMTP outgoing server';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = $fromEmail;
$mail->Password = '********';
$mail->setFrom($fromEmail, 'GitHub');   
$mail->addAddress($MailTo);
$mail->addReplyTo($fromEmail, 'GitHub');
$mail->isHTML(true);
$mail->Subject = $Subject;
$mail->Body    = $Body;
$mail->send();

?>

Whether this happens automatically is up to your mail server, but many do not do it. The way to do it manually is to upload a copy of the message via IMAP. This is outside PHPMailer鈥檚 scope, but look at the gmail example provided to see how to do that.

Was this page helpful?
0 / 5 - 0 ratings