📄 ch09_04.htm
字号:
$messageEND_OF_MESSAGE close MAIL or die "Error closing sendmail: $!";}sub send_receipt { my $email = shift; open MAIL, "| /usr/lib/sendmail -t -F'$from_name' -f'$from_email'" or die "Could not open sendmail: $!"; print MAIL <<END_OF_MESSAGE;To: $emailSubject: Your feedbackYour message has been sent and someone should be responding to you shortly. Thanks for taking the time to provide us with your feedback!END_OF_MESSAGE close MAIL or die "Error closing sendmail: $!";}</pre></blockquote></div><p>We collect two pieces of information from the user: an email addressand a message to send to customer service. We validate the emailaddress according to the subroutine earlier in this chapter, but wedon't include the code for that subroutine here. The scriptthen composes two messages and forwards users to a static page tothank them.</p><p>The first message goes to customer service. It uses the<em class="emphasis">-t</em><a name="INDEX-1877" /> option as well as the<em class="emphasis">-i</em> option. The<a name="INDEX-1878" /> <em class="emphasis">-i</em> option is agood idea if the message includes any dynamic information. Itprevents a single dot from prematurely ending the email message.</p><p>The <em class="emphasis">-t</em> option is the most important of theseoptions. It tells <tt class="command">sendmail</tt> to read the routinginformation for the recipient from the message itself. Otherwise youhave to provide the recipient's email address on the commandline. Generally, you call <tt class="command">sendmail</tt> like this:</p><blockquote><pre class="code">/usr/lib/sendmail mary@somewhere.com</pre></blockquote><p><tt class="command">sendmail</tt> then reads the message including theheaders and body from its STDIN and sends the message on to Mary,even if the <em class="emphasis">To</em>, <em class="emphasis">Cc</em>, or<em class="emphasis">Bcc</em> fields say it should go elsewhere! This canget confusing.</p><p>You should always use the<em class="emphasis">-t</em><a name="INDEX-1879" />flag. First, it makes your life easier, since it automaticallyhandles the <em class="emphasis">To</em>, <em class="emphasis">Cc</em>, and<em class="emphasis">Bcc</em> fields. Second, it lets you avoid that awful<a name="INDEX-1880" />security risk of passing user datathrough the <a name="INDEX-1881" />shell. Many times you will besending email to an address that was entered into an HTML form, sobeing able to simply include the email address in the body of themessage instead is another big win.</p><p>Once this message has been sent, the script sends a<a name="INDEX-1882" />confirmation to the user. It also uses the<em class="emphasis">-t</em> option here, and here we see the securitybenefit. The email address comes from the user, but we don'thave to worry about passing it through the shell.</p><p>In this second email, we also use two other<a name="INDEX-1883" />fields to override the sender's<a name="INDEX-1884" />routing information.<tt class="command">sendmail</tt> will not automatically read thesender's email address from the headers as it does for the<em class="emphasis">-t</em> option. This must be specified with the<em class="emphasis">-f</em><a name="INDEX-1885" /> and <em class="emphasis">-F</em> options.There are two options in order to support the extended addressnotation including a name and an email address in this form:</p><blockquote><pre class="code">The Webmaster <webmaster@scripted.com></pre></blockquote><p>It is important to override the sender's routing informationbecause if the message to Mary bounces, it will come back to theoriginal sender, and if the<a name="INDEX-1886" /> <a name="INDEX-1,887" />user that the web server runs as has astandard account with a mail box, bounced messages will collectthere. If it has no mail account, then they'll bounce back andforth either until they time out or some system administrator getsannoyed at the increased network traffic and steps in. Ideally, yoursystem should be configured so that any mail addressed to<em class="emphasis">nobody</em> (the user your web server runs as) isautomatically forwarded to the webmaster. If this hasn't beendone, or you aren't sure, then it's a good idea to setthe <em class="emphasis">-f</em> option to a real email address thatsomeone monitors or that is processed automatically. We'll seehow to set up a process to handle mail like this at the end of thischapter.</p><p>Note that if you do <a name="INDEX-1888" />override the sender's emailaddress with the <em class="emphasis">-f</em> option,<tt class="command">sendmail</tt> will add an extra<a name="INDEX-1889" />header to the email message unless youare a <a name="INDEX-1890" /><a name="INDEX-1891" />trusted user. This extra headertypically looks like the following:</p><blockquote><pre class="code">X-Authentication-Warning: scripted.com: sguelich set sender to nobody@scripted.com using -f</pre></blockquote><p>By default, the users who have permission to use the<em class="emphasis">-f</em> option without generating this warning are<em class="emphasis">root</em>, <em class="emphasis">daemon</em>, and<em class="emphasis">uucp</em>. Most mail agents do not actually payattention to this header, so it is rare that recipients will see it.However, you can avoid sending it by adding<em class="emphasis">nobody</em> to the trusted users section in<em class="filename">/etc/sendmail.cf</em>.</p></div><a name="ch09-6-fm2xml" /><div class="sect2"><h3 class="sect2">9.4.2. Mail Queue</h3><p>The remaining option <a name="INDEX-1892" /><a name="INDEX-1893" /> <a name="INDEX-1,894" /> <a name="INDEX-1,895" />we haven't discussed is the<em class="emphasis">-odq</em> option. It is useful if you are sending outmany messages at the same time. For example, you may run a web sitethat connects job hunters with available positions. You have the jobhunters record keywords describing the types of positions they arelooking for in a database along with their email addresses. Then,when the new positions available today have been entered, you start aCGI script which matches the job hunters' keywords against thepositions. The script generates and sends out customized messages tothe job hunters notifying them if there are any matches. In thisexample, you would want to use the <em class="emphasis">-odq</em> option.It takes sendmail time to find remote servers and deliver messages,so your script runs much, much faster if you simply add them to thequeue to be processed separately and don't wait for sendmail totry to deliver each message.</p><p>You do need to make sure that <em class="emphasis">sendmail</em> isconfigured on your system to process the queue or the messages mayjust sit around indefinitely. If you aren't sure, ask yoursystem administrator.</p><p>Also note that queuing messages this way is only a good idea if eachmessage you are sending out is unique. If you are sending the samemessage to multiple people, don't queue a separate messageaddressed to each person, <a name="INDEX-1896" /> <a name="INDEX-1,897" /> <a name="INDEX-1,898" />use the<em class="emphasis">Bcc</em><a name="INDEX-1899" /> field instead.</p></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch09_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch09_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">9.3. Structure of Internet Email</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">9.5. mailx and mail</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -