⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch18.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
socket(SMTP, AF_INET(), SOCK_STREAM(), $proto)    or die(&quot;socket: $!&quot;);$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(&quot;connect: $!&quot;);select(SMTP); $| = 1; select(STDOUT);    # use unbuffered i/o.{    my($inpBuf) = '';    recv(SMTP, $inpBuf, 200, 0);    recv(SMTP, $inpBuf, 200, 0);}sendSMTP(1, &quot;HELO\n&quot;);sendSMTP(1, &quot;MAIL From: &lt;$mailFrom&gt;\n&quot;);sendSMTP(1, &quot;RCPT To: &lt;$mailTo&gt;\n&quot;);sendSMTP(1, &quot;DATA\n&quot;);send(SMTP, &quot;From: $realName\n&quot;, 0);send(SMTP, &quot;Subject: $subject\n&quot;, 0);send(SMTP, $body, 0);sendSMTP(1, &quot;\r\n.\r\n&quot;);sendSMTP(1, &quot;QUIT\n&quot;);close(SMTP);sub closeSocket {     # close smtp socket on error    close(SMTP);    die(&quot;SMTP socket closed due to SIGINT\n&quot;);}sub sendSMTP {    my($debug)  = shift;    my($buffer) = @_;    print STDERR (&quot;&gt; $buffer&quot;) if $debug;    send(SMTP, $buffer, 0);    recv(SMTP, $buffer, 200, 0);    print STDERR (&quot;&lt; $buffer&quot;) if $debug;    return( (split(/ /, $buffer))[0] );}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE><B>&gt; HELO</FONT></B><FONT SIZE=2 FACE="Courier New">&lt; 250 saturn.planet.net Hello medined@stan54.planet.net    [207.3.100.120], pleased to meet you</FONT><B><FONT SIZE=2 FACE="Courier">&gt; MAIL From: &lt;medined@planet.net&gt;</FONT></B><FONT SIZE=2 FACE="Courier New">&lt; 250 &lt;medined@planet.net&gt;... Sender ok</FONT><B><FONT SIZE=2 FACE="Courier">&gt; RCPT To: &lt;~r00tbeer@fundy.csd.unbsj.ca&gt;</FONT></B><FONT SIZE=2 FACE="Courier New">&lt; 250 &lt;~r00tbeer@fundy.csd.unbsj.ca&gt;... Recipient ok</FONT><B><FONT SIZE=2 FACE="Courier">&gt; DATA</FONT></B><FONT SIZE=2 FACE="Courier New">&lt; 354 Enter mail, end with &quot;.&quot; on a line by itself</FONT><B><FONT SIZE=2 FACE="Courier">&gt;<B>.</B></FONT></B><FONT SIZE=2 FACE="Courier New">&lt; 250 TAA12656 Message accepted for delivery</FONT><B><FONT SIZE=2 FACE="Courier">&gt; QUIT</FONT></B><FONT SIZE=2 FACE="Courier New">&lt; 221 saturn.planet.net closing connection</PRE></BLOCKQUOTE><P>The lines in bold are the commands that were sent to the server.The body of the letter is not shown in the output. <H2><A NAME="ReceivingMailPOP"><FONT SIZE=5 COLOR=#FF0000>Receiving Mail (POP)</FONT></A></H2><P>The flip side to sending mail is, of course, receiving it. Thisis done using the POP or Post Office Protocol. SiNCe you've alreadyread about the SMTP protocol in detail, I'll skip describing thedetails of the POP. After all, the details can be read in theRFC documents when they are needed. Instead, I'll use the POP3Clientmodule- available on the CD-ROM-to demonstrate receiving mail.<P>Listing 18.3 contains a program that will <I>filter</I> your mail.It will display a report of the authors and subject line for anymail that relates to EarthDawn&#153;, a role-playing game fromFASA. This program will not delete any mail from the server, soyou can experiment with confideNCe.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>Before trying to run this program, make sure that the POP3Client module (POP3Client.pm) is in the <TT><B><FONT FACE="Courier">Mail</FONT></B></TT> subdirectory of the library directory. You may need to create the <TT><B><FONT FACE="Courier">Mail</FONT></B></TT> subdirectory as I did. On my system, this directory is called  it is probably different on your system though. See your system administratior if you need help placing the file into the correct directory.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>This script was tested using Windows 95. You might need to modify it for other systems. On SunOS 5.4+ (Solaris 2), you'll need to change the <TT><B><FONT FACE="Courier">POP3Client</FONT></B></TT> module to use a packing format of <TT>'<B>S n c4 x8</B>'</TT> instead of <TT>'<B>S n a4 x8</B>'</TT>. Other changes might also be needed.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Turn on the warning compiler option.<BR>Load the </I><TT><I>POP3Client module</I></TT><I>.The </I><TT><I>POP3Client</I></TT><I>module will load the </I><TT><I>Socket</I></TT><I>module automatically.<BR>Turn on the </I><TT><I>strict</I></TT><I>pragma.<BR>Declare some variables used to temporary values.<BR>Define the header format for the report.<BR>Define the detail format for the report.<BR>Initialize </I><TT><I>$username</I></TT><I>to a valid username for the mail server.<BR>Initialize </I><TT><I>$password</I></TT><I>to a valid password for the user name.<BR>Create a new </I><TT><I>POP3Client</I></TT><I>object.<BR>Iterate over the mail messages on the server. </I><TT><I>$pop-&gt;Count</I></TT><I>holds the number of messages waiting on the server to be read.<BR>Initialize a flag variable. When set true, the script will havea mail message relating to </I><TT><I>EarthDawn</I></TT><I>.<BR>Iterate over the headers in each mail messages. The </I><TT><I>Head()</I></TT><I>method of the </I><TT><I>POP3Client</I></TT><I>module returns the header lines one at a time in the </I><TT><I>$_</I></TT><I>variable.<BR>Store the author's name if looking at the From header line.<BR>Store the subject if looking at the Subject line.<BR>This is the filter test. It checks to see if the word &quot;EarthDawn&quot;is in the subject line. If so, the </I><TT><I>$earthDawn</I></TT><I>flag variable is set to true (or 1).<BR>This line is commented out; normally it would copy the text ofthe message into the </I><TT><I>@body</I></TT><I>array.<BR>This line is also commented out; it will delete the current mailmessage from the server. <I><B>Use with caution!</B> ONCe deleleted,you can't recover the messages.<BR>Set the flag variable, </I></I><TT><I>$earthDawn</I></TT><I>,to true.<BR>Write a detail line to the report if the flag variable is true.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 18.3&nbsp;&nbsp;18LST03.PL-Creating a Mail Filter<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>#!/usr/bin/perl -wuse Mail::POP3Client;use strict;my($i, $from, $subject);format main::STDOUT_TOP =    @|||||||||||||||||||||||||||||||||||||||||||||||||  Pg @&lt;    &quot;Waiting Mail Regarding EarthDawn&quot;,                    $%    Sender                  Subject    ----------------------  --------------------------------.format main::STDOUT =    @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;  @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;           $from,                    $subject.my($username)   = 'medined';my($password)   = 'XXXXXXXX';my($mailServer) = 'mailhost2.planet.net';my($pop) = Mail::POP3Client-&gt;new($username, $password, $mailServer);for ($i = 1; $i &lt;= $pop-&gt;Count; $i++) {    my($earthDawn) = 0;    foreach ($pop-&gt;Head($i)) {        $from = $1 if /From:\s(.+)/;$subject = $1 if /Subject:\s(.+)/;        if (/Subject: .*EarthDawn/) {#           @body = $pop-&gt;Body($i);#           $pop-&gt;Delete($i);            $earthDawn = 1;        }    }    if ($earthDawn) {        write();    }}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>Waiting Mail Regarding EarthDawn           Pg 1    Sender                  Subject    ----------------------  ---------------------------------    Bob.Schmitt             [EarthDawn] NethermaNCer    Doug.Stoechel           [EarthDawn] Weaponsmith    Mindy.Bailey            [EarthDawn] Troubador</PRE></BLOCKQUOTE><P>When you run this script, you should change <TT>$username</TT>,<TT>$password</TT>, and <TT>$mailServer</TT>and the filter test to whatever is appropriate for your system.<P>You could combine the filter program with the send mail program(from Listing 18.2) to create an automatic mail-response program.For example, if the subject of a message is &quot;Info,&quot;you can automaticallly send a predefined message with informationabout a given topic. You could also create a program to automaticallyforward the messages to a covering person while you are on vacation.I'm sure that with a little thought you can come up with a half-dozenways to make your life easier by automatically handling some ofyour iNComing mail.<H2><A NAME="CheckingforUpnessEcho"><FONT SIZE=5 COLOR=#FF0000>Checking for Upness (Echo)</FONT></A></H2><P>Occasionally it's good to know if a server is up and fuNCtioning.The echo service is used to make that determination. Listing 18.4shows a program that checks the upness of two servers.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>Windows 95 (and perhaps other operating systems) can't use the SIGALRM interrupt signal. This might cause problems if you use this script on those systems because the program will wait forever when a server does not respond.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Turn on the warning compiler option.<BR>Load the  </I><TT><I>Socket</I></TT><I>module.<BR>Turn on the strict pragma.<BR>Display a message if the </I><TT><I>red.planet.net</I></TT><I>server is reachable.<BR>Display a message if the </I><TT><I>saturn.planet.net</I></TT><I>server is reachable.<BR>Declare the </I><TT><I>echo()</I></TT><I>fuNCtion.<BR>Get the host and timeout parameters from the paramter array. Ifno timeout parameter is specified, 5 seconds wil be used.<BR>Declare some local variables.<BR>Get the tcp protocol and echo port numbers.<BR>Get the server's Internet address.<BR>If </I><TT><I>$serverAddr</I></TT><I>is undefined then the name of the server was probably iNCorrectand an error message is displayed.<BR>Check to see if the script is running under Windows 95.<BR>If not under Windows 95, store the old alarm handler fuNCtion,set the alarm handler to be an anonymous fuNCtion that simplyends the script, and set an alarm to go off in </I><TT><I>$timeout</I></TT><I>seconds.<BR>Initialize the status variable to true.<BR>Create a socket called </I><TT><I>ECHO</I></TT><I>.<BR>Initialize </I><TT><I>$packedFormat</I></TT><I>with format specifiers.<BR>Connect the socket to the remote server.<BR>Close the socket.<BR>Check to see if the script is running under Windows 95.<BR>If not under Windows 95, reset the alarm and restore the old alarmhandler fuNCtion.<BR>Return the status.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 18.4&nbsp;&nbsp;18LST04.PL-Using the Echo Service<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>#!/usr/bin/perl -wuse Socket;use strict;print &quot;red.planet.net is up.\n&quot; if echo('red.planet.net');print &quot;saturn.planet.net is up.\n&quot; if echo('saturn.planet.net');sub echo {    my($host)    = shift;    my($timeout) = shift || 5;    my($oldAlarmHandler, $status);    my($proto)      = getprotobyname(&quot;tcp&quot;)        || 6;    my($port)       = getservbyname(&quot;echo&quot;, &quot;tcp&quot;) || 7;    my($serverAddr) = (gethostbyname($host))[4];    return(print(&quot;echo: $host could not be found, sorry.\n&quot;), 0)        if ! defined($serverAddr);    if (0 == Win32::IsWin95) {        $oldAlarmHandler = $SIG{'ALRM'};        $SIG{'ALRM'} = sub { die(); };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -