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

📄 ch09_04.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<?label 9.4. sendmail?><html><head><title>sendmail (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><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">CGI Programming with Perl</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></table></div><hr align="left" width="515" /><h2 class="sect1">9.4. sendmail</h2><p>Without <tt class="command">sendmail</tt><a name="INDEX-1869" /><a name="INDEX-1870" /><a name="INDEX-1871" />, Internet email mightnot exist. Although other mail transport agents (MTAs) do exist, thevast majority of mail servers on the Internet all run<tt class="command">sendmail</tt>. It was originally written by Eric Allmanstarting around 1980 and, as we mentioned earlier, the Internet wasvery different then. <em class="emphasis">sendmail</em> tackled theformidable task of transferring mail between very different networks.Thus, it has never been a simple program, and it has grown. It hasbecome one of the most complicated applications to ever fullyunderstand; the number of command-line flags and configurationparameters it now accepts is truly mind-boggling. Fortunately, weonly need to learn a few things in order to have it send messages forus. If you want to learn more about sendmail, see<em class="citetitle">sendmail</em> by Bryan Costales with Eric Allman(O'Reilly &amp; Associates, Inc.).</p><p><tt class="command">sendmail</tt> generally comes preinstalled on<a name="INDEX-1872" />Unixmachines and has recently been ported to Windows NT. On Unix, it isoften installed in <em class="filename">/usr/lib/sendmail</em>, but<em class="filename">/usr/sbin/sendmail</em> and<em class="filename">/usr/ucb/lib/sendmail</em> are also possiblelocations. These examples will use<em class="filename">/usr/lib/sendmail</em> as the location of the<tt class="command">sendmail</tt> program. If your copy is installedelsewhere, simply replace this with the path to your copy of<tt class="command">sendmail</tt>.</p><a name="ch09-5-fm2xml" /><div class="sect2"><h3 class="sect2">9.4.1. Command-Line Options</h3><p>You generally want to call<a name="INDEX-1873" /> <a name="INDEX-1,874" /><a name="INDEX-1875" />sendmailwith at least a couple of command-line options. When sending amessage, <tt class="command">sendmail</tt> assumes it is being runinteractively by a user, so it sets the sender to be that of theuser, and it allows the user to enter a period on its own line tosignal the end of the message. You can override these settings andwill probably want to. In addition, if you are sending multiple emailmessages, you may wish to queue them so that<tt class="command">sendmail</tt> can deliver them asynchronously withoutpausing to deliver each one.</p><p><a href="ch09_04.htm#ch09-97770">Table 9-1</a> lists the important options you shouldknow.</p><a name="ch09-97770" /><h4 class="objtitle">Table 9-1. Common sendmail Options </h4><table border="1"><tr><th><p>Option</p></th><th><p>Description</p></th></tr><tr><td><blockquote><pre class="code">-t</pre></blockquote></td><td><p>Read <em class="emphasis">To</em>, <em class="emphasis">Cc</em>, and<em class="emphasis">Bcc</em> from the message headers.</p></td></tr><tr><td><blockquote><pre class="code">-f "email address"</pre></blockquote></td><td><p>Make the message appear to be <em class="emphasis">From</em> the specifiedemail address.</p></td></tr><tr><td><blockquote><pre class="code">-F "full name"</pre></blockquote></td><td><p>Make the message appear to be <em class="emphasis">From</em> the specifiedname.</p></td></tr><tr><td><blockquote><pre class="code">-i</pre></blockquote></td><td><p>Ignore periods on lines by themselves.</p></td></tr><tr><td><blockquote><pre class="code">-odq</pre></blockquote></td><td><p>Queue messages to be sent later instead of processing them one at atime.</p></td></tr></table><p><a href="ch09_04.htm#ch09-99939">Example 9-1</a> is a short CGI script in Perl that usesmany of these options.</p><a name="ch09-99939" /><div class="example"><h4 class="objtitle">Example 9-1. feedback_sendmail.cgi </h4><a name="INDEX-1876" /><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;use CGI;# Clean up environment for taint mode before calling sendmailBEGIN {    $ENV{PATH} = "/bin:/usr/bin";    delete @ENV{ qw( IFS CDPATH ENV BASH_ENV ) };}my $q       = new CGI;my $email   = validate_email_address( $q-&gt;param( "email" ) );my $message = $q-&gt;param( "message" );unless ( $email ) {    print $q-&gt;header( "text/html" ),          $q-&gt;start_html( "Invalid Email Address" ),          $q-&gt;h1( "Invalid Email Address" ),          $q-&gt;p( "The email address you entered is invalid. " .                 "Please use your browser's Back button to " .                 "return to the form and try again." );          $q-&gt;end_html;    exit;}send_feedback( $email, $message );send_receipt( $email );print $q-&gt;redirect( "/feedback/thanks.html" );sub send_feedback {    my( $email, $message ) = @_;        open MAIL, "| /usr/lib/sendmail -t -i"        or die "Could not open sendmail: $!";        print MAIL &lt;&lt;END_OF_MESSAGE;To: webmaster\@scripted.comReply-To: $emailSubject: Web Site FeedbackFeedback from a user:

⌨️ 快捷键说明

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