-
Notifications
You must be signed in to change notification settings - Fork 786
/
Copy pathmail.xml
433 lines (415 loc) · 13.6 KB
/
mail.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.mail" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>mail</refname>
<refpurpose>Send mail</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>bool</type><methodname>mail</methodname>
<methodparam><type>string</type><parameter>to</parameter></methodparam>
<methodparam><type>string</type><parameter>subject</parameter></methodparam>
<methodparam><type>string</type><parameter>message</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>array</type><type>string</type></type><parameter>additional_headers</parameter><initializer>[]</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>additional_params</parameter><initializer>""</initializer></methodparam>
</methodsynopsis>
<para>
Sends an email.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>to</parameter></term>
<listitem>
<para>
Receiver, or receivers of the mail.
</para>
<para>
The formatting of this string must comply with
<link xlink:href="&url.rfc;2822">RFC 2822</link>. Some examples are:
<simplelist>
<member>user@example.com</member>
<member>user@example.com, anotheruser@example.com</member>
<member>User <user@example.com></member>
<member>User <user@example.com>, Another User <anotheruser@example.com></member>
</simplelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>subject</parameter></term>
<listitem>
<para>
Subject of the email to be sent.
</para>
<caution>
<para>
Subject must satisfy <link xlink:href="&url.rfc;2047">RFC 2047</link>.
</para>
</caution>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>message</parameter></term>
<listitem>
<para>
Message to be sent.
</para>
<para>
Each line should be separated with a CRLF (\r\n). Lines should not be
larger than 70 characters.
</para>
<caution>
<para>
(Windows only) When PHP is talking to a SMTP server directly, if a full
stop is found on the start of a line, it is removed. To counter-act this,
replace these occurrences with a double dot.
<programlisting role="php">
<![CDATA[
<?php
$text = str_replace("\n.", "\n..", $text);
?>
]]>
</programlisting>
</para>
</caution>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>additional_headers</parameter> (optional)</term>
<listitem>
<para>
<type>String</type> or <type>array</type> to be inserted at the end of the email header.
</para>
<para>
This is typically used to add extra headers (From, Cc, and Bcc).
Multiple extra headers should be separated with a CRLF (\r\n).
If outside data are used to compose this header, the data should be sanitized
so that no unwanted headers could be injected.
</para>
<para>
If an <type>array</type> is passed, its keys are the header names and its
values are the respective header values.
</para>
<note>
<para>
When sending mail, the mail <emphasis>must</emphasis> contain
a <literal>From</literal> header. This can be set with the
<parameter>additional_headers</parameter> parameter, or a default
can be set in &php.ini;.
</para>
<para>
Failing to do this will result in an error
message similar to <literal>Warning: mail(): "sendmail_from" not
set in php.ini or custom "From:" header missing</literal>.
The <literal>From</literal> header sets also
<literal>Return-Path</literal> when sending directly via SMTP (Windows only).
</para>
</note>
<note>
<para>
If messages are not received, try using a LF (\n) only.
Some Unix mail transfer agents (most notably
<link xlink:href="&url.qmail;">qmail</link>) 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
<link xlink:href="&url.rfc;2822">RFC 2822</link>.
</para>
</note>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>additional_params</parameter> (optional)</term>
<listitem>
<para>
The <parameter>additional_params</parameter> parameter
can be used to pass additional flags as command line options to the
program configured to be used when sending mail, as defined by the
<literal>sendmail_path</literal> configuration setting. For example,
this can be used to set the envelope sender address when using
sendmail with the <literal>-f</literal> sendmail option.
</para>
<para>
This parameter is escaped by <function>escapeshellcmd</function> internally
to prevent command execution. <function>escapeshellcmd</function> prevents
command execution, but allows to add additional parameters. For security reasons,
it is recommended for the user to sanitize this parameter to avoid adding unwanted
parameters to the shell command.
</para>
<para>
Since <function>escapeshellcmd</function> is applied automatically, some characters
that are allowed as email addresses by internet RFCs cannot be used.
<function>mail</function> can not allow such characters, so in programs where the use of
such characters is required, alternative means of sending emails (such as using a framework
or a library) is recommended.
</para>
<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 <filename>/etc/mail/trusted-users</filename>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns &true; if the mail was successfully accepted for delivery, &false; otherwise.
</para>
<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.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>7.2.0</entry>
<entry>
The <parameter>additional_headers</parameter> parameter now also accepts
an <type>array</type>.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Sending mail.</title>
<para>
Using <function>mail</function> to send a simple email:
</para>
<programlisting role="php">
<![CDATA[
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('caffeinated@example.com', 'My Subject', $message);
?>
]]>
</programlisting>
</example>
<example>
<title>Sending mail with extra headers.</title>
<para>
The addition of basic headers, telling the MUA
the From and Reply-To addresses:
</para>
<programlisting role="php">
<![CDATA[
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
]]>
</programlisting>
</example>
<example>
<title>Sending mail with extra headers as <type>array</type></title>
<para>
This example sends the same mail as the example immediately above, but
passes the additional headers as array (available as of PHP 7.2.0).
</para>
<programlisting role="php">
<![CDATA[
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = array(
'From' => 'webmaster@example.com',
'Reply-To' => 'webmaster@example.com',
'X-Mailer' => 'PHP/' . phpversion()
);
mail($to, $subject, $message, $headers);
?>
]]>
</programlisting>
</example>
<example>
<title>Sending mail with an additional command line parameter.</title>
<para>
The <parameter>additional_params</parameter> parameter
can be used to pass an additional parameter to the program configured
to use when sending mail using the <literal>sendmail_path</literal>.
</para>
<programlisting role="php">
<![CDATA[
<?php
mail('nobody@example.com', 'the subject', 'the message', null,
'-fwebmaster@example.com');
?>
]]>
</programlisting>
</example>
<example>
<title>Sending HTML email</title>
<para>
It is also possible to send HTML email with <function>mail</function>.
</para>
<programlisting role="php">
<![CDATA[
<?php
// Multiple recipients
$to = 'johny@example.com, sally@example.com'; // note the comma
// Subject
$subject = 'Birthday Reminders for August';
// Message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Johny</td><td>10th</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: Mary <mary@example.com>, Kelly <kelly@example.com>';
$headers[] = 'From: Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc: birthdayarchive@example.com';
$headers[] = 'Bcc: birthdaycheck@example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
?>
]]>
</programlisting>
</example>
</para>
<para>
<note>
<para>
If intending to send HTML or otherwise Complex mails, it is recommended
to use the PEAR package <link xlink:href="&url.pear.package;Mail_Mime">PEAR::Mail_Mime</link>.
</para>
</note>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
The SMTP implementation (Windows only) of <function>mail</function> differs in many
ways from the sendmail implementation. First, it doesn't use a local binary
for composing messages but only operates on direct sockets which means a
<literal>MTA</literal> is needed listening on a network socket (which
can either on the localhost or a remote machine).
</para>
<para>
Second, the custom headers like
<literal>From:</literal>,
<literal>Cc:</literal>,
<literal>Bcc:</literal> and
<literal>Date:</literal> are
<emphasis role="strong">not</emphasis> interpreted by the
<literal>MTA</literal> in the first place, but are parsed by PHP.
</para>
<para>
As such, the <parameter>to</parameter> 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.
</para>
</note>
<note>
<para>
It is worth noting that the <function>mail</function> 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.
</para>
<para>
For the sending of large amounts of email, see the
<link xlink:href="&url.pear.package;Mail">PEAR::Mail</link>, and
<link xlink:href="&url.pear.package;Mail_Queue">PEAR::Mail_Queue</link> packages.
</para>
</note>
<note>
<para>
The following RFCs may be useful:
<link xlink:href="&url.rfc;1896">RFC 1896</link>,
<link xlink:href="&url.rfc;2045">RFC 2045</link>,
<link xlink:href="&url.rfc;2046">RFC 2046</link>,
<link xlink:href="&url.rfc;2047">RFC 2047</link>,
<link xlink:href="&url.rfc;2048">RFC 2048</link>,
<link xlink:href="&url.rfc;2049">RFC 2049</link>, and
<link xlink:href="&url.rfc;2822">RFC 2822</link>.
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mb_send_mail</function></member>
<member><function>imap_mail</function></member>
<member><link xlink:href="&url.pear.package;Mail">PEAR::Mail</link></member>
<member><link xlink:href="&url.pear.package;Mail_Mime">PEAR::Mail_Mime</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->