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

📄 ch18.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:

The complete list of reply codes is shown in Table 18.4. Hopefully,

you'll never see most of them.<BR>

<CENTER><B>Table 18.4&nbsp;&nbsp;Reply Codes Used by Mail Servers</B></CENTER>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD WIDTH=62><CENTER><I>Code</I></CENTER></TD><TD WIDTH=528><I>Description</I>

</TD></TR>

<TR><TD WIDTH=62><CENTER>211</CENTER></TD><TD WIDTH=528>A system status or help reply.

</TD></TR>

<TR><TD WIDTH=62><CENTER>214</CENTER></TD><TD WIDTH=528>Help Message.

</TD></TR>

<TR><TD WIDTH=62><CENTER>220</CENTER></TD><TD WIDTH=528>The server is ready.

</TD></TR>

<TR><TD WIDTH=62><CENTER>221</CENTER></TD><TD WIDTH=528>The server is ending the conversation.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;250</CENTER></TD><TD WIDTH=528>The requested action was completed.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;251</CENTER></TD><TD WIDTH=528>The specified user is not local, but the server will forward the mail message.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;354</CENTER></TD><TD WIDTH=528>This is a reply to the <TT>DATA</TT> command. After getting this, start sending the body of the mail message, ending with &quot;\r\n.\r\n.&quot;

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;421</CENTER></TD><TD WIDTH=528>The mail server will be shut down. Save the mail message and try again later.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;450</CENTER></TD><TD WIDTH=528>The mailbox that you are trying to reach is busy. Wait a little while and try again.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;451</CENTER></TD><TD WIDTH=528>The requested action was not done. Some error occurred in the mail server.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;452</CENTER></TD><TD WIDTH=528>The requested action was not done. The mail server ran out of system storage.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;500</CENTER></TD><TD WIDTH=528>The last command contained a syntax error or the command line was too long.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;501</CENTER></TD><TD WIDTH=528>The parameters or arguments in the last command contained a syntax error.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;502</CENTER></TD><TD WIDTH=528>The mail server has not implemented the last command.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;503</CENTER></TD><TD WIDTH=528>The last command was sent out of sequeNCe. For example, you might have sent <TT>DATA</TT> before sending <TT>RECV</TT>.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;504</CENTER></TD><TD WIDTH=528>One of the parameters of the last command has not been implemented by the server.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;550</CENTER></TD><TD WIDTH=528>The mailbox that you are trying to reach can't be found or you don't have access rights.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;551</CENTER></TD><TD WIDTH=528>The specified user is not local; part of the text of the message will contain a forwarding address.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;552</CENTER></TD><TD WIDTH=528>The mailbox that you are trying to reach has run out of space. Store the message and try again tomorrow or in a few days-after the user gets a chaNCe to delete some messages.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;553</CENTER></TD><TD WIDTH=528>The mail address that you specified was not syntactically correct.

</TD></TR>

<TR><TD WIDTH=62><CENTER>&nbsp;554</CENTER></TD><TD WIDTH=528>The mail transaction has failed for unknown causes.

</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

Now that you've seen all of the SMTP commands and reply codes,

let's see what a typical mail conversation might look like. In

the following conversation, the '&gt;' lines are the SMTP commands

that your program issues. The '&lt;' lines are the mail server's

replies.

<BLOCKQUOTE>

<PRE>

<B>&gt;HELO

</FONT></B><FONT SIZE=2 FACE="Courier">&lt;250 saturn.planet.net Hello medined@planet.net [X.X.X.X],pleased to meet you



<B>&gt;MAIL From: &lt;(Rolf D'Barno, 5th Circle Archer)&gt;

</B>&lt;250 &lt;(Rolf D'Barno, 5th Circle Archer)&gt;... Sender ok



<B>&gt;RCPT To: &lt;medined@planet.net&gt;

</B>&lt;250 &lt;medined@planet.net&gt;... Recipient ok



<B>&gt;DATA

</B>&lt;354 Enter mail, end with &quot;.&quot; on a line by itself



<B>&gt;From: (Rolf D'Barno, 5th Circle Archer)

&gt;Subject: Arrows

&gt;This is line one.

&gt;This is line two.

</B>&gt;.

&lt;250 AAA14672 Message accepted for delivery



<B>&gt;QUIT

</B>&lt;221 saturn.planet.net closing connection

</PRE>

</BLOCKQUOTE>

<P>

The bold lines are the commands that are sent to the server. Some

of the SMTP commands are a bit more complex than others. In the

next few sections, the <TT>MAIL</TT>,

<TT>RCPT,</TT> and <TT>DATA</TT>

commands are discussed. You will also see how to react to undeliverable

mail.

<H3><A NAME="TheTTFONTSIZEFACECourierMAILFONTTTFONTSIZECommandFONT">

The <TT>MAIL</TT>

Command</FONT></A></H3>

<P>

The <TT>MAIL</TT> command tells the

mail server to start a new conversation. It's also used to let

the mail server know where to send a mail message to report errors.

The syntax looks like this:

<BLOCKQUOTE>

<PRE>

MAIL FROM:&lt;reverse-path&gt;

</PRE>

</BLOCKQUOTE>

<P>

If the mail server accepts the command, it will reply with a code

of 250. Otherwise, the reply code will be greater than 400.

<P>

In the example shown previously

<BLOCKQUOTE>

<PRE>

<B>&gt;MAIL From:&lt;(medined@planet.net)&gt;

</FONT></B><FONT SIZE=2 FACE="Courier">&lt;250 &lt;(medined@planet.net)&gt;... Sender ok

</PRE>

</BLOCKQUOTE>

<P>

The reverse-path is different from the name given as the sender

following the <TT>DATA</TT> command.

You can use this technique to give a mailing list or yourself

an alias. For example, if you are maintaining a mailing list to

your college alumni, you might want the name that appears in the

reader's mailer to be <TT>'87 RugRats</TT>

instead of your own name.

<H3><A NAME="TheTTFONTSIZEFACECourierRCPTFONTTTFONTSIZECommandFONT">

The <TT>RCPT </TT>Command</FONT>

</A></H3>

<P>

You tell the mail server who the recipient of your message is

by using the <TT>RCPT</TT> command.

You can send more than one <TT>RCPT</TT>

command for multiple recipients. The server will respond with

a code of 250 to each command. The syntax for the <TT>RCPT</TT>

is:

<BLOCKQUOTE>

<PRE>

RCPT TO:&lt;forward-path&gt;

</PRE>

</BLOCKQUOTE>

<P>

Only one recipient can be named per <TT>RCPT</TT>

command. If the recipient is not known to the mail server, the

response code will be 550. You might also get a response code

indicating that the recipient is not local to the server. If that

is the case, you will get one of two responses back from the server:

<UL>

<LI><B>251 User not local; will forward to &lt;forward-path&gt;</B>-This

reply means that the server will forward the message. The correct

mail address is returned so that you can store it for future use.

<LI><B>551 User not local; please try &lt;forward-path&gt;</B>-This

reply means that the server won't forward the message. You need

to issue another <TT>RCPT</TT> command

with the new address.

</UL>

<H3><A NAME="TheTTFONTSIZEFACECourierDATAFONTTTFONTSIZECommandFONT">

The <TT>DATA</TT>

Command</FONT></A></H3>

<P>

After starting the mail conversation and telling the server who

the recipient or recipients are, you use the<TT>

DATA</TT> command to send the body of the message. The

syntax for the <TT>DATA</TT> command

is very simple:

<BLOCKQUOTE>

<PRE>

DATA

</PRE>

</BLOCKQUOTE>

<P>

After you get the standard 354 response, send the body of the

message followed by a line with a single period to indicate that

the body is finished. When the end of message line is received,

the server will respond with a 250 reply code.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

The body of the message can also iNClude several header items like Date, Subject, To, Cc, and From.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<H3><A NAME="ReportingUndeliverableMail">

Reporting Undeliverable Mail</A></H3>

<P>

The mail server is responsible for reporting undeliverable mail,

so you may not need to know too much about this topic. However,

this information may come in handy if you ever run a list service

or if you send a message from a temporary account.

<P>

An endless loop happens when an error notification message is

sent to a non-existent mailbox. The server keeps trying to send

a notification message to the reverse-path specified in the <TT>MAIL</TT>

command.

<P>

The answer to this dilemma is to specify an empty reverse path

in the <TT>MAIL</TT> command of a

notification message like this:

<BLOCKQUOTE>

<PRE>

MAIL FROM:&lt;&gt;

</PRE>

</BLOCKQUOTE>

<P>

An entire mail session that delivers an error notification message

might look like the following:

<BLOCKQUOTE>

<PRE>

<B>MAIL FROM:&lt;&gt;

</FONT></B><FONT SIZE=2 FACE="Courier">250 ok

<B>RCPT TO:&lt;@HOST.COM@HOSTW.ARPA&gt;

</B>250 ok

<B>DATA

</B>354 send the mail data, end with .

<B>Date: 12 May 96 12:34:53

From: MEDINED@PLANET.NET

To: ROBIN@UIC.HOST.COM

Subject: Problem delivering mail.



Robin, your message to JACK@SILVER.COM was not

delivered.



    SILVER.COM said this:

        &quot;550 No Such User&quot;

</B>.

250 ok

</PRE>

</BLOCKQUOTE>

<H3><A NAME="UsingPerltoSendMail">

Using Perl to Send Mail</A></H3>

<P>

I'm sure that by now you've had enough theory and would like to

see some actual Perl code. Without further explanation, Listing

18.2 shows you how to send mail.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Caution</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

The script in Listing 18.2 was tested on Windows 95. Some comments have been added to indicate changes that are needed for SunOS 4.1+ and SunOS 5.4+ (Solaris 2). The SunOS comments were supplied by Qusay H. Mahmoud-also known as Perlman on IRC. <FONT 
FACE="LI Helvetica Light Oblique">Thanks, Qusay!</FONT>

</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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>

Initialize </I><TT><I>$mail To </I></TT><I>which

holds the recipient's mail address.<BR>

Initialize </I><TT><I>$mailServer</I></TT><I>

which holds the symbolic name of your mail server.<BR>

Initialize </I><TT><I>$mailFrom</I></TT><I>

which holds the originator's mail address.<BR>

Initialize </I><TT><I>$realName </I></TT><I>which

holds the text that appears in the From header field.<BR>

Initialize </I><TT><I>$subject</I></TT><I>

which holds the text that appears in the Subject header field.

<BR>

Initialize </I><TT><I>$body</I></TT><I>

which holds the text of the letter.<BR>

Declare a signal handler for the Interrupt signal. This handler

will trap users hitting Ctrl+c or Ctrl+break.<BR>

Get the protocol number for the tcp protocol and the port number

for the smtp service. Windows 95 and NT do not implement the </I><TT><I>getprotobyname()</I></TT><I>

or </I><TT><I>getservbyname()</I></TT><I>

fuNCtions so default values are supplied.<BR>

Initialize </I><TT><I>$serverAddr</I></TT><I>

with the mail server's Internet address.<BR>

The </I><TT><I>$length</I></TT><I>

variable is tested to see if it is defined, if not, then the</I><TT><I>gethostbyname()</I></TT><I>

fuNCtion failed.<BR>

Create a socket called </I><TT><I>SMTP</I></TT><I>

using standard parameters.<BR>

Initialize </I><TT><I>$packedFormat</I></TT><I>

with format specifiers.<BR>

Connect the socket to the port on the mail server.<BR>

Change the socket to use unbuffer input/output. Normally, sends

and receives are stored in an internal buffer before being sent

to your script. This line of code eliminates the buffering steps.

<BR>

Create a temporary buffer. The buffer is temporary because it

is local to the block surrounded by the curly brackets.<BR>

Read two responses from the server. My mail server sends two reponses

when the connection is made. Your server may only send one response.

<BR>

If so, delete one of the </I><TT><I>recv()</I></TT><I>

calls.<BR>

Send the </I><TT><I>HELO</I></TT><I>

command. The </I><TT><I>sendSMTP()</I></TT><I>

fuNCtion will take care of reading the response.<BR>

Send the </I><TT><I>MAIL</I></TT><I>

command indicating where messages that the mail server sends back

(like undeliverable mail messages) should be sent.<BR>

Send the </I><TT><I>RCPT</I></TT><I>

command to specify the recipient.<BR>

Send the </I><TT><I>DATA</I></TT><I>

command.<BR>

Send the body of the letter. Note that no reponses are received

from the mail server while the letter is sent.<BR>

Send a line containing a single period indicating that you are

finished sending the body of the letter.<BR>

Send the </I><TT><I>QUIT</I></TT><I>

command to end the conversation.<BR>

⌨️ 快捷键说明

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