http://qs1969.pair.com?node_id=480570

chrisj0 has asked for the wisdom of the Perl Monks concerning the following question:

HI all,
I have about 600-700 email in maildir accounts of a dozen users I have to resend to another account. Some have attachments some are html and still some are just plain text. I've used MIME::Parser,Entity,Body,...etc... to parse emails and save attachments write log files etc... But I've never had to do a forward like this.

I basically have to send the email so it looks as close to the original as possible. All attachments must be correct and intact the from address and reply to address should be the same and the only difference should be the To: address.

Is there a module that will do all of this. Or should I use the parser and then a smtp module? Will the body of a parsed email preserve the attachment when resend it?
Any help would be great.

Thanks,
chrisj

Replies are listed 'Best First'.
Re: forwarding emails with perl??
by gellyfish (Monsignor) on Aug 03, 2005 at 17:55 UTC

    You can do this with Email::Folder and Email::Send quite easily:

    use strict; use warnings; + use Email::Send; use Email::Folder; + my %received; my $box = Email::Folder->new('/home/jonathan/mail/Recruiters/'); while ( my $mail = $box->next_message ) { + $mail->header_set('To','jonathan@localhost'); send SMTP => $mail->as_string(), 'localhost'; }
    Of course fixing some of the values as appropriate.

    /J\

      thanks, I'll give this a try and let you know.

      I have been looking at this for some time and need to handle email received from stdin via a pipe and bounce it somewhere else.

      Can I just plug this in somehow into the Email::Folder and Email::Send stream?

        In the case of a single message you would be better off modifying the above to slurp the message off the STDIN into a scalar and pass that to the constructor of Email::Simple, then do much the same as the above

        /J\

      thanks again gellyfish..
      I've written the code similar to the way you show. I added some changes to the header modification to ensure that replies and replies to all work, and that the CC addresses in the message aren't copied on the remailed message.
      This works great and was very easy.

      I've been looking at the module and I don't see a way to only select new messages? I've tried giving Email::Folder->new only the new folder but the module complains that it's not an IMAP directory.
      Is there a way to only process new messages? I'd like to add a command line option to only remail new messages rather than the entire inbox. Would I have to use another module to do this?
      Any suggestions would be great
      TIA,
      chrisj

Re: forwarding emails with perl??
by jhourcle (Prior) on Aug 03, 2005 at 17:49 UTC

    You don't want to modify the body of the message at all. Use the existing content, completely unmodified (unparsed, etc, other than seperated into messages) as the message body, and use the new envelope-from and envelope-to in the SMTP transaction.

    There is no reason to attempt to parse the message, unless there's some sort of information that you need to use to determine where to route the mesage to, if you really want it to be as close to the original as possible.

    Now we come to the question -- do you really want it close to the original? If the mail program that's reading the messages sorts on the message's date, and not the received time of the message, this could cause some odd effects, as they might not realize that you've just inserted a couple hundred messages that were dated 3 years ago into their mailbox. Most modern mail readers don't have this problem, as they default to using the received time for their date sorting, but it's possible that this might be an issue.

    Update: If people are going to suggest non-perl solutions, I might as well, too ... using pine, enable aggregate commands, and bounce command, then ;aab (or well, whatever selection criteria for the folder)

Re: forwarding emails with perl??
by sgifford (Prior) on Aug 03, 2005 at 20:51 UTC
    If you don't mind a non-perl solution, maildirsmtp from the serialmail package is designed to do nearly exactly what you want.
Re: forwarding emails with perl??
by cajun (Chaplain) on Aug 04, 2005 at 04:37 UTC
Re: forwarding emails with perl??
by shiza (Hermit) on Aug 03, 2005 at 23:04 UTC
    Could you not do this with sendmail?