Reading an email from an mbox file and resending to a differ

Greetings,

I’m trying to emulate the “Redirect” function of some email clients
but am not having much success. I am sending messages with
ActionMailer templates just fine so I’m pretty sure my environment is
set up correctly and that my mail server is accepting the messages to
relay.

I am reading messages out of an mbox file and parsing them into RMail
objects with RMail::Parser.read(). Then I’m storing some info about
the message as well as storing the a text version of the RMail object
in a field as the “raw” message (I get the text version of RMail using
to_s() )

At a different point in the program, I want to “resend” or “redirect”
the message to an external email address - but I want it to appear in
their mailbox unmodified from the original.

I thought the easiest way to do this would be to use Net::SMTP.start()
and pass it the “raw” message as well the from/to address for the SMTP
transaction (which shouldn’t affect the RFC822 headers in the original
message being delivered). I realized along the way that I need to
delete the first line from the mbox file that contains the From and
Date.

When I try this with simple text emails it seems to work. But when I
try to attach MIME emails it seems to get confused and creates a new
set of RFC822 headers and puts the entire MIME message (including its
RFC822 headers) into the body of the new message that is sent.

  smtp.send_message raw_mail, "[email protected]", "[email protected]"

Any pointers? Is there an easier way to resend a raw RFC822 message
than Net::SMTP.start() ? Any idea what could be wrong with my message
formatting?

Thanks!

~Josh


Here is a sample of the “raw” output of a message (truncated)

Return-Path: [email protected]\nX-Original-To: [email protected]
\nDelivered-To: [email protected]\nReceived: from ms-smtp-04.xxx.rr.com
(ms-smtp-04.xxx.rr.com [xx.xx.47.43])\n\tby mail.xxx.com (Postfix)
with ESMTP id 15CE495A92\n\tfor [email protected]; Sat, 14 Jul 2007
12:37:18 -0500 (CDT)\nReceived: from [10.0.1.5] (cpe-
xxx-157-199.xxx.xxx.rr.com [xx.xx.157.199])\n\tby ms-
smtp-04.xxx.rr.com (8.13.6/8.13.6) with ESMTP id l6EHbCjV027495\n\tfor
[email protected]; Sat, 14 Jul 2007 12:37:13 -0500 (CDT)\nResent-
Message-Id: [email protected]\nMime-
Version: 1.0 (Apple Message framework v752.2)\nContent-Type: multipart/
alternative; boundary=Apple-Mail-17–30998656\nResent-Date: Sat, 14
Jul 2007 12:37:11 -0500\nMessage-Id:
[email protected]\nResent-To:
[email protected]\nFrom: Stanford Technology Ventures Program
[email protected]\nSubject: Educators Corner highlights -
July edition\nResent-From: Joshua B. [email protected]\nDate: Wed, 11
Jul 2007 04:35:13 -0700 (PDT)\nTo: Joshua B. [email protected]\nX-
Mailer: Apple Mail (2.752.2)\nX-Virus-Scanned: Symantec AntiVirus Scan
Engine\n\n\n–Apple-Mail-17–30998656\nContent-Transfer-Encoding: 7bit
\nContent-Type: text/plain;\n\tcharset=US-ASCII;\n\tdelsp=yes;\n
\tformat=flowed\n\nTrouble viewing this email? View it online\n
\nRecently Added Videos\n\nListed below are a few video clips of
recent speakers from the \nEntrepreneurial Thought Leaders Seminar.\n
\nIncentives for People in a Community-Based Organization\n\nMitchell
Baker, Mozilla Corporation - 2 min. 10 sec.\nMitchell Baker, CEO of
Mozilla, describes the richness in decision- \nmaking authority that
one is endowed with while working in a \ncommunity-based organization
like Mozilla. She talks about the