This is the proper way to start the SMTP conversation.HELO hostname
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:
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.#!/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";
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:
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.#!/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/;
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.
In reply to Re: ESMTP, linefeed and <CRLF>
by fokat
in thread ESMTP, linefeed and <CRLF>
by com_arkan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |