perlquestion
hweefarn
<p>hi everybody :)<br />
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 :)
<br>
<br>here is part of the sample code that i dont understand:
<code>
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: $!");
</code>
<br>*****************************************************
<p>i dont understand about $mailserver, $proto, $port, $serverAddr and $packFormat.
</p><p>
the explaination that i get from the web are as below, but i'm still abit confused.</p>
<br>
<br>-Initialize $mailServer which holds the symbolic name of your mail server.
<br>-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.
<br>-Initialize $serverAddr with the mail server's Internet address.
<br>-Initialize $packedFormat with format specifiers.
<br>
<br>
<br>my questions are:
<br>1. is $mailserver means my computer name? or the hostname of the gateway of my LAN? or something like "smtp.streamyx.com"?
<br>2. why do i need the tcp protocol? i want to send email, so i only need smtp, right?
<br>3. is $serverAddr means the ip address of my pc? or the ip address of the gateway? or else?
<br>4. what is $packedFormat means? it is for what?
<br>
<br>
<br>that's all my questions. if u need more information about the scripts for sending email using socket, u can go to http://www.cs.cf.ac.uk/Dave/PERL/node180.html
<br>
<br>
<br>ok, thank you very much :)
<br>
<br>
<br>regards,
<br>hweefarn
<p><small>20031220 Edit by [Corion]: Changed formatted code to CODE tags</small></p>
<p><small>20031221 Edit by [jeffa]: Aliased email addresses</small></p>