Well, first of all, before the MAIL FROM, you need to say something like
HELO hostname
This is the proper way to start the SMTP conversation.
I tried with sendmail as the mail server and got your script to work (after adding the HELO) either with \n or with \r\n.
Note also that you should fit your email message with some minimal headers to make it compliant with the RFC-822 syntax, and insure that your @'s are kept that way by escaping them. The code below does this:
#!/usr/bin/perl
use IO::Socket;
$socket=IO::Socket::INET->new(PeerAddr => "mail.your.net",
PeerPort => "25",
Proto => "tcp"
) or die "MAIL: $!";
print $socket "HELO your_hostname\r\n";
print $socket "MAIL FROM: <you\@your.domain>\r\n";
print $socket "RCPT TO: <them\@their.domain>\r\n";
print $socket "DATA\r\n";
print $socket "From: <you\@your.domain>\r\n";
print $socket "To: <them\@their.domain>\r\n";
print $socket "Subject: My automatic message\r\n";
print $socket "\r\n";
print $socket "Your data here\r\n";
print $socket ".", "\r\n";
print $socket "QUIT", "\r\n";
Finally, note that you're not really performing an SMTP dialog with the server, as mandated by the protocol. You're simply saying your part alone and as fast as you can. There is no error checking at all. This makes this approach unreliable.
In order to properly handle potential problems with the message, you could read a line after you write each part. An easy way to do this could be:
#!/usr/bin/perl
use IO::Socket;
$socket=IO::Socket::INET->new(PeerAddr => "mail.your.net",
PeerPort => "25",
Proto => "tcp"
) or die "MAIL: $!";
my $line = 'No line';
die "Error in greeting: $line"
unless ($line = scalar <$socket>) =~ /^2/;
print $socket "HELO your_hostname\r\n";
die "Error in HELO: $line"
unless ($line = scalar <$socket>) =~ /^2/;
print $socket "MAIL FROM: <you\@your.domain>\r\n";
die "Error in MAIL FROM: $line"
unless ($line = scalar <$socket>) =~ /^2/;
print $socket "RCPT TO: <them\@their.domain>\r\n";
die "Error in RCPT TO: $line"
unless ($line = scalar <$socket>) =~ /^2/;
print $socket "DATA\r\n";
die "Error in DATA: $line"
unless ($line = scalar <$socket>) =~ /^3/;
print $socket "From: <you\@your.domain>\r\n";
print $socket "To: <them\@their.domain>\r\n";
print $socket "Subject: My automatic message\r\n";
print $socket "\r\n";
print $socket "Your data here\r\n";
print $socket ".", "\r\n";
die "Error in DOT: $line"
unless ($line = scalar <$socket>) =~ /^2/;
print $socket "QUIT", "\r\n";
die "Error in QUIT: $line"
unless ($line = scalar <$socket>) =~ /^2/;
Which is not complete code, but would give you a good idea about how to move forward. Keep in mind that this example does not handle hairy cases such as multi-line greetings, which you can find in the real world.
For a more complete solution, I would seek to use CPAN modules such as the ones my felow monks have suggested. It is often wise to challenge the restrictions in favor of not reinventing the wheel and using code that has been well tested and widely used by other people.
Regards.
|