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

📄 ch18_06.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 18.5. Reading Mail with POP3 (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:04Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch18_01.htm"TITLE="18. Internet Services"><LINKREL="prev"HREF="ch18_05.htm"TITLE="18.4. Reading and Posting Usenet News Messages"><LINKREL="next"HREF="ch18_07.htm"TITLE="18.6. Simulating Telnet from a Program"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_05.htm"TITLE="18.4. Reading and Posting Usenet News Messages"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.4. Reading and Posting Usenet News Messages"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch18_01.htm"TITLE="18. Internet Services"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_07.htm"TITLE="18.6. Simulating Telnet from a Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 18.6. Simulating Telnet from a Program"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch18-chap18_reading_1">18.5. Reading Mail with POP3</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-709">Problem</A></H3><PCLASS="para">You want to fetch mail from a <ACLASS="indexterm"NAME="ch18-idx-1000004305-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004305-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004305-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004305-3"></A>POP3 server. This lets you write a program to summarize your unread mail, move it from a remote server to a local mailbox, or toggle between Internet and local mail systems.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-715">Solution</A></H3><PCLASS="para">Use the CPAN module Net::POP3:</P><PRECLASS="programlisting">$pop = Net::POP3-&gt;new($mail_server)    or die &quot;Can't open connection to $mail_server : $!\n&quot;;defined ($pop-&gt;login($username, $password))    or die &quot;Can't authenticate: $!\n&quot;;$messages = $pop-&gt;list    or die &quot;Can't get list of undeleted messages: $!\n&quot;;foreach $msgid (keys %$messages) {    $message = $pop-&gt;get($msgid);    unless (defined $message) {        warn &quot;Couldn't fetch $msgid from server: $!\n&quot;;        next;    }    # $message is a reference to an array of lines    $pop-&gt;delete($msgid);}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-751">Discussion</A></H3><PCLASS="para">Traditionally, mail has been a three-party system: the <EMCLASS="emphasis">MTA</EM> (Mail Transport Agent, a system program like <EMCLASS="emphasis">sendmail</EM>) delivers mail to the <EMCLASS="emphasis">spool</EM>, where it is read by the <EMCLASS="emphasis">MUA</EM> (Mail User Agent, a program like <EMCLASS="emphasis">mail</EM>). This dates from the days of big servers holding mail and users reading it through dumb terminals. As PCs and networks entered the picture, the need arose for MUAs like Pine to run on different machines than the one housing the spool. The Post Office Protocol (POP) implements efficient message listing, reading, and deleting over a TCP/IP session.</P><PCLASS="para">The CPAN module Net::POP3 is a POP client. That is, it lets your Perl program act as an MUA. The first step in using Net::POP3 is to create a new Net::POP3 object. Pass <CODECLASS="literal">new</CODE> the name of the POP3 server:</P><PRECLASS="programlisting">$pop = Net::POP3-&gt;new( &quot;pop.myisp.com&quot; )    or die &quot;Can't connect to pop.myisp.com: $!\n&quot;;</PRE><PCLASS="para">All Net::POP3 functions return <CODECLASS="literal">undef</CODE> or the empty list <CODECLASS="literal">()</CODE> if an error occurs, depending on the context they were called in. If an error occurs, <CODECLASS="literal">$!</CODE> may contain a meaningful error message (but also may not).</P><PCLASS="para">You may optionally pass further arguments to <CODECLASS="literal">new</CODE> in a hash-like fashion, indicating a timeout value (in seconds) for network operations:</P><PRECLASS="programlisting">$pop = Net::POP3-&gt;new( &quot;pop.myisp.com&quot;,                       Timeout =&gt; 30 )    or die &quot;Can't connect to pop.myisp.com : $!\n&quot;;</PRE><PCLASS="para">Authenticate yourself to the POP3 server with the <CODECLASS="literal">login</CODE> method. It takes two arguments, username and password, but both are optional. If the username is omitted, the current username is used. If the password is omitted, Net::POP3 tries to use Net::Netrc to find a password:</P><PRECLASS="programlisting">defined ($pop-&gt;login(&quot;gnat&quot;, &quot;S33kr1T Pa55w0rD&quot;))    or die &quot;Hey, my username and password didn't work!\n&quot;;defined ($pop-&gt;login( &quot;midget&quot; ))           # use Net::Netrc to find password    or die &quot;Authentication failed.\n&quot;;defined ($pop-&gt;<CODECLASS="literal">login())</CODE>                     # current username and Net::Netrc    or die &quot;Authentication failed.  Miserably.\n&quot;;</PRE><PCLASS="para">The <CODECLASS="literal">login</CODE> method sends the password in plain text across the network. This is undesirable, so if you have the MD5 module from CPAN, you can use the <CODECLASS="literal">apop</CODE> method. It works exactly like <CODECLASS="literal">login</CODE>, except that it encrypts the password:</P><PRECLASS="programlisting">$pop-&gt;apop( $username, $password )    or die &quot;Couldn't authenticate: $!\n&quot;;</PRE><PCLASS="para">Once authenticated, you may then access the spool with <CODECLASS="literal">list</CODE>, <CODECLASS="literal">get</CODE>, and <CODECLASS="literal">delete</CODE>. The <CODECLASS="literal">list</CODE> method gives you a list of undeleted messages in the spool. It returns a hash, where each key is a message number and each value is the size of the corresponding message in bytes:</P><PRECLASS="programlisting">%undeleted = $pop-&gt;<CODECLASS="literal">list();</CODE>foreach $msgnum (keys %undeleted) {    print &quot;Message $msgnum is $undeleted{$msgnum} bytes long.\n&quot;;}</PRE><PCLASS="para">To retrieve a message, call <CODECLASS="literal">get</CODE> with the message number. It returns a reference an array of lines in the message:</P><PRECLASS="programlisting">print &quot;Retrieving $msgnum : &quot;;$message = $pop-&gt;get($msgnum);if ($message) {    # succeeded    print &quot;\n&quot;;    print @$message;                # print the message} else {        # failed    print &quot;failed ($!)\n&quot;;}</PRE><PCLASS="para">The <CODECLASS="literal">delete</CODE> method marks a message as deleted. When you call <CODECLASS="literal">quit</CODE> to terminate your POP3 session, the messages marked as deleted are removed from the mailbox. The <CODECLASS="literal">reset</CODE> method undoes any <CODECLASS="literal">delete</CODE> calls made during the session. If the session is terminated by the Net::POP3 object being destroyed because it went out of scope, the <CODECLASS="literal">reset</CODE> will be called automatically.</P><PCLASS="para">You have probably noticed there's no way to <EMCLASS="emphasis">send</EM> mail. POP3 only supports reading and deleting existing messages. To send new ones, you still have to use programs like <EMCLASS="emphasis">mail</EM> or <EMCLASS="emphasis">sendmail</EM>, or do <CODECLASS="literal">SMTP</CODE>. In other words, you still need to use <ACLASS="xref"HREF="ch18_04.htm"TITLE="Sending Mail">Recipe 18.3</A>.</P><PCLASS="para">The task attempted by POP3 &nbsp;-  connecting mail clients and mail servers &nbsp;-  is also attempted by the IMAP protocol. IMAP has more features and is more typically seen on very large sites.<ACLASS="indexterm"NAME="ch18-idx-1000004311-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004311-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004311-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004311-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-835">See Also</A></H3><PCLASS="para">The documentation for the Net::POP3 module from CPAN; RFC 1734, <CITECLASS="citetitle">POP3 AUTHentication command</CITE>; RFC 1957, <CITECLASS="citetitle">Some Observations on Implementations of the Post Office Protocol</CITE></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_05.htm"TITLE="18.4. Reading and Posting Usenet News Messages"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.4. Reading and Posting Usenet News Messages"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_07.htm"TITLE="18.6. Simulating Telnet from a Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 18.6. Simulating Telnet from a Program"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">18.4. Reading and Posting Usenet News Messages</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">18.6. Simulating Telnet from a Program</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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