Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here is a working example in Perl. Read RFC 821 for the protocol. Unless you are familiar with socket.h and socket.c I would suggest using IO::Socket::INET as it is much more intuitive to use. Note Net::SMTP is a much better option than code like this simple example which is just to show how it works.

#!/usr/bin/perl -w use strict; use IO::Socket::INET; # see RFC 821 http://www.ietf.org/rfc/rfc0821.txt my $forged_from = 'hotmail.com'; my $from = 'nobody@hotmail.com'; my $to = 'smtp_test@hotmail.com'; my $msg = "Subject: Subject\n\nHello me!\n\nNew Line\nMore Lin +es\n"; my $server = 'xxxx.xxxx.net.au'; # SMTP SERVER my $port = 25; my $timeout = 10; my $sock = IO::Socket::INET->new( PeerAddr => $server, PeerPort => $port, Proto => 'tcp', Timeout => $timeout, ); unless ( $sock ) { die"ERR - Could not connect socket to $server on port $port"; } if ( my $server = <$sock>) { print "Got Handshake: $server"; dialog( $sock, "HELO $forged_from\015\012" ); dialog( $sock, "MAIL FROM: $from\015\012" ); dialog( $sock, "RCPT TO: $to\015\012" ); dialog( $sock, "DATA\015\012" ); dialog( $sock, "$msg\015\012.\015\012" ); dialog( $sock, "QUIT\015\012" ); } else { die "No handshake sent from SMTP server\n" } sub dialog { my ( $sock, $send ) = @_; print $sock $send; print "Sent: $send"; my $receive = <$sock>; die "Got no response to $send" unless $receive; print "Recv: $receive"; } __DATA__ Got Handshake: 220 xxxx.xxxx.net.au ESMTP Sendmail 8.11.6/8.11.6; Sat, + 20 Dec 2003 21:39:21 +1100 (EST) Sent: HELO hotmail.com Recv: 250 xxxx.xxxx.net.au Hello blah.blah.blah.blah.com.au [203.220.x +xx.xxx], pleased to meet you Sent: MAIL FROM: nobody@hotmail.com Recv: 250 2.1.0 nobody@hotmail.com... Sender ok Sent: RCPT TO: smtp_test@hotmail.com Recv: 250 2.1.5 smtp_test@hotmail.com... Recipient ok Sent: DATA Recv: 354 Please start mail input. Sent: Subject: Subject Hello me! New Line More Lines . Recv: 250 Mail queued for delivery. Sent: QUIT Recv: 221 Closing connection. Good bye.

cheers

tachyon


In reply to Re: perl socket mail by tachyon
in thread perl socket mail by hweefarn

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2023-05-28 22:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?