<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >mail</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Mail 邮件函数" HREF="ref.mail.html"><LINK REL="PREVIOUS" TITLE="ezmlm_hash" HREF="function.ezmlm-hash.html"><LINK REL="NEXT" TITLE="Mailparse Functions" HREF="ref.mailparse.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.ezmlm-hash.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="ref.mailparse.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.mail" ></A >mail</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN107063" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >mail -- 发送邮件</DIV ><DIV CLASS="refsect1" ><A NAME="AEN107066" ></A ><H2 >说明</H2 >bool <B CLASS="methodname" >mail</B > ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )<BR ></BR ><P > 发送一封电子邮件。 </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN107087" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >to</CODE ></DT ><DD ><P > 电子邮件收件人,或收件人列表。 </P ><P > 本字符串的格式必须符合 <A HREF="http://www.faqs.org/rfcs/rfc2822" TARGET="_top" >RFC 2822</A >。例如: <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD >user@example.com</TD ></TR ><TR ><TD >user@example.com, anotheruser@example.com</TD ></TR ><TR ><TD >User <user@example.com></TD ></TR ><TR ><TD >User <user@example.com>, Another User <anotheruser@example.com></TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DD ><DT ><CODE CLASS="parameter" >subject</CODE ></DT ><DD ><P > 电子邮件的主题。 </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >小心</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > 本项不能包含任何换行符,否则邮件可能无法正确发送。 </P ></TD ></TR ></TABLE ></DIV ></DD ><DT ><CODE CLASS="parameter" >message</CODE ></DT ><DD ><P > 所要发送的消息。 </P ><P > 行之间必须以一个 LF(\n)分隔。每行不能超过 70 个字符。 </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >小心</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > (Windows 下)当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉。要避免此问题,将单个句号替换成两个句号。 <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$text </font><font color="#007700">= </font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#DD0000">"\n."</font><font color="#007700">, </font><font color="#DD0000">"\n.."</font><font color="#007700">, </font><font color="#0000BB">$text</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE > </P ></TD ></TR ></TABLE ></DIV ></DD ><DT ><CODE CLASS="parameter" >additional_headers</CODE >(可选项)</DT ><DD ><P > String to be inserted at the end of the email header. </P ><P > This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > When sending mail, the mail <SPAN CLASS="emphasis" ><I CLASS="emphasis" >must</I ></SPAN > contain a <TT CLASS="literal" >From</TT > header. This can be set with the <CODE CLASS="parameter" >additional_headers</CODE > parameter, or a default can be set in <TT CLASS="filename" >php.ini</TT >. </P ><P > Failing to do this will result in an error message similar to <TT CLASS="literal" >Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing</TT >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with <A HREF="http://www.faqs.org/rfcs/rfc2822" TARGET="_top" >RFC 2822</A >. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><CODE CLASS="parameter" >additional_parameters</CODE > (optional)</DT ><DD ><P > The <CODE CLASS="parameter" >additional_parameters</CODE > parameter can be used to pass an additional parameter to the program configured to use when sending mail using the <TT CLASS="literal" >sendmail_path</TT > configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the <TT CLASS="literal" >-f</TT > sendmail option. </P ><P > The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is <TT CLASS="filename" >/etc/mail/trusted-users</TT >. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN107146" ></A ><H2 >返回值</H2 ><P > Returns <TT CLASS="constant" ><B >TRUE</B ></TT > if the mail was successfully accepted for delivery, <TT CLASS="constant" ><B >FALSE</B ></TT > otherwise. </P ><P > It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN107152" ></A ><H2 >更新日志</H2 ><P > <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN107155" ></A ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >版本</TH ><TH >说明</TH ></TR ></THEAD ><TBODY ><TR ><TD >4.3.0 (Windows only)</TD ><TD > All custom headers (like From, Cc, Bcc and Date) are supported, and are not case-sensitive. (As custom headers are not interpreted by the MTA in the first place, but are parsed by PHP, PHP < 4.3 only supported the Cc header element and was case-sensitive). </TD ></TR ><TR ><TD >4.2.3</TD ><TD > The <CODE CLASS="parameter" >additional_parameters</CODE > parameter is disabled in <A HREF="features.safe-mode.html#ini.safe-mode" >safe_mode</A > and the <B CLASS="function" >mail()</B > function will expose a warning message and return <TT CLASS="constant" ><B >FALSE</B ></TT > when used. </TD ></TR ><TR ><TD >4.0.5</TD ><TD > The <CODE CLASS="parameter" >additional_parameters</CODE > parameter was added. </TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN107176" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN107179" ></A ><P ><B >例 1. Sending mail.</B ></P ><P > Using <B CLASS="function" >mail()</B > to send a simple email: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// The message<br /></font><font color="#0000BB">$message </font><font color="#007700">= </font><font color="#DD0000">"Line 1\nLine 2\nLine 3"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// In case any of our lines are larger than 70 characters, we should use wordwrap()<br /></font><font color="#0000BB">$message </font><font color="#007700">= </font><font color="#0000BB">wordwrap</font><font color="#007700">(</font><font color="#0000BB">$message</font><font color="#007700">, </font><font color="#0000BB">70</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Send<br /></font><font color="#0000BB">mail</font><font color="#007700">(</font><font color="#DD0000">'caffinated@example.com'</font><font color="#007700">, </font><font color="#DD0000">'My Subject'</font><font color="#007700">, </font><font color="#0000BB">$message</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN107184" ></A ><P ><B >例 2. Sending mail with extra headers.</B ></P ><P > The addition of basic headers, telling the MUA the From and Reply-To addresses: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$to </font><font color="#007700">= </font><font color="#DD0000">'nobody@example.com'</font><font color="#007700">;<br /></font><font color="#0000BB">$subject </font><font color="#007700">= </font><font color="#DD0000">'the subject'</font><font color="#007700">;<br /></font><font color="#0000BB">$message </font><font color="#007700">= </font><font color="#DD0000">'hello'</font><font color="#007700">;<br /></font><font color="#0000BB">$headers </font><font color="#007700">= </font><font color="#DD0000">'From: webmaster@example.com' </font><font color="#007700">. </font><font color="#DD0000">"\r\n" </font><font color="#007700">.<br /> </font><font color="#DD0000">'Reply-To: webmaster@example.com' </font><font color="#007700">. </font><font color="#DD0000">"\r\n" </font><font color="#007700">.<br /> </font><font color="#DD0000">'X-Mailer: PHP/' </font><font color="#007700">. </font><font color="#0000BB">phpversion</font><font color="#007700">();<br /><br /></font><font color="#0000BB">mail</font><font color="#007700">(</font><font color="#0000BB">$to</font><font color="#007700">, </font><font color="#0000BB">$subject</font><font color="#007700">, </font><font color="#0000BB">$message</font><font color="#007700">, </font><font color="#0000BB">$headers</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN107188" ></A ><P ><B >例 3. Sending mail with an additional command line parameter.</B ></P ><P > The <CODE CLASS="parameter" >additional_parameters</CODE > parameter can be used to pass an additional parameter to the program configured to use when sending mail using the <TT CLASS="literal" >sendmail_path</TT >. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />mail</font><font color="#007700">(</font><font color="#DD0000">'nobody@example.com'</font><font color="#007700">, </font><font color="#DD0000">'the subject'</font><font color="#007700">, </font><font color="#DD0000">'the message'</font><font color="#007700">, </font><font color="#0000BB">null</font><font color="#007700">,<br /> </font><font color="#DD0000">'-fwebmaster@example.com'</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN107194" ></A ><P ><B >例 4. Sending HTML email</B ></P ><P > It is also possible to send HTML email with <B CLASS="function" >mail()</B >. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// multiple recipients<br /></font><font color="#0000BB">$to </font><font color="#007700">= </font><font color="#DD0000">'aidan@example.com' </font><font color="#007700">. </font><font color="#DD0000">', '</font><font color="#007700">; </font><font color="#FF8000">// note the comma<br /></font><font color="#0000BB">$to </font><font color="#007700">.= </font><font color="#DD0000">'wez@example.com'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// subject<br /></font><font color="#0000BB">$subject </font><font color="#007700">= </font><font color="#DD0000">'Birthday Reminders for August'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// message<br /></font><font color="#0000BB">$message </font><font color="#007700">= </font><font color="#DD0000">'<br /><html><br /><head><br /> <title>Birthday Reminders for August</title><br /></head><br /><body><br /> <p>Here are the birthdays upcoming in August!</p><br /> <table><br /> <tr><br /> <th>Person</th><th>Day</th><th>Month</th><th>Year</th><br /> </tr><br /> <tr><br /> <td>Joe</td><td>3rd</td><td>August</td><td>1970</td><br /> </tr><br /> <tr><br /> <td>Sally</td><td>17th</td><td>August</td><td>1973</td><br /> </tr><br /> </table><br /></body><br /></html><br />'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// To send HTML mail, the Content-type header must be set<br /></font><font color="#0000BB">$headers </font><font color="#007700">= </font><font color="#DD0000">'MIME-Version: 1.0' </font><font color="#007700">. </font><font color="#DD0000">"\r\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$headers </font><font color="#007700">.= </font><font color="#DD0000">'Content-type: text/html; charset=iso-8859-1' </font><font color="#007700">. </font><font color="#DD0000">"\r\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Additional headers<br /></font><font color="#0000BB">$headers </font><font color="#007700">.= </font><font color="#DD0000">'To: Mary <mary@example.com>, Kelly <kelly@example.com>' </font><font color="#007700">. </font><font color="#DD0000">"\r\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$headers </font><font color="#007700">.= </font><font color="#DD0000">'From: Birthday Reminder <birthday@example.com>' </font><font color="#007700">. </font><font color="#DD0000">"\r\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$headers </font><font color="#007700">.= </font><font color="#DD0000">'Cc: birthdayarchive@example.com' </font><font color="#007700">. </font><font color="#DD0000">"\r\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$headers </font><font color="#007700">.= </font><font color="#DD0000">'Bcc: birthdaycheck@example.com' </font><font color="#007700">. </font><font color="#DD0000">"\r\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Mail it<br /></font><font color="#0000BB">mail</font><font color="#007700">(</font><font color="#0000BB">$to</font><font color="#007700">, </font><font color="#0000BB">$subject</font><font color="#007700">, </font><font color="#0000BB">$message</font><font color="#007700">, </font><font color="#0000BB">$headers</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package <A HREF="http://pear.php.net/package/Mail" TARGET="_top" >PEAR::Mail</A >. </P ></BLOCKQUOTE ></DIV > </P ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN107203" ></A ><H2 >注释</H2 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The Windows implementation of <B CLASS="function" >mail()</B > differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a <TT CLASS="literal" >MTA</TT > is needed listening on a network socket (which can either on the localhost or a remote machine). </P ><P > Second, the custom headers like <TT CLASS="literal" >From:</TT >, <TT CLASS="literal" >Cc:</TT >, <TT CLASS="literal" >Bcc:</TT > and <TT CLASS="literal" >Date:</TT > are <SPAN CLASS="strong" ><B CLASS="emphasis" >not</B ></SPAN > interpreted by the <TT CLASS="literal" >MTA</TT > in the first place, but are parsed by PHP. </P ><P > As such, the <CODE CLASS="parameter" >to</CODE > parameter should not be an address in the form of "Something <someone@example.com>". The mail command may not parse this properly while talking with the MTA. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > Email with attachments and special types of content (e.g. HTML) can be sent using this function. This is accomplished via MIME-encoding - for more information, see this <A HREF="http://www.zend.com/zend/spotlight/sendmimeemailpart1.php" TARGET="_top" > Zend article</A > or the <A HREF="http://pear.php.net/package/Mail_Mime" TARGET="_top" > PEAR Mime Classes</A >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > It is worth noting that the <B CLASS="function" >mail()</B > function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient. </P ><P > For the sending of large amounts of email, see the <A HREF="http://pear.php.net/package/Mail" TARGET="_top" >PEAR::Mail</A >, and <A HREF="http://pear.php.net/package/Mail_Queue" TARGET="_top" >PEAR::Mail_Queue</A > packages. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The following RFCs may be useful: <A HREF="http://www.faqs.org/rfcs/rfc1896" TARGET="_top" >RFC 1896</A >, <A HREF="http://www.faqs.org/rfcs/rfc2045" TARGET="_top" >RFC 2045</A >, <A HREF="http://www.faqs.org/rfcs/rfc2046" TARGET="_top" >RFC 2046</A >, <A HREF="http://www.faqs.org/rfcs/rfc2047" TARGET="_top" >RFC 2047</A >, <A HREF="http://www.faqs.org/rfcs/rfc2048" TARGET="_top" >RFC 2048</A >, <A HREF="http://www.faqs.org/rfcs/rfc2049" TARGET="_top" >RFC 2049</A >, and <A HREF="http://www.faqs.org/rfcs/rfc2822" TARGET="_top" >RFC 2822</A >. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN107237" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.imap-mail.html" ><B CLASS="function" >imap_mail()</B ></A ></TD ></TR ><TR ><TD ><A HREF="http://pear.php.net/package/Mail" TARGET="_top" >PEAR::Mail</A ></TD ></TR ><TR ><TD ><A HREF="http://pear.php.net/package/Mail_Mime" TARGET="_top" >PEAR::Mail_Mime</A ></TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.ezmlm-hash.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="ref.mailparse.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >ezmlm_hash</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.mail.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Mailparse Functions</TD ></TR ></TABLE ></DIV ></BODY ></HTML >