Page 1 of 1

forwarding multipart messages

Posted: Thu Mar 06, 2008 4:56 pm
by gmorehoudh
Is there an easy way to forward multipart messages out of an IMAP mailbox using Swift? Right now I'm constructing a new Message using the raw body as retrieved by imap_body(). I'm also attempting to retrieve the Content-Type header from the message in the IMAP box and set the Content-Type header on the Swift Message to the same string using Message->headers->set(), but my string is getting encoded and having a different charset tacked on the end of it, as well as format=flowed. So, it looks like this:

Content-Type: =?iso-8859-1?B?b250ZW50LVR5cGU6IG11bHRpcGFydC9taXhlZDsg?=
=?iso-8859-1?B?Ym91bmRhcnk9QXBwbGUtTWFpbC0yNS05NDY3NTM2MzcN?=;
charset=iso-8859-1; format=flowed

Am I going to have to parse the original Content-Type and set it and its attributes piecemeal with Message->headers->set() followed by Message->headers->setAttribute()? Will this even work for what I'm trying to do (forward a message with the content essentially unchanged except for From, Sender and Return-Path headers)?

I'd really love to not have to parse the entire MIME structure and rebuild a Message from scratch in order to resend it, considering I have the already-packaged MIME message already for free.

Re: forwarding multipart messages

Posted: Thu Mar 06, 2008 7:08 pm
by gmorehoudh
Okay, I've got it working on a single multipart test e-mail for starters. :P

Code: Select all

 
        $message =& new Swift_Message($subject, $rawbody, $content_type);
 
        // copy the original content-type attributes over
        foreach($content_type_attributes as $attr => $val) {
            $message->headers->setAttribute("Content-Type", trim($attr), trim($val));
        }
 
$content_type contains the mime type from the original header, $content_type_attributes is an associative array of the attributes from the original header.

Of course this is still sticking a default charset in, as well as format=flowed, but it's a lot more functional than what I had when I first posted.

Tips appreciated.

Re: forwarding multipart messages

Posted: Thu Mar 06, 2008 8:44 pm
by Chris Corbyn
You can attach a message to forward it. This is how thunderbird does it.

NOTE: $originalMessage is the entire message including all headers (unmodified).

Code: Select all

$message = new Swift_Message('FW: Whatever');
$message->attach(new Swift_Message_Part("Here's a forwarded message"));
$message->attach(new Swift_Message_Attachment($originalMessage, 'Whatever.eml', 'message/rfc822'));
 
$swift->send($message, .... );

Re: forwarding multipart messages

Posted: Fri Mar 07, 2008 11:35 am
by gmorehoudh
I should clarify this isn't a 'normal' forward, I'm building a little app that resends e-mails out of an IMAP box to a distribution list, so I don't want them to appear as an attachment. Is what I described above the best way to do it?

Edit: I'm trying some other test messages now and finding that Swift is re-encoding the raw body (for example, one original message was quoted-printable and the resulting message sent out of swift is double-encoded quoted-printable). So I'm digging through the API docs looking for a way to stop/circumvent this.