Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

perl socket mail

by hweefarn (Acolyte)
on Dec 20, 2003 at 09:51 UTC ( [id://316023]=perlquestion: print w/replies, xml ) Need Help??

hweefarn has asked for the wisdom of the Perl Monks concerning the following question:

hi everybody :)
recently i am learning about perl socket programming. i found a tutorial and sample about using perl to send email (through socket). i try to run that script but not successful. then i try to debug it but there are something that i dont understand. so i decide to ask you all :)

here is part of the sample code that i dont understand:

use Socket; use strict; my($mailTo) = 'me@here'; my($mailServer) = 'host.there'; my($mailFrom) = 'me@here'; my($realName) = "Ralph Martin"; my($subject) = 'Test'; my($body) = "Test Line One.\nTest Line Two.\n"; $main::SIG{'INT'} = 'closeSocket'; my($proto) = getprotobyname("tcp") || 6; my($port) = getservbyname("SMTP", "tcp") || 25; my($serverAddr) = (gethostbyname($mailServer))[4]; if (! defined($length)) { die('gethostbyname failed.'); } socket(SMTP, AF_INET(), SOCK_STREAM(), $proto) or die("socket: $!"); $packFormat = 'S n a4 x8'; # Windows 95, SunOs 4.1+ #$packFormat = 'S n c4 x8'; # SunOs 5.4+ (Solaris 2) connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr)) or die("connect: $!");


i dont understand about $mailserver, $proto, $port, $serverAddr and $packFormat.

the explaination that i get from the web are as below, but i'm still abit confused.

-Initialize $mailServer which holds the symbolic name of your mail server.
-Get the protocol number for the tcp protocol and the port number for the smtp service. Recall: Windows 95 and NT do not implement the getprotobyname()or getservbyname() functions so default values are supplied.
-Initialize $serverAddr with the mail server's Internet address.
-Initialize $packedFormat with format specifiers.

my questions are:
1. is $mailserver means my computer name? or the hostname of the gateway of my LAN? or something like ""?
2. why do i need the tcp protocol? i want to send email, so i only need smtp, right?
3. is $serverAddr means the ip address of my pc? or the ip address of the gateway? or else?
4. what is $packedFormat means? it is for what?

that's all my questions. if u need more information about the scripts for sending email using socket, u can go to

ok, thank you very much :)


20031220 Edit by Corion: Changed formatted code to CODE tags

20031221 Edit by jeffa: Aliased email addresses

Replies are listed 'Best First'.
Re: perl socket mail
by tachyon (Chancellor) on Dec 20, 2003 at 10:50 UTC

    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 my $forged_from = ''; my $from = ''; my $to = ''; my $msg = "Subject: Subject\n\nHello me!\n\nNew Line\nMore Lin +es\n"; my $server = ''; # 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 ESMTP Sendmail 8.11.6/8.11.6; Sat, + 20 Dec 2003 21:39:21 +1100 (EST) Sent: HELO Recv: 250 Hello [203.220.x], pleased to meet you Sent: MAIL FROM: Recv: 250 2.1.0 Sender ok Sent: RCPT TO: Recv: 250 2.1.5 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.



Re: perl socket mail
by atcroft (Abbot) on Dec 20, 2003 at 10:40 UTC

    I think the answers to some of your questions are:

    1. $mailserver will be the mailserver you will be sending thru, such as or something.
    2. TCP is the underlying protocol used for the data transmission (as opposed to UDP or ICMP), whereas SMTP is the application protocol.

    I *believe* (but am not for 100% sure) that $serverAddr is the address of the server you will be connecting to. As to the pack format, this may be of help. In the code, it appears as if it is being used in the initial connection (although I am not certain).

    Hope that helps (a little, at least).

Re: perl socket mail
by jeffa (Bishop) on Dec 21, 2003 at 05:22 UTC
    2 pieces of advice:
    1. If you want to really learn about network programming in Perl, get Network Programming with Perl and read/program away. It's worth every penny.
    2. If you just want to send an email, or more importantly, you are seriously going to need to plug email submissions in your CGI app, then use MIME::Lite. It's as easy as: (straight from the docs)
      my $msg = MIME::Lite->new( From =>'', To =>'', Cc =>',', Subject =>'Helloooooo, nurse!', Data =>"How's it goin', eh?" ); $msg->send;
      Really. Raw socket programming is worth learning, but after you do learn how to unclog the pipes to your toliet, do you really want to keep Doing It The Hard Way? ;)


    (the triplet paradiddle with high-hat)
Re: perl socket mail
by pg (Canon) on Dec 20, 2003 at 17:18 UTC

    I don't think that any direct answer would actually help you much, as I can see you are really new to network communication. No Perl book can clearly help you on this, and I strongly suggest you to read some books about network, its layered architecture, and its main protocols in general.

    Learning takes time, don't rush, try to make the foundation solid.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://316023]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-06-20 19:30 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.