Web Hosting Talk







View Full Version : SMTP Technicalities and Perl


Lawrence
06-22-2001, 10:13 PM
This is a rather technical question that I hope someone has an answer to. I've been writing a Perl program using the Net::SMTP module to communicate with SMTP servers. I've also been looking at the standards in RFC2821 (http://www.faqs.org/rfcs/rfc2821.html) and RFC822 (http://www.faqs.org/rfcs/rfc822.html). I would assume that these are the same or similar to what are used today.

Now, once connected to an SMTP server, you first send a MAIL command with the sender's address. You then send a series of RCPT commands with each of the receivers' e-mail addresses. After this, you send a stack of data that makes up the e-mail message.

Now, in that data you have the standard headers (info in RFC822, above). These are your standard To, From, Subject headers etc that you often use with sendmail in Perl scripts (and that you often use for a lot of other things of course). For example:

From: me@here.com
To: me@there.com
Subject: Hello

Each of the addresses in the To header is supposed to be converted into a RCPT command.

Now, the problem is this. When I send, the SMTP requires that I send a MAIL and RCPT command before rattling off with the From and To headers. So I send MAIL, send RCPT, then send my headers, but the headers override what I sent initially in MAIL and RCPT (so what was in To overrides RCPT, and what was in From overrides MAIL).

I've got no problem with that, in fact I prefer it that way (because I want to send all the data in headers rather than in separate MAIL and RCPT commands). The thing is that the initial commands seem necessary, but pointless, as they just get overridden.

Does anyone know of the technicalities of this? Are MAIL and RCPT supposed to be overridden, or am I missing something? Are they not actually overridden but serve another purpose?

DavidU
06-23-2001, 02:51 AM
net::smtp takes care of this for you...

however....

use mime::lite instead -- it can handle attachments.

-davidu

Lawrence
06-23-2001, 05:12 AM
It doesn't quite seem to take care of it David. I've been something along these lines:

$smtp->mail("me\@here.com");
$smtp->recipient("me\@there.com");
$smtp->data();
$smtp->datasend("From: me2\@here.com\nTo: me2\@there.com\nSubject: Testing Perl Net::SMTP module\n\nThis is just a test message.\n\n");
$smtp->dataend();
$smtp->quit;

And the final message is sent to me2@there.com from me2@here.com, rather than from me@here.com to me@there.com. If I don't use "mail" and "recipient" the debug information tells me something like "Unrecognized command From:"

$smtp->mail of course is what sends the MAIL command, $smtp->recipient sends the RCPT command.

I need to use a standard distribution Perl module, and as far as I'm aware Net::SMTP is the only standard one that can do this. mime::lite isn't standard is it?

Mike the newbie
06-23-2001, 06:56 AM
Originally posted by Lawrence
...

Now, the problem is this. When I send, the SMTP requires that I send a MAIL and RCPT command before rattling off with the From and To headers. So I send MAIL, send RCPT, then send my headers, but the headers override what I sent initially in MAIL and RCPT (so what was in To overrides RCPT, and what was in From overrides MAIL).

I've got no problem with that, in fact I prefer it that way (because I want to send all the data in headers rather than in separate MAIL and RCPT commands). The thing is that the initial commands seem necessary, but pointless, as they just get overridden.

Does anyone know of the technicalities of this? Are MAIL and RCPT supposed to be overridden, or am I missing something? Are they not actually overridden but serve another purpose?


The way it works allows for, among other things, blind copies.

DavidU
06-23-2001, 07:24 PM
Originally posted by Lawrence
It doesn't quite seem to take care of it David. I've been something along these lines:

$smtp->mail("me\@here.com");
$smtp->recipient("me\@there.com");
$smtp->data();
$smtp->datasend("From: me2\@here.com\nTo: me2\@there.com\nSubject: Testing Perl Net::SMTP module\n\nThis is just a test message.\n\n");
$smtp->dataend();
$smtp->quit;

And the final message is sent to me2@there.com from me2@here.com, rather than from me@here.com to me@there.com. If I don't use "mail" and "recipient" the debug information tells me something like "Unrecognized command From:"

$smtp->mail of course is what sends the MAIL command, $smtp->recipient sends the RCPT command.

I need to use a standard distribution Perl module, and as far as I'm aware Net::SMTP is the only standard one that can do this. mime::lite isn't standard is it?

Well, Mime::Lite might not be as standard as net::smtp but it is faster, more secure, newer, better, and is MUCH easier to use...also, chances are that you have it.

Here is a quick mime::lite script to show you how easy it is:


$mesg = new MIME::Lite
From =>'admin@everydns.net',
To =>'client@userdomain.com',
Subject =>"EveryDNS support ticket",
Type =>'multipart/mixed';
attach $mesg
Type =>'TEXT',
Data =>"Attached is an everydns mysql dump from $now\n\n--root\n";
attach $mesg
Type =>'text/plain',
Path =>"/secure/sql_backups/$now.sql-dump",
Filename=>"$now.sql-dump";
$mesg->send;


This sends a note with a small body and an attachment.

It is reall just that easy....if you NEED a net::smtp example I can give you that too. ;-)

-davidu

Lawrence
06-23-2001, 09:16 PM
Thanks for your ideas, but the script is not just for myself, so I need to stick with standard modules that everyone with Perl will have. Net::SMTP does exactly what I want it to do, and it's standard, so that's what I'll have to use. All I really need to know is why those mail and recipient calls seem to get overridden by the headers, or if they serve a somewhat "hidden" purpose. I don't want to be sending ghost e-mails off to server admins or something like that!

I've got all the instructions I need for Net::SMTP from perldoc, and have been successfully sending e-mails with it. All I'm really after is an explanation for the apparent useless but compulsory mail and recipient calls when you're using To: and From: headers.

DavidU
06-24-2001, 02:10 AM
Originally posted by Lawrence
Thanks for your ideas, but the script is not just for myself, so I need to stick with standard modules that everyone with Perl will have. Net::SMTP does exactly what I want it to do, and it's standard, so that's what I'll have to use. All I really need to know is why those mail and recipient calls seem to get overridden by the headers, or if they serve a somewhat "hidden" purpose. I don't want to be sending ghost e-mails off to server admins or something like that!

I've got all the instructions I need for Net::SMTP from perldoc, and have been successfully sending e-mails with it. All I'm really after is an explanation for the apparent useless but compulsory mail and recipient calls when you're using To: and From: headers.

I am trying to explain to you...

MIME::lite is becoming more and more standard EVERY DAY. It's better to use the new then the old. As to answering your Net::SMTP question...I'll look it up and post your answer.

-davidu

Lawrence
06-24-2001, 07:18 AM
Thanks David, I do appreciate your help.

Yes, I am aware that Mime::Lite is becoming more and more standard (in fact, it's probably used more than Net::SMTP these days), I'll take your word that it's faster, more secure and better, and from your example it certainly looks easier and more feature-full. But unfortunately, it's not yet standard enough for my purposes, and unfortunately that rules it out as an option.

Thanks in advance for looking up the question. I checked the RFCs (as in original post) and the perldoc for Net::SMTP, and couldn't find a thing. Any other ideas as to where to look for these things?

Mike the newbie
06-24-2001, 09:01 AM
Originally posted by Lawrence
....All I really need to know is why those mail and recipient calls seem to get overridden by the headers, or if they serve a somewhat "hidden" purpose. I don't want to be sending ghost e-mails off to server admins or something like that!...



The don't really get overwritten by the header info (or at least they shouldn't).

The mail and recipient values are used by the SMTP server you connect to for purposes of identifying you (mail) and indentifying who you are sending the message to (recipient).

The To: and From: in the header are used by the recipient's email client to fill in the To: and From: fields when the message is displayed. Any value you put in the To: field will show up to everyone who receives the message. So if you have a bunch of people who are to receive the same message, yet you do not want anyone to see anyone else on the To: list, you can do something like the following message excerpt:

RCPT: ppl1@example.com ppl2@example.com ppl3@example.com

To: MikeTheNewbie@example.com

When ppl1, ppl2 and ppl3 receive the message, they will not see each other's email address in the To field, they will see MikeTheNewbie@example.com in the To: field.



Take a peek here http://www.freesoft.org/CIE/RFC/821/4.htm for some more info (and a better explanation).

Lawrence
06-24-2001, 08:58 PM
Thanks Mike, I think I'm starting to piece this one together.

I think we may have a different understanding of how the headers work, however. My understanding was that any e-mail addresses in the To: header would be broken up and sent as additional RCPT headers, rather than only masking the original RCPT headers. This may, however, be something that the Perl Net::SMTP module does, rather than something in the actual protocol (although I would think it unlikely).

I think I might just have to solve this one with some extensive testing.

Mike the newbie
06-24-2001, 09:30 PM
Originally posted by Lawrence
Thanks Mike, I think I'm starting to piece this one together.

I think we may have a different understanding of how the headers work, however. My understanding was that any e-mail addresses in the To: header would be broken up and sent as additional RCPT headers, rather than only masking the original RCPT headers. This may, however, be something that the Perl Net::SMTP module does, rather than something in the actual protocol (although I would think it unlikely).

I think I might just have to solve this one with some extensive testing.



If the perl module is working as you described, then it is not RFC 821 (http://www.faqs.org/rfcs/rfc821.html) compliant. Given the free-form nature of the perl environment, that may very well be the case.


Happy spelunking!! :)

Lawrence
06-25-2001, 03:04 AM
Mike: Happy spelunking!!

That's a very nice description of what it will be!

Lawrence
07-05-2001, 10:28 AM
It's been a little while, but I managed to get around to sorting this one out - with a little more reading and a lot more testing (this time on a different SMTP server). Mike was right, to the dot! :cool:

The MAIL and RCPT headers specify the sender and actual recipients. The headers do nothing more than fill in the fields in a user's email client.

I think I may have gotten the impression of the To header being split up into RCPT commands from an obsolete version of one of the RFCs (from memory, perhaps I used RFC821 when it is superceded by RFC2821 or something like that).

Mike the newbie
07-05-2001, 06:06 PM
Originally posted by Lawrence
It's been a little while, but I managed to get around to sorting this one out - with a little more reading and a lot more testing (this time on a different SMTP server).



Thanks for the follow-up.