📄 function.mail.html
字号:
<table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0 (Windows only)</td> <td colspan="1" rowspan="1" align="left"> 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 valign="middle"> <td colspan="1" rowspan="1" align="left">4.2.3</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">additional_parameters</tt></i> parameter is disabled in <a href="features.safe-mode.html#ini.safe-mode" class="link">safe_mode</a> and the <b>mail()</b> function will expose a warning message and return <b><tt>FALSE</tt></b> when used. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.0.5</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">additional_parameters</tt></i> parameter was added. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Sending mail.</b></p> <div class="example-contents"><p> Using <b>mail()</b> to send a simple email: </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// The message<br /></span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #DD0000">"Line 1\nLine 2\nLine 3"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// In case any of our lines are larger than 70 characters, we should use wordwrap()<br /></span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #0000BB">wordwrap</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">70</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Send<br /></span><span style="color: #0000BB">mail</span><span style="color: #007700">(</span><span style="color: #DD0000">'caffinated@example.com'</span><span style="color: #007700">, </span><span style="color: #DD0000">'My Subject'</span><span style="color: #007700">, </span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example"> <p><b>Example #2 Sending mail with extra headers.</b></p> <div class="example-contents"><p> The addition of basic headers, telling the MUA the From and Reply-To addresses: </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$to </span><span style="color: #007700">= </span><span style="color: #DD0000">'nobody@example.com'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$subject </span><span style="color: #007700">= </span><span style="color: #DD0000">'the subject'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #DD0000">'hello'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$headers </span><span style="color: #007700">= </span><span style="color: #DD0000">'From: webmaster@example.com' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\r\n" </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">'Reply-To: webmaster@example.com' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\r\n" </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">'X-Mailer: PHP/' </span><span style="color: #007700">. </span><span style="color: #0000BB">phpversion</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">mail</span><span style="color: #007700">(</span><span style="color: #0000BB">$to</span><span style="color: #007700">, </span><span style="color: #0000BB">$subject</span><span style="color: #007700">, </span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">$headers</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example"> <p><b>Example #3 Sending mail with an additional command line parameter.</b></p> <div class="example-contents"><p> The <i><tt class="parameter">additional_parameters</tt></i> parameter can be used to pass an additional parameter to the program configured to use when sending mail using the <i>sendmail_path</i>. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />mail</span><span style="color: #007700">(</span><span style="color: #DD0000">'nobody@example.com'</span><span style="color: #007700">, </span><span style="color: #DD0000">'the subject'</span><span style="color: #007700">, </span><span style="color: #DD0000">'the message'</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'-fwebmaster@example.com'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example"> <p><b>Example #4 Sending HTML email</b></p> <div class="example-contents"><p> It is also possible to send HTML email with <b>mail()</b>. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// multiple recipients<br /></span><span style="color: #0000BB">$to </span><span style="color: #007700">= </span><span style="color: #DD0000">'aidan@example.com' </span><span style="color: #007700">. </span><span style="color: #DD0000">', '</span><span style="color: #007700">; </span><span style="color: #FF8000">// note the comma<br /></span><span style="color: #0000BB">$to </span><span style="color: #007700">.= </span><span style="color: #DD0000">'wez@example.com'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// subject<br /></span><span style="color: #0000BB">$subject </span><span style="color: #007700">= </span><span style="color: #DD0000">'Birthday Reminders for August'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// message<br /></span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="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 />'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// To send HTML mail, the Content-type header must be set<br /></span><span style="color: #0000BB">$headers </span><span style="color: #007700">= </span><span style="color: #DD0000">'MIME-Version: 1.0' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$headers </span><span style="color: #007700">.= </span><span style="color: #DD0000">'Content-type: text/html; charset=iso-8859-1' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\r\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Additional headers<br /></span><span style="color: #0000BB">$headers </span><span style="color: #007700">.= </span><span style="color: #DD0000">'To: Mary <mary@example.com>, Kelly <kelly@example.com>' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$headers </span><span style="color: #007700">.= </span><span style="color: #DD0000">'From: Birthday Reminder <birthday@example.com>' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$headers </span><span style="color: #007700">.= </span><span style="color: #DD0000">'Cc: birthdayarchive@example.com' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$headers </span><span style="color: #007700">.= </span><span style="color: #DD0000">'Bcc: birthdaycheck@example.com' </span><span style="color: #007700">. </span><span style="color: #DD0000">"\r\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Mail it<br /></span><span style="color: #0000BB">mail</span><span style="color: #007700">(</span><span style="color: #0000BB">$to</span><span style="color: #007700">, </span><span style="color: #0000BB">$subject</span><span style="color: #007700">, </span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">$headers</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> <blockquote><p><b class="note">Note</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_Mime" class="link external">» PEAR::Mail_Mime</a>. <br /> </p></blockquote> </p></div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: The Windows implementation of <b>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 <i>MTA</i> is needed listening on a network socket (which can either on the localhost or a remote machine). <br /> Second, the custom headers like <i>From:</i>, <i>Cc:</i>, <i>Bcc:</i> and <i>Date:</i> are <em class="emphasis">not</em> interpreted by the <i>MTA</i> in the first place, but are parsed by PHP. <br /> As such, the <i><tt class="parameter">to</tt></i> 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. <br /> </p></blockquote> <blockquote><p><b class="note">Note</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" class="link external">» Zend article</a> or the <a href="http://pear.php.net/package/Mail_Mime" class="link external">» PEAR Mime Classes</a>. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: It is worth noting that the <b>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. <br /> For the sending of large amounts of email, see the <a href="http://pear.php.net/package/Mail" class="link external">» PEAR::Mail</a>, and <a href="http://pear.php.net/package/Mail_Queue" class="link external">» PEAR::Mail_Queue</a> packages. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: The following RFCs may be useful: <a href="http://www.faqs.org/rfcs/rfc1896" class="link external">» RFC 1896</a>, <a href="http://www.faqs.org/rfcs/rfc2045" class="link external">» RFC 2045</a>, <a href="http://www.faqs.org/rfcs/rfc2046" class="link external">» RFC 2046</a>, <a href="http://www.faqs.org/rfcs/rfc2047" class="link external">» RFC 2047</a>, <a href="http://www.faqs.org/rfcs/rfc2048" class="link external">» RFC 2048</a>, <a href="http://www.faqs.org/rfcs/rfc2049" class="link external">» RFC 2049</a>, and <a href="http://www.faqs.org/rfcs/rfc2822" class="link external">» RFC 2822</a>. <br /> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.imap-mail.html" class="function" rel="rdfs-seeAlso">imap_mail()</a></li> <li class="member"><a href="http://pear.php.net/package/Mail" class="link external">» PEAR::Mail</a></li> <li class="member"><a href="http://pear.php.net/package/Mail_Mime" class="link external">» PEAR::Mail_Mime</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.mail.html">Mail Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.mailparse.html">Mailparse</a></div> <div class="up"><a href="ref.mail.html">Mail Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -