PHPMailer / PHPMailer

The classic email sending library for PHP
GNU Lesser General Public License v2.1
20.97k stars 9.73k forks source link

[Question] Save sent mail #895

Closed KSas closed 7 years ago

KSas commented 7 years ago

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.

ooxi commented 7 years ago

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 :)

NiklasBr commented 7 years ago

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

Would you accept a pull request @Synchro?

Synchro commented 7 years ago

Sure, that example looks good.

NiklasBr commented 7 years ago

I created a pull request: #1116

NiklasBr commented 7 years ago

New pull request: #1117

MESWEB commented 6 years ago

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

akadebnath commented 5 years ago

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?

Synchro commented 5 years ago

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

akadebnath commented 5 years ago

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='myemail@gmail.com';
$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();

?>
Synchro commented 5 years ago

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’s scope, but look at the gmail example provided to see how to do that.

kishor10d commented 3 years ago

@akadebnath I am using PHPMailer 6.1.3 The email is sent correcly to the recipient, and successfully saved in sent folder. I have my own domain. I am using webmail (RoundCube). I done some changes in @NiklasBr example. Please refer to his example before looking at this method.

May be you already figured out this thing. But I am giving here for others who are searching the same thing.

This is my code:

function save_mail($mail) {
    //You can change 'Sent Mail' to any other folder or tag
    //Use imap_getmailboxes($imapStream, '/imap/ssl') to retrieve a list of available folders or tags
    $path = "{mail.yourdomain.com:993/imap/ssl/novalidate-cert}INBOX.Sent"; // 993 is my IMAP port.
    //Tell your server to open an IMAP connection using the same username and password as you used for SMTP
    $imapStream = imap_open($path, $mail->Username, $mail->Password);
    $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
    imap_close($imapStream);
    return $result;
}