7
mail ( P H P4, P H P 5) m ail S end m ail D e s cri p t i o n bool mail ( string $to , string $subject , string $message [, string $additional_headers  [, string$additional_parameters  ]] ) S end s an em ai l . P ar am eters to R e ce i ve r, o r r e ce i ve rs o f t h e m a il. T h e f o r m a t ti n g o f t h is s t ri n g m ust co m p l y w i t h » R FC2822 . S om e exam pl es are: user @exam pl e. com user @exam pl e. com , anot her user @exam pl e. com U ser <user @exam pl e. com > U ser <user @exam pl e. com >, A not her U ser <a not her user @exam pl e.com> subject S u b j e ct o f t h e e m a il to be se n t . C au ti o n S u b j e ct m u st sa t i sf y » R FC2047 .  message

php mail

Embed Size (px)

DESCRIPTION

mail

Citation preview

Page 1: php mail

7/17/2019 php mail

http://slidepdf.com/reader/full/php-mail 1/7

mail

(PHP 4, PHP 5)

mail — Send mail

Description

bool mail ( string $to , string $subject , string $message [, string 

$additional_headers [, string$additional_parameters ]] )

Sends an email.

Parameters

to

Receiver, or receivers of the mail.

The formatting of this string must comply with» RFC 2822. Some examples are:

[email protected]

[email protected], [email protected]

• User <[email protected]>

• User <[email protected]>, Another User <[email protected]>

subject

Subject of the email to be sent.

Caution

Subject must satisfy» RFC 2047.

 message

Page 2: php mail

7/17/2019 php mail

http://slidepdf.com/reader/full/php-mail 2/7

Message to be sent.

Each line should be separated with a CRLF (\r\n). Lines should not be larger than

70 characters.

Caution

(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). If outside data are used to

compose this header, the data should be sanitized so that no unwanted headers

could be injected.

Note:

additional_headers does not have mail header injection protection.Therefore, users must make sure specified headers are safe and contains

headers only. i.e. Never start mail body by putting multiple newlines.

Note:

When sending mail, the mailmust contain aFrom header. This can be set

with theadditional_headers parameter, or a default can be set

inphp!ini.

Failing to do this will result in an error message similar toWarning: mail():

"sendmail_from" not set in php.ini or custom "From:" header missing.

TheFrom header sets alsoReturn-Path under Windows.

Note:

If messages are not received, try using a LF (\n) only. Some Unix mail

transfer agents (most notably» qmail) replace LF by CRLF automatically

Page 3: php mail

7/17/2019 php mail

http://slidepdf.com/reader/full/php-mail 3/7

(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)

Theadditional_parameters 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 thesendmail_path configuration setting. For example, this can be

used to set the envelope sender address when using sendmail with the-

f sendmail option.

This parameter is escaped byescapeshellcmd() internally to prevent command

execution.escapeshellcmd() prevents command execution, but allows to add

addtional parameters. For security reasons, it is recommended for the user to

sanitize this parameter to avoid adding unwanted parameters to the shell

command.

Sinceescapeshellcmd() is applied automatically, some characters that are

allowed as email addresses by internet RFCs cannot be used. mail() 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.

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 isetcmailtrusted%users.

Return Values

ReturnsTRUE 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.

Changelog

Page 4: php mail

7/17/2019 php mail

http://slidepdf.com/reader/full/php-mail 4/7

Version Description

4.2.3 The additional_parameters parameter is disabled in safe_mode and the mail() function will

used.

Examples

Example #1 Sending mail.

Using mail() to send a simple email:

<?php

&he message

$message = "'ine rn'ine rn'ine *"

+n case an o- our lines are larger than ./ characters, 0e should use 0ord0

rap()

$message = 0ord0rap($message, ./, "rn")

1end

mail(2ca--einated3example!com2, 24 1ubject2, $message)

?#

Example #2 Sending mail with extra headers.

The addition of basic headers, telling the MUA the From and Reply-To addresses:

<?php

$to = 2nobod3example!com2

$subject = 2the subject2

$message = 2hello2

$headers = 25rom6 0ebmaster3example!com2 ! "rn" !

  27epl%&o6 0ebmaster3example!com2 ! "rn" !  28%4ailer6 9:92 ! php;ersion()

mail($to, $subject, $message, $headers)

?#

Example #3 Sending mail with an additional command line parameter.

Page 5: php mail

7/17/2019 php mail

http://slidepdf.com/reader/full/php-mail 5/7

Theadditional_parameters parameter can be used to pass an additional parameter to

the program configured to use when sending mail using thesendmail_path.

<?php

mail(2nobod3example!com2, 2the subject2, 2the message2, null,

  2%-0ebmaster3example!com2)

?#

Example #4 Sending HTML email

It is also possible to send HTML email with mail().

<?php

multiple recipients$to = 2aidan3example!com2 ! 2, 2 note the comma

$to != 20e3example!com2

subject

$subject = 2irthda 7eminders -or >ugust2

message

$message = 2

<html#

<head#

  <title#irthda 7eminders -or >ugust<title#

<head#

<bod#

  <p#:ere are the birthdas upcoming in >ugust<p#

  <table#

  <tr#

  <th#9erson<th#<th#@a<th#<th#4onth<th#<th#Aear<th#  <tr#

  <tr#

  <td#Boe<td#<td#*rd<td#<td#>ugust<td#<td#C./<td#

  <tr#

  <tr#

Page 6: php mail

7/17/2019 php mail

http://slidepdf.com/reader/full/php-mail 6/7

  <td#1all<td#<td#.th<td#<td#>ugust<td#<td#C.*<td#

  <tr#

  <table#

<bod#

<html#

2

&o send :&4' mail, the Dontent%tpe header must be set

$headers = 24+4E%Fersion6 !/2 ! "rn"

$headers != 2Dontent%tpe6 texthtml charset=iso%GGHC%2 ! "rn"

>dditional headers

$headers != 2&o6 4ar <mar3example!com#, Iell <Jell3example!com#2 ! "rn"$headers != 25rom6 irthda 7eminder <birthda3example!com#2 ! "rn"

$headers != 2Dc6 birthdaarchi;e3example!com2 ! "rn"

$headers != 2cc6 birthdachecJ3example!com2 ! "rn"

4ail it

mail($to, $subject, $message, $headers)

?#

Note:

If intending to send HTML or otherwise Complex mails, it is recommended to use the

PEAR package» PEAR::Mail_Mime.

Notes

Note:

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 aMTA is needed listening on a network socket

(which can either on the localhost or a remote machine).

Second, the custom headers likeFrom:,Cc:,Bcc: andDate: arenot interpreted by

theMTA in the first place, but are parsed by PHP.

Page 7: php mail

7/17/2019 php mail

http://slidepdf.com/reader/full/php-mail 7/7

As such, theto parameter should not be an address in the form of "Something

<[email protected]>". The mail command may not parse this properly while

talking with the MTA.

Note: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.

Note:

The following RFCs may be useful:» RFC 1896,» RFC 2045,» RFC 2046,» RFC2047,» RFC 2048,» RFC 2049, and» RFC 2822.