2003-07-20 15:49:23 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2009-07-11 07:54:14 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<refentry xml:id="function.mail" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
2005-02-14 02:45:46 +00:00
|
|
|
<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>string</type><parameter>additional_headers</parameter></methodparam>
|
|
|
|
<methodparam choice="opt"><type>string</type><parameter>additional_parameters</parameter></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
|
2007-06-20 22:25:43 +00:00
|
|
|
<link xlink:href="&url.rfc;2822">RFC 2822</link>. Some examples are:
|
2005-02-14 02:45:46 +00:00
|
|
|
<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>
|
2008-11-05 09:38:39 +00:00
|
|
|
Subject must satisfy <link xlink:href="&url.rfc;2047">RFC 2047</link>.
|
2005-02-14 02:45:46 +00:00
|
|
|
</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 LF (\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,
|
2005-02-14 03:37:34 +00:00
|
|
|
replace these occurrences with a double dot.
|
2005-02-14 02:45:46 +00:00
|
|
|
<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>
|
|
|
|
String 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).
|
|
|
|
</para>
|
2005-04-28 12:57:53 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
When sending mail, the mail <emphasis>must</emphasis> contain
|
2005-04-28 21:21:06 +00:00
|
|
|
a <literal>From</literal> header. This can be set with the
|
2005-04-28 12:57:53 +00:00
|
|
|
<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>.
|
2006-04-26 11:44:15 +00:00
|
|
|
The <literal>From</literal> header sets also
|
|
|
|
<literal>Return-Path</literal> under Windows.
|
2005-04-28 12:57:53 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
<note>
|
|
|
|
<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
|
2007-06-20 22:25:43 +00:00
|
|
|
<link xlink:href="&url.rfc;2822">RFC 2822</link>.
|
2005-04-28 12:57:53 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
2005-02-14 02:45:46 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>additional_parameters</parameter> (optional)</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The <parameter>additional_parameters</parameter> parameter
|
2009-04-25 15:12:50 +00:00
|
|
|
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.
|
2005-02-14 02:45:46 +00:00
|
|
|
</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
|
2005-02-14 03:37:34 +00:00
|
|
|
to the message when the envelope sender (-f) is set using this method.
|
2005-02-14 02:45:46 +00:00
|
|
|
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>4.3.0 (Windows only)</entry>
|
|
|
|
<entry>
|
|
|
|
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).
|
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>4.2.3</entry>
|
|
|
|
<entry>
|
|
|
|
The <parameter>additional_parameters</parameter> parameter is disabled in
|
|
|
|
<link linkend="ini.safe-mode">safe_mode</link> and the
|
|
|
|
<function>mail</function> function will expose a warning message
|
|
|
|
and return &false; when used.
|
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>4.0.5</entry>
|
|
|
|
<entry>
|
|
|
|
The <parameter>additional_parameters</parameter> parameter was added.
|
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</informaltable>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Sending mail.</title>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
2005-02-14 02:45:46 +00:00
|
|
|
Using <function>mail</function> to send a simple email:
|
2002-04-15 00:12:54 +00:00
|
|
|
</para>
|
2005-02-14 02:45:46 +00:00
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-05-14 06:11:49 +00:00
|
|
|
<?php
|
2005-02-14 02:45:46 +00:00
|
|
|
// The message
|
|
|
|
$message = "Line 1\nLine 2\nLine 3";
|
|
|
|
|
|
|
|
// In case any of our lines are larger than 70 characters, we should use wordwrap()
|
|
|
|
$message = wordwrap($message, 70);
|
|
|
|
|
|
|
|
// Send
|
2008-12-28 00:21:18 +00:00
|
|
|
mail('caffeinated@example.com', 'My Subject', $message);
|
2003-05-14 06:11:49 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2005-02-14 02:45:46 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title>Sending mail with extra headers.</title>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
2005-02-14 02:45:46 +00:00
|
|
|
The addition of basic headers, telling the MUA
|
|
|
|
the From and Reply-To addresses:
|
|
|
|
</para>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-05-14 06:11:49 +00:00
|
|
|
<?php
|
2005-02-14 02:45:46 +00:00
|
|
|
$to = 'nobody@example.com';
|
|
|
|
$subject = 'the subject';
|
|
|
|
$message = 'hello';
|
2005-04-28 12:57:53 +00:00
|
|
|
$headers = 'From: webmaster@example.com' . "\r\n" .
|
|
|
|
'Reply-To: webmaster@example.com' . "\r\n" .
|
2005-02-14 02:45:46 +00:00
|
|
|
'X-Mailer: PHP/' . phpversion();
|
|
|
|
|
|
|
|
mail($to, $subject, $message, $headers);
|
2003-05-14 06:11:49 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2005-02-14 02:45:46 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title>Sending mail with an additional command line parameter.</title>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
2003-07-20 15:49:23 +00:00
|
|
|
The <parameter>additional_parameters</parameter> parameter
|
|
|
|
can be used to pass an additional parameter to the program configured
|
2005-02-14 02:45:46 +00:00
|
|
|
to use when sending mail using the <literal>sendmail_path</literal>.
|
|
|
|
</para>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-05-14 06:11:49 +00:00
|
|
|
<?php
|
2005-02-14 02:45:46 +00:00
|
|
|
mail('nobody@example.com', 'the subject', 'the message', null,
|
2005-04-28 12:57:53 +00:00
|
|
|
'-fwebmaster@example.com');
|
2003-05-14 06:11:49 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2005-02-14 02:45:46 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title>Sending HTML email</title>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
2005-02-14 02:45:46 +00:00
|
|
|
It is also possible to send HTML email with <function>mail</function>.
|
|
|
|
</para>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-05-14 06:11:49 +00:00
|
|
|
<?php
|
2005-02-14 02:45:46 +00:00
|
|
|
// multiple recipients
|
|
|
|
$to = 'aidan@example.com' . ', '; // note the comma
|
|
|
|
$to .= 'wez@example.com';
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2005-02-14 02:45:46 +00:00
|
|
|
// subject
|
|
|
|
$subject = 'Birthday Reminders for August';
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2005-02-14 02:45:46 +00:00
|
|
|
// message
|
2002-04-15 00:12:54 +00:00
|
|
|
$message = '
|
|
|
|
<html>
|
|
|
|
<head>
|
2005-02-14 02:45:46 +00:00
|
|
|
<title>Birthday Reminders for August</title>
|
2002-04-15 00:12:54 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
2005-02-14 02:45:46 +00:00
|
|
|
<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>Joe</td><td>3rd</td><td>August</td><td>1970</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2002-04-15 00:12:54 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
';
|
|
|
|
|
2005-02-14 02:45:46 +00:00
|
|
|
// To send HTML mail, the Content-type header must be set
|
|
|
|
$headers = 'MIME-Version: 1.0' . "\r\n";
|
|
|
|
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2005-02-14 02:45:46 +00:00
|
|
|
// Additional headers
|
|
|
|
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
|
|
|
|
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
|
|
|
|
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
|
|
|
|
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2005-02-14 02:45:46 +00:00
|
|
|
// Mail it
|
2002-04-15 00:12:54 +00:00
|
|
|
mail($to, $subject, $message, $headers);
|
2003-05-14 06:11:49 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2005-02-14 02:45:46 +00:00
|
|
|
</programlisting>
|
2002-08-14 00:27:00 +00:00
|
|
|
<para>
|
2005-02-14 02:45:46 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
If intending to send HTML or otherwise Complex mails, it is recommended
|
2007-06-20 22:25:43 +00:00
|
|
|
to use the PEAR package <link xlink:href="&url.pear.package;Mail_Mime">PEAR::Mail_Mime</link>.
|
2005-02-14 02:45:46 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
2002-08-14 00:27:00 +00:00
|
|
|
</para>
|
2005-02-14 02:45:46 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="notes">
|
|
|
|
&reftitle.notes;
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
The Windows implementation of <function>mail</function> 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
|
|
|
|
<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>
|
|
|
|
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
|
2007-06-20 22:25:43 +00:00
|
|
|
<link xlink:href="&url.email.mime.zend;">
|
|
|
|
Zend article</link> or the <link xlink:href="&url.pear.package;Mail_Mime">
|
|
|
|
PEAR Mime Classes</link>.
|
2005-02-14 02:45:46 +00:00
|
|
|
</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
|
2007-06-20 22:25:43 +00:00
|
|
|
<link xlink:href="&url.pear.package;Mail">PEAR::Mail</link>, and
|
|
|
|
<link xlink:href="&url.pear.package;Mail_Queue">PEAR::Mail_Queue</link> packages.
|
2005-02-14 02:45:46 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
2005-02-14 03:37:34 +00:00
|
|
|
The following RFCs may be useful:
|
2007-06-20 22:25:43 +00:00
|
|
|
<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>.
|
2005-02-14 02:45:46 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
|
|
|
<para>
|
|
|
|
<simplelist>
|
|
|
|
<member><function>imap_mail</function></member>
|
2007-06-20 22:25:43 +00:00
|
|
|
<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>
|
2005-02-14 02:45:46 +00:00
|
|
|
</simplelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
<!-- 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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-04-15 00:12:54 +00:00
|
|
|
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
|
|
|
|
-->
|