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;
Jenda | [reply] [d/l] |
|
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).
| [reply] |
|
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 ;-)
Jenda
| [reply] |
|
Thanks! This was exactly what I needed.
/J
| [reply] |
Re: Resending a complete e-mail
by Chady (Priest) on Nov 04, 2002 at 14:59 UTC
|
| [reply] |
|
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.
| [reply] |
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
-- | [reply] [d/l] |
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.
| [reply] |
Re: Resending a complete e-mail
by hiseldl (Priest) on Nov 04, 2002 at 15:00 UTC
|
| [reply] |
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-
| [reply] |
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. | [reply] |
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. | [reply] |