Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: perl socket mail

by tachyon (Chancellor)
on Dec 20, 2003 at 10:50 UTC ( [id://316028]=note: print w/replies, xml ) Need Help??


in reply to perl socket mail

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://316028]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-18 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found