mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
368 lines
22 KiB
HTML
368 lines
22 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<title>发送邮件</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.mail" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">mail</h1>
|
||
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">mail</span> — <span class="dc-title">发送邮件</span></p>
|
||
|
||
</div>
|
||
|
||
<div class="refsect1 description" id="refsect1-function.mail-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>mail</strong></span>
|
||
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$to</code></span>
|
||
, <span class="methodparam"><span class="type">string</span> <code class="parameter">$subject</code></span>
|
||
, <span class="methodparam"><span class="type">string</span> <code class="parameter">$message</code></span>
|
||
[, <span class="methodparam"><span class="type">string</span> <code class="parameter">$additional_headers</code></span>
|
||
[, <span class="methodparam"><span class="type">string</span> <code class="parameter">$additional_parameters</code></span>
|
||
]] ) : <span class="type">bool</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
发送一封电子邮件。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.mail-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">to</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
电子邮件收件人,或收件人列表。
|
||
</p>
|
||
<p class="para">
|
||
本字符串的格式必须符合
|
||
<a href="http://www.faqs.org/rfcs/rfc2822" class="link external">» RFC 2822</a>。例如:
|
||
<ul class="simplelist">
|
||
<li class="member">user@example.com</li>
|
||
<li class="member">user@example.com, anotheruser@example.com</li>
|
||
<li class="member">User <user@example.com></li>
|
||
<li class="member">User <user@example.com>, Another User <anotheruser@example.com></li>
|
||
</ul>
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">subject</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
电子邮件的主题。
|
||
</p>
|
||
<div class="caution"><strong class="caution">Caution</strong>
|
||
<p class="para">
|
||
本项不能包含任何换行符,否则邮件可能无法正确发送。
|
||
</p>
|
||
</div>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">message</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
所要发送的消息。
|
||
</p>
|
||
<p class="para">
|
||
行之间必须以一个 LF(\n)分隔。每行不能超过 70 个字符。
|
||
</p>
|
||
<div class="caution"><strong class="caution">Caution</strong>
|
||
<p class="para">
|
||
(Windows 下)当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉。要避免此问题,将单个句号替换成两个句号。
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$text </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"\n."</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n.."</span><span style="color: #007700">, </span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</p>
|
||
</div>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">additional_headers</code>(可选项)</dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
String to be inserted at the end of the email header.
|
||
</p>
|
||
<p class="para">
|
||
This is typically used to add extra headers (From, Cc, and Bcc).
|
||
Multiple extra headers should be separated with a CRLF (\r\n).
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
When sending mail, the mail <em class="emphasis">must</em> contain
|
||
a <em>From</em> header. This can be set with the
|
||
<code class="parameter">additional_headers</code> parameter, or a default
|
||
can be set in <var class="filename">php.ini</var>.
|
||
</p>
|
||
<p class="para">
|
||
Failing to do this will result in an error
|
||
message similar to <em>Warning: mail(): "sendmail_from" not
|
||
set in php.ini or custom "From:" header missing</em>.
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
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" class="link external">» RFC 2822</a>.
|
||
</p>
|
||
</p></blockquote>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">additional_parameters</code> (optional)</dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
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 <em>sendmail_path</em>
|
||
configuration setting. For example, this can be used to set the
|
||
envelope sender address when using sendmail with the
|
||
<em>-f</em> sendmail option.
|
||
</p>
|
||
<p class="para">
|
||
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 <var class="filename">/etc/mail/trusted-users</var>.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.mail-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
Returns <strong><code>TRUE</code></strong> if the mail was successfully accepted for delivery, <strong><code>FALSE</code></strong> otherwise.
|
||
</p>
|
||
<p class="para">
|
||
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 changelog" id="refsect1-function.mail-changelog">
|
||
<h3 class="title">更新日志</h3>
|
||
<p class="para">
|
||
<table class="doctable informaltable">
|
||
|
||
<thead>
|
||
<tr>
|
||
<th>版本</th>
|
||
<th>说明</th>
|
||
</tr>
|
||
|
||
</thead>
|
||
|
||
<tbody class="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="ini.sect.safe-mode.html#ini.safe-mode" class="link">safe_mode</a> and the
|
||
<span class="function"><strong>mail()</strong></span> function will expose a warning message
|
||
and return <strong><code>FALSE</code></strong> 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>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.mail-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-4043">
|
||
<p><strong>Example #1 Sending mail.</strong></p>
|
||
<div class="example-contents"><p>
|
||
Using <span class="function"><strong>mail()</strong></span> to send a simple email:
|
||
</p></div>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
<div class="example" id="example-4044">
|
||
<p><strong>Example #2 Sending mail with extra headers.</strong></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"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
<div class="example" id="example-4045">
|
||
<p><strong>Example #3 Sending mail with an additional command line parameter.</strong></p>
|
||
<div class="example-contents"><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 <em>sendmail_path</em>.
|
||
</p></div>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
<div class="example" id="example-4046">
|
||
<p><strong>Example #4 Sending HTML email</strong></p>
|
||
<div class="example-contents"><p>
|
||
It is also possible to send HTML email with <span class="function"><strong>mail()</strong></span>.
|
||
</p></div>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
<div class="example-contents"><p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
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" class="link external">» PEAR::Mail</a>.
|
||
</p>
|
||
</p></blockquote>
|
||
</p></div>
|
||
</div>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 notes" id="refsect1-function.mail-notes">
|
||
<h3 class="title">注释</h3>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
The Windows implementation of <span class="function"><strong>mail()</strong></span> 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
|
||
<em>MTA</em> is needed listening on a network socket (which
|
||
can either on the localhost or a remote machine).
|
||
</p>
|
||
<p class="para">
|
||
Second, the custom headers like
|
||
<em>From:</em>,
|
||
<em>Cc:</em>,
|
||
<em>Bcc:</em> and
|
||
<em>Date:</em> are
|
||
<em class="emphasis">not</em> interpreted by the
|
||
<em>MTA</em> in the first place, but are parsed by PHP.
|
||
</p>
|
||
<p class="para">
|
||
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>
|
||
</p></blockquote>
|
||
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
It is worth noting that the <span class="function"><strong>mail()</strong></span> 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 class="para">
|
||
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.
|
||
</p>
|
||
</p></blockquote>
|
||
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
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>.
|
||
</p>
|
||
</p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 seealso" id="refsect1-function.mail-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="imap_mail.html" class="function" rel="rdfs-seeAlso">imap_mail()</a> - Send an email message</span></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></div></div></body></html> |