forwarding multipart messages

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
gmorehoudh
Forum Commoner
Posts: 50
Joined: Tue Mar 04, 2008 1:49 pm

forwarding multipart messages

Post 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.
gmorehoudh
Forum Commoner
Posts: 50
Joined: Tue Mar 04, 2008 1:49 pm

Re: forwarding multipart messages

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: forwarding multipart messages

Post 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, .... );
gmorehoudh
Forum Commoner
Posts: 50
Joined: Tue Mar 04, 2008 1:49 pm

Re: forwarding multipart messages

Post 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.
Post Reply