📄 ch16_02.htm
字号:
<html><head><title>The Mail Modules (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch16_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch17_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">16.2. The Mail Modules</h2><p><a name="INDEX-2063" /><a name="INDEX-2064" />The Mail modules operate at a higherlevel than the Net modules, interacting with external mail packagessuch as <em class="emphasis">mail</em>, <em class="emphasis">mailx</em>,<em class="emphasis">sendmail</em>, or a POP3 server in the case ofNet::POP3. This section describes some of the MailTools modules,Mail::Folder, and the other mail-related modules that were mentionedat the beginning of this chapter.</p><a name="perlnut2-CHP-16-SECT-2.1" /><div class="sect2"><h3 class="sect2">16.2.1. Send Email with Mail::Mailer</h3><p><a name="INDEX-2065" /><a name="INDEX-2066" />The Mail::Mailermodule interacts with external mail programs. When you useMail::Mailer or create a new Mail::Mailer object, you can specifywhich mail program you want your program to talk to:</p><blockquote><pre class="code">use Mail::Mailer qw(mail);</pre></blockquote><p>Another way to specify the mailer is: </p><blockquote><pre class="code">use Mail::Mailer;$type = 'sendmail';$mailprog = Mail::Mailer->new($type);</pre></blockquote><p>in which <tt class="literal">$type</tt> is the mail program. Onceyou've created a new object, use the<tt class="literal">open</tt> function to send the message headers to themail program as a hash of key/value pairs, in which each keyrepresents a header type, and the value is the value of that header:</p><blockquote><pre class="code"># Mail headers to use in the message%headers = ( 'To' => 'you@mail.somename.com', 'From' => 'me@mail.somename.com', 'Subject' => 'working?');</pre></blockquote><p>This code represents headers in which the recipient of the mailmessage is <em class="emphasis">you@mail.somename.com</em>, the mail wassent from <em class="emphasis">me@mail.somename.com</em>, and the subjectof the mail message is <em class="emphasis">working?</em>.</p><p>Once <tt class="literal">%headers</tt> has been defined, it is passed to<tt class="literal">open</tt>:</p><blockquote><pre class="code">$mailprog->open(\%headers);</pre></blockquote><p>You then send the body of the message to the mail program: </p><blockquote><pre class="code">print $mailprog "This is the message body.\n";</pre></blockquote><p>Close the program when the message is finished: </p><blockquote><pre class="code">$mailprog->close;</pre></blockquote><p>A practical example of using Mail::Mailer might be a commandline-driven application that works much like the Unix<em class="emphasis">mail</em> program, either reading STDIN untilend-of-file or mailing a file specified on the command line.</p><p><a name="INDEX-2067" />Mail::Mailer uses the environmentvariable PERL_MAILERS to augment or modify the built-in mailerselection. PERL_MAILERS is specified in the following format:</p><blockquote><pre class="code">"type1:mailbinary1;mailbinary2;...:type2:mailbinaryX;...:..."</pre></blockquote><p>The possible types are listed for the <tt class="literal">new</tt> methodbelow.</p><p>The following methods are defined in Mail::Mailer.</p><a name="INDEX-2068" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">mailer</em> = new Mail::Mailer [<em class="replaceable">type</em>, <em class="replaceable">command</em>]</pre><p><a name="INDEX-2068" />Constructor. Creates a new Mailerobject representing the message to be sent. If the optional argumentsare specified, the value of <em class="replaceable"><tt>command</em>depends on <em class="replaceable">type</tt></em>, which can be one of:</p><dl><dt><b><tt class="literal">mail</tt></b></dt><dd>Uses the Unix <em class="emphasis">mail</em> program.<em class="replaceable"><tt>command</tt></em> is the path to<em class="emphasis">mail</em>. The module searches for<em class="emphasis">mailx</em>, <em class="emphasis">Mail</em>, and<em class="emphasis">mail</em>, in that order.</p></dd><dt><b><tt class="literal">sendmail</tt></b></dt><dd>Uses the <em class="emphasis">sendmail</em> program.<em class="replaceable"><tt>command</tt></em> is the path to<em class="emphasis">sendmail</em>.</p></dd><dt><b><tt class="literal">test</tt></b></dt><dd>Used for debugging. Calls <em class="emphasis">/bin/echo</em> to displaythe data, but doesn't actually send any mail.<em class="replaceable"><tt>command</tt></em> is ignored, if specified.</p></dd></dl><p>If no arguments are specified, the Mailer object searches forexecutables in the above order and uses the first one found as thedefault mailer.</p></div><a name="INDEX-2069" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>close</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">mailer</em>->close</pre><p><a name="INDEX-2069" />Closes the mail program.</p></div><a name="INDEX-2070" /><a name="INDEX-2071" /><a name="INDEX-2072" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>open</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">mailer</em>->open(\%hashref)</pre><p><a name="INDEX-2070" />Sends message headers to the<em class="emphasis">mail</em> program. The headers are passed via areference to a hash, in which each key is the name of a header, andthe value is the contents of the header field. The value can beeither a scalar or a reference to an array of scalars.<a name="INDEX-2071" /><a name="INDEX-2072" /></p></div></div><a name="perlnut2-CHP-16-SECT-2.2" /><div class="sect2"><h3 class="sect2">16.2.2. Better Header Control with Mail::Send</h3><p><a name="INDEX-2073" /><a name="INDEX-2074" />Mail::Send isbuilt on top of Mail::Mailer, which means that you can also choosethe mail program that sends the mail. Mail::Send has implemented themethods <tt class="literal">to</tt>, <tt class="literal">cc</tt>,<tt class="literal">bcc</tt>, and <tt class="literal">subject</tt> to replace the<tt class="literal">%headers</tt> hash used in Mail::Mailer.</p><p><a name="INDEX-2075" />Mail::Send uses the<tt class="literal">open</tt> method to open the mail program for output;it is built on Mail::Mailer's <tt class="literal">new</tt>method, so that:</p><blockquote><pre class="code"># Start mailer and output headers$fh = $msg->open('sendmail');</pre></blockquote><p>serves the same purpose as: </p><blockquote><pre class="code"># Use sendmail for mailing$mailer = Mail::Mailer->new('sendmail)';</pre></blockquote><p>This code tells Mail::Send to use <em class="emphasis">sendmail</em> asthe mail program.</p><p><a name="INDEX-2076" /><a name="INDEX-2077" />Mail::Send also provides the<tt class="literal">set</tt> and <tt class="literal">add</tt> functions, whichassign a value to a header tag and append a value to a header tag,respectively. The <tt class="literal">set</tt> function takes twoarguments—a header tag and a value—and is used like this:</p><blockquote><pre class="code">$msg->set($scalar, @array);</pre></blockquote><p>Therefore, to address a message to<em class="emphasis">you@mail.somename.com</em>:</p><blockquote><pre class="code">$msg->set('To', 'you@mail.somename.com');</pre></blockquote><p>The above sets the <tt class="literal">To</tt> header to<em class="emphasis">you@mail.somename.com</em>; however, the followingsets the <tt class="literal">To</tt> header to<em class="emphasis">postmaster@mail.somename.com</em> and<em class="emphasis">you@mail.somename.com</em> because they represent anarray of values:</p><blockquote><pre class="code">$msg->set('To', ('you@mail.somename.com', 'postmaster@mail.somename.com'));</pre></blockquote><p>You might think that you could use the <tt class="literal">set</tt>function as follows to add multiple values to a header value:</p><blockquote><pre class="code">$msg->set('To', 'you@mail.somename.com');$msg->set('To', 'someone@their.mailaddress.com');</pre></blockquote><p>However, <tt class="literal">set</tt> doesn't appendinformation from one call to another, and the above example wouldsend the mail only to<em class="emphasis">someone@their.mailaddress.com</em>. To append a nameto the <tt class="literal">To</tt> header, use the <tt class="literal">add</tt>method. For example:</p><blockquote><pre class="code">$msg->add('To', 'you@mail.somename.com');$msg->add('To', 'someone@their.mailaddress.com');</pre></blockquote><p>The following methods are defined for Mail::Send.</p><a name="INDEX-2078" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">msg</em> = new Mail::Send [<em class="replaceable">header</em>=>'<em class="replaceable">value</em>'[, ...]]</pre><p><a name="INDEX-2078" />Constructor. Creates a new Mail::Sendobject that is the mail message you want to send. You can includevalues for headers when you create the object, or include them laterby calling the appropriate methods.</p></div><a name="INDEX-2079" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>add</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">msg</em>->add(<em class="replaceable">header</em>, <em class="replaceable">values</em>)</pre><p><a name="INDEX-2079" />Adds a header to the message.<em class="replaceable"><tt>header</tt></em> is the header to be added, and<em class="replaceable"><tt>values</tt></em> is a list of values to be appendedto that header.</p></div><a name="INDEX-2080" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>bcc</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -