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.


In reply to Re: ESMTP, linefeed and <CRLF> by fokat
in thread ESMTP, linefeed and <CRLF> by com_arkan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.