Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

forwarding emails with perl??

by chrisj0 (Acolyte)
on Aug 03, 2005 at 17:22 UTC ( #480570=perlquestion: print w/replies, xml ) Need Help??

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.


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.


      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


      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

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 shiza (Hermit) on Aug 03, 2005 at 23:04 UTC
    Could you not do this with sendmail?
Re: forwarding emails with perl??
by cajun (Chaplain) on Aug 04, 2005 at 04:37 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://480570]
Approved by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2023-03-28 14:28 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (67 votes). Check out past polls.