|
|
mail (PHP 3, PHP 4, PHP 5) mail -- Send mail Описаниеbool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )
Sends an email.
Список параметров
to
Receiver, or receivers of the mail.
The formatting of this string must comply with
RFC 2822. Some examples are:
| user@example.com | | user@example.com, anotheruser@example.com | | User <user@example.com> | | User <user@example.com>, Another User <anotheruser@example.com> |
subject
Subject of the email to be sent.
| Предостережение |
This must not contain any newline characters, or the mail may not be
sent properly.
|
message
Message to be sent.
Each line should be separated with a LF (\n). Lines should not be larger
than 70 characters.
| Предостережение |
(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.
|
<?php
$text = str_replace("\n.", "\n..", $text);
?>
|
|
additional_headers (optional)
String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc).
Multiple extra headers should be separated with a CRLF (\r\n).
Замечание:
When sending mail, the mail must contain
a From header. This can be set with the
additional_headers parameter, or a default
can be set in php.ini.
Failing to do this will result in an error
message similar to Warning: mail(): "sendmail_from" not
set in php.ini or custom "From:" header missing.
The From header sets also
Return-Path under Windows.
Замечание:
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
RFC 2822.
additional_parameters (optional)
The additional_parameters parameter
can be used to pass an additional parameter to the program configured
to use when sending mail using the sendmail_path
configuration setting. For example, this can be used to set the
envelope sender address when using sendmail with the
-f sendmail option.
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 /etc/mail/trusted-users.
Возвращаемые значения
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
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.
Примеры
Пример 1. Sending mail.
Using mail() to send a simple email:
|
<?php
$message = "Line 1\nLine 2\nLine 3";
$message = wordwrap($message, 70);
mail('caffinated@example.com', 'My Subject', $message);
?>
|
|
Пример 2. Sending mail with extra headers.
The addition of basic headers, telling the MUA
the From and Reply-To addresses:
|
<?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);
?>
|
|
Пример 3. Sending mail with an additional command line parameter.
The additional_parameters parameter
can be used to pass an additional parameter to the program configured
to use when sending mail using the sendmail_path.
|
<?php
mail('nobody@example.com', 'the subject', 'the message', null,
'-fwebmaster@example.com');
?>
|
|
Пример 4. Sending HTML email
It is also possible to send HTML email with mail().
|
<?php
$to = 'aidan@example.com' . ', '; $to .= 'wez@example.com';
$subject = 'Birthday Reminders for August';
$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>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>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$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";
mail($to, $subject, $message, $headers);
?>
|
Замечание:
If intending to send HTML or otherwise Complex mails, it is recommended
to use the PEAR package PEAR::Mail_Mime.
|
ПримечанияЗамечание:
The Windows implementation of mail() 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
MTA is needed listening on a network socket (which
can either on the localhost or a remote machine).
Second, the custom headers like
From:,
Cc:,
Bcc: and
Date: are
not interpreted by the
MTA in the first place, but are parsed by PHP.
As such, the to 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.
Замечание:
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
Zend article or the
PEAR Mime Classes.
Замечание:
It is worth noting that the mail() 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.
For the sending of large amounts of email, see the
PEAR::Mail, and
PEAR::Mail_Queue packages.
hn at nesland dot net
02-Nov-2007 05:37
Tired of idiots and imbeciles who creates unsecure php-code and lets spammers abuse mail()? Try this dirty trick:
With auto_prepend, prepend this file:
<?php
dl("runkit.so");
runkit_function_copy ( "mail","intmail" );
runkit_function_remove( "mail" );
function mail( $to, $subject, $message, $additional_headers = null, $additional_parameters = null ) {
$___domain = $_SERVER['SERVER_NAME'];
$fp = fopen("/tmp/my_super_mail_logg", "a");
fwrite( $fp, date("d.m.y H:i:s") . " " . $___domain . ": $to / $subject\n");
fclose( $fp );
return intmail( $to, $subject, $message, $additional_headers, $additional_parameters );
}
?>
You probably shouldn't log to /tmp, or any other place as the webserver-user, see syslog-functions ;)
And of course you can manipulate the different parameters, like adding custom headers to each email (For instance; "X-From-Web: {$_SERVER['SERVER_NAME']}")..
Ben
03-Oct-2007 11:14
There was a comment that
mail("User Name <username@email.com>","Subject Here",$msg,"From: us@mysite.com");
does not work. I've always used that and never had any issues - from Linux servers. I don't see how this could be different in IE vs Firefox; I've always gotten the same result in both. Just tried it on a Windows server and got this as a bounce back:
<User Name <username@email.com>:
x.x.x.x does not like recipient.
Remote host said: 550 Requested action not taken: 550 No such recipient
Giving up on x.x.x.x.
(Details changed to protect the innocent/guilty (for using a Windows server))
Took me a while to find the bounce until I used ini_set('sendmail_from', 'my@account');
So it is probably trying to deliver to "User Name <username" instead of simply "username".
largo at email dot pcleak dot com
03-Oct-2007 02:01
hello ok i have this email form right and it is
<?php
if (isset($_REQUEST['email']))
{
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
i like it but i want to change like but i want it to ask for sending it "TOO" and it automatically post the sender
phpcoder at cyberpimp dot ig3 dot net
27-Sep-2007 08:51
In addition to the $to parameter restrictions on Windows (ie. address can not be in "name <user@example.com>" format), the same restrictions apply to the parsed Cc and Bcc headers of the $additional_headers parameter.
However, you can include a To header in $additional_parameters which lists the addresses in any RFC-2822 format. (For display purposes only. You still need to list the bare addresses in the $to parameter.)
omgs
30-Aug-2007 01:57
I haven't seen in this page a reference about how to properly handle subject encoding when using non-ascii characters. I've found that info at http://www.johanvanmol.org/content/view/34/37/1/3/, which I paste:
"According to RFC 2822, mail header fields, including the subject, MUST be composed of printable US-ASCII characters (i.e., characters that have values between 33 and 126, inclusive). So if you want a subject with accents, you must encode it from your original character set to a US-ASCII character set. There are 2 of ways to do this: quoted-printable or base64.
[...]
Now we have an encoded subject, but our mail reader won't know that. So we need to tell it by formatting our subject as follows: "=?" charset "?" encoding "?" encoded-text "?=" , where charset is the original character set and encoding is either "Q" for Quoted-Printable or "B" for Base64.
E.g The subject containing the Quoted-Printable ISO-8859-1 string "Voil
|
|