Add mail to Send folder with SwiftMailer
Asked Answered
C

2

7

I'm using SwiftMailer for PHP from swiftmailer.org

Everything works well but I wonder if there is a way to add the sent message into the sent folder from the mail account that SwiftMailer is sending from?

That's all, have a nice day.

Comanchean answered 21/10, 2013 at 14:41 Comment(0)
M
13

According to the developer, swiftmailer cannot copy to Sent folder because it is a mail sender and not mailbox manager.

As mentioned on the github page:

Swiftmailer is a library to send emails, not to manage mailboxes. So, this is indeed out of the scope of Swiftmailer.

However, someone from php.net posted a solution that might work for you:

  1. Use SwiftMailer to send the message via PHP.

    $message = Swift_Message::newInstance("Subject goes here");
    // (then add from, to, body, attachments etc)
    $result = $mailer->send($message);
    
  2. When you construct the message in step 1) above save it to a variable as follows:

    $msg = $message->toString(); 
    //  (this creates the full MIME message required for imap_append()!!
    //  After this you can call imap_append like this:
    imap_append($imap_conn,$mail_box,$msg."\r\n","\\Seen"); 
    
Mortgage answered 21/10, 2013 at 14:59 Comment(0)
M
8

I've had similar problem and Sutandiono's answer got me into right direction. However because of completeness and as I had additional problem connecting to an Exchange 2007 server, I wanted to provide a complete snippet for storing message to Sent folder on IMAP:

$msg = $message->toString();
// $message is instance of Swift_Message from SwiftMailer

$stream = imap_open("{mail.XXXXX.org/imap/ssl/novalidate-cert}", "username", "password", null, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));
// connect to IMAP SSL (port 993) without Kerberos and no certificate validation

imap_append($stream,"{mail.XXXXX.org/imap/ssl/novalidate-cert}Sent Items",$msg."\r\n","\\Seen");
// Saves message to Sent folder and marks it as read

imap_close($stream);
// Close connection to the server when you're done

Replace server hostname, username and password with your own information.

Mcarthur answered 10/3, 2014 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.