Sent mails with phpmailer don't go to "Sent" IMAP folder
Asked Answered
A

6

16

in my CRM online system I control ingoing mails with IMAP protocol. Now I'm making sending mails with phpmailer and SMTP protocol. Everything is ok but I have one wierd thing. How to make sent with phpmailer script mails go to "Sent" IMAP folder?

Aruwimi answered 19/12, 2011 at 12:38 Comment(0)
H
16

There is now a method getSentMIMEMessage in PHPMailer which returns the whole MIME string

$mail = new PHPMailer();
//code to handle phpmailer
$result = $mail->Send();
if ($result) {
  $mail_string = $mail->getSentMIMEMessage();
  imap_append($ImapStream, $folder, $mail_string, "\\Seen");
}
Hermia answered 28/7, 2015 at 16:3 Comment(1)
That's right. Since July 13, 2012. See this changelog: github.com/PHPMailer/PHPMailer/blob/master/… Also need to check php.net/manual/en/function.imap-append.php to understand the function imap_append() and $folder parameter, and php.net/manual/en/function.imap-open.php to understand how to get $ImapStream.Probative
A
14

I found easier way to do this. PHPmailer prepares email as string - all You have to do is to put it into right IMAP folder.

I expanded phpmailer class with this code (since vars are protected I can't reach them):

class PHPMailer_mine extends PHPMailer {
public function get_mail_string() {
    return $this->MIMEHeader.$this->MIMEBody;
}}

PHP code:

$mail= new PHPMailer_mine();
//code to handle phpmailer
$result=$mail->Send();
if ($result) {
    $mail_string=$mail->get_mail_string();
    imap_append($ImapStream, $folder, $mail_string, "\\Seen");
}

It works well.

Aruwimi answered 23/12, 2011 at 11:41 Comment(1)
Can you give an example for values of $ImapStream and $folder? I am unable to figure out what should be the correct value for $folder, especially when I do not have file level access to mail server (I am using with Gmail).Exquisite
J
5

Well, it's pretty difficult, but can be done.

Take a look at the imap-append function.
By being connected to an IMAP stream resource, you can use the imap-append() to append your mails to the Sent folder of your IMAP account.

But reading through the comments will show you that it's a bit tedious to accomplish, but certainly not impossible - you'll probably need to code something on your own, since phpmailer doesn't support this out of the box (and will most likely be too time consuming to implement instead of making something yourself).

Jabber answered 19/12, 2011 at 13:12 Comment(2)
Ugh, really? Still, I suppose it a solution, albeit a work around, so I shall give you one of these delicious +1s.Acceleration
@Acceleration I agree with you, it's not pretty, but it's possible to solve the issue OP has; although I don't understand the need...Jabber
E
4

This works well : Php Manual

if (!$mail->send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
} else{

//echo "Message sent!";
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
if (save_mail($mail)) {
echo "Message saved!";
}
}

//function
function save_mail($mail)
{
$providerMail = 'Gmail';
$providerMailSentFolder = 'Sent Mail';//You can change 'Sent Mail' to any folder
$providerMailImap = 'imap.gmail.com';//imap.one.com
$path = "{".$providerMailImap.":993/imap/ssl}[".$providerMail."]/".$providerMailSentFolder;
//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);
}
Extragalactic answered 26/9, 2019 at 1:8 Comment(0)
A
2
  • You need to be relaying your sent mail through the IMAP host
  • The IMAP host needs to support the feature (which very few do)

If either of these two points are not true, the short answer is "You can't". In short, really it's down to the mail provider, not your code.

As much as I hate M$, Exchange is one place where they really have got things right - if you are using an Exchange server, all of this is handled for you.

Acceleration answered 19/12, 2011 at 13:4 Comment(0)
A
0

Probably the simplest solution I have found which works on every environment is to just add your sender's email address to the BCC when sending the mail, then you will get a copy of all the messages sent out from that script.

Austronesian answered 22/3 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.