copying message to IMAP "sent" folder

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
whiskeyseven
Forum Newbie
Posts: 7
Joined: Mon Apr 16, 2007 11:03 am

copying message to IMAP "sent" folder

Post by whiskeyseven »

hi again all,

so i've got a webmail app, and after i fire the "send" command on my message, i'd like to copy it to INBOX.Sent automatically.

in the past i've used the php command "imap_append". however, since upgrading to swiftmailer, i'm not sure of the best way to form the content string for that function.

so far i've tried the following. $message is my Swift_Message

Code: Select all

imap_append($mailCon,$conString."INBOX.Sent",$message->headers->build()."\r\n".$message->getBody()."\r\n");
the code above DOES place a message in my sent folder. it has the correct subject and timestamp, as well as the content-type, mime version, and user-agent. however, it's missing the recipient and the actual message body.

any suggestions?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

First things first, Swift isn't being given the opportunity to filter/encode the message when you use getBody(). This function should help.

Code: Select all

function messageString(&$message)
{
  $stream =& $message->build();
  return $stream->readFull();
}

imap_append($mailCon,$conString."INBOX.Sent",messageString($message)."\r\n");
The message by itself doesn't contain the recipients. Swift adds these at runtime and then removes them again once done so that the message can be re-used. You need to set these manually.

Code: Select all

function messageString(&$message, &$recipientList)
{
  //Get original values
  $to = $message->getTo();
  $cc = $message->getCc();
  $bcc = $message->getBcc();
  //Set whatever is in RecipientList instance
  $message->setTo($recipients->getTo());
  $message->setCc($recipients->getCc());
  $message->setBcc($recipients->getBcc());
  //Build the message
  $stream =& $message->build();
  //Restore the values again
  $message->setTo($to);
  $message->setCc($cc);
  $message->setBcc($bcc);
  //Return the built message
  return $stream->readFull();
}
If you're using the Swift_Message class entirely for MIME purposes without actually sending messages with Swift you would realisitically just setTo(), setCc() and setBcc() yourself anyway :)
Post Reply