Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Resending a complete e-mail

by jplindstrom (Monsignor)
on Nov 04, 2002 at 14:02 UTC ( #210171=perlquestion: print w/replies, xml ) Need Help??

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

I need to send an e-mail.

It's a complete e-mail (with headers and a full body, most likely multipart with large attachments) popped from a mailbox. If certain things fail I'd like to resend it to another e-mail address, i.e. change the To: address and leave everything else as-is.

I've experimented a little with Mail::Sendmail and Mail::Sender, but they don't seem to play my game.

Any experience with this? Any ideas on how to do it?


Replies are listed 'Best First'.
Re: Resending a complete e-mail
by Jenda (Abbot) on Nov 04, 2002 at 16:21 UTC

    Since you already have the headers and the MIME stuff done you don't need the power of Mail::Sender or MIME::Lite, but the low-levelness of Net::SMTP.

    You should be able to change the To: address inside the email and then use just

    use Net::SMTP; $smtp = Net::SMTP->new('mailhost', Timeout => 60); $smtp->mail($from_address); $smtp->to($to_address); $smtp->data(); $smtp->datasend($the_email_with_headers); $smtp->dataend(); $smtp->quit;


      Ok, this is my standard rant about sending email from scripts, being a former system administrator of a mail system for 40.000+ users. If you are using this in anything else than a very trivial testing-only environment, 'mailhost' in Jenda's example above should be set to 'localhost' and you should run an SMTP daemon on your localhost. This is for reasons of error handling and redundancy. This is also true if you are running this in a cgi script on a webserver. You want to send the email as quickly as possible to be able continue with the cgi script, and sending the mail to localhost is a lot quicker than over the network.

      If you are not able to run an SMTP daemon on localhost, you can use it the way Jenda describes above, if mailhost is close to you, and under your control. This is a riskier solution, and will not take advantage of MX records (think sendmail 'smarterhost' set to an MX record of mailhost1, mailhost2 & mailhost3 (or, as in my case, virusscanner1, virusscanner2, virusscanner3...)).

      Anyway, always check that new creates a valid $smtp object. If you get undefined or something, the connection with mailhost could not be established, and you will have to deal with that.

      Don't even think about sending the mail directly to the recipient's mailhost. You won't be able to deal with spooling, error handling, DNS resolving and cacheing, MX records etc. as efficiently as sendmail (or other MTA's).

        Agreed. Thanks for writing this.

        I took the code directly from Net::SMTP's docs and only deleted a few lines and changed it a little.

        I think it's all too common that even the most well known modules miss any error checking in the examples included in the docs. :-(
        Sorry I did not add it ;-)


      Thanks! This was exactly what I needed.


Re: Resending a complete e-mail
by Chady (Priest) on Nov 04, 2002 at 14:59 UTC

    still one more thing to experiment in.. the MIME::* family and the lightweight MIME::Lite.

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady |
      I second that. Need to send mail with attachments? MIME::Lite. It's an automatism with me.

      Update: As you don't have to construct a MIME message, but only have to resend the mail to another address, that doesn't really apply to this case, does it? OK, second choice: Net::SMTP. As somebody else already wrote: you don't have to alter anything in the message, not even the To: header. SMTP doesn't parse the message, either. The recipients are set using the to() method, the message itself, headers and body, can be sent using data().

      You do need a live connection to an SMTP server, and it requires you to set some custom values, such as the server, your own domain, and so on. Most servers don't accept mail just from anybody or to anybody, so it could be tricky to get it to work. And it might pose some restrictions on the acceptable email address format.

Re: Resending a complete e-mail
by vek (Prior) on Nov 04, 2002 at 15:39 UTC
    Usually I advocate the sendmail route if you are on a *nix box. Just recently however I've had the pleasure of creating Perl programs that run on Windows desktops. In order for those boxes to send out e-mails I've had some great success using MIME::Lite and thoroughly recommend it.

    -- vek --
Re: Resending a complete e-mail
by strider corinth (Friar) on Nov 04, 2002 at 15:43 UTC
    Another good module to check out is Net::SMTP. It's a pretty much copy-and-paste operation to resend mail with it.

    Love justice; desire mercy.
Re: Resending a complete e-mail
by hiseldl (Priest) on Nov 04, 2002 at 15:00 UTC
Re: Resending a complete e-mail
by gjb (Vicar) on Nov 04, 2002 at 16:01 UTC

    The Mail::Box set of modules is very nice to do the kind of manipulations you want to perform, i.e. dissecting and reassembling emails.

    Hope this helps, -gjb-

Re: Resending a complete e-mail
by iburrell (Chaplain) on Nov 04, 2002 at 17:05 UTC
    You don't need to change the To: address. The SMTP destination address(es) can be completely different from the addresses listed in the header. That is how mailing lists (and spam) work, they resend the email with new recipient and return addresses but without changing the headers.
Re: Resending a complete e-mail
by mattr (Curate) on Nov 05, 2002 at 13:13 UTC
    If it's just one email, telnet to port 25.. :)

    Actually Net::SMTP::Server is interesting and with practice on it you will be able to EHLO with the best of 'em.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2022-06-28 17:47 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (92 votes). Check out past polls.