PHP has a built in set of mail functions that allow a properly coded script to send email. This can come in handy when dealing with information submission forms, shopping carts, etc.
In order to properly use the built in mail function, the proper syntax must be used. Here is an example of the correct syntax:
mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )
In order to understand how this function works, it is important to understand what each parameter does.
Parameters:
to
Receiver, or receivers of the email message
In order to work properly, the to address must be formatted to be RFC compliant. Here are
some examples:
user@domain.com
user@domain.com, anotheruser@domain.com
User <user@domain.com>
User <user@domain.com>, Another User <anotheruser@domain.com>
subject
The subject of the email to be sent. WARNING: The subject can not contain any newline
characters, or the message may not be sent properly.
message
Message to be sent. Each line should be separated with a LF(n.). The lines should not be any
larger than 70 characters.
additional_headers (optional)
String to be entered at the end of the email header.
additional_parameters (optional)
Additional parameters can be used to pass extra options to the program PHP uses to send the
mail. Weberz uses sendmail. If you put the -f option here, you could specify the envelope sender
address on the message.
Here are some examples that help to further illustrate how to properly use the mail function within a PHP script.
Example Scripts:
Example 1:
---------------------------------------------------------------------------
<?php
// The message
$message = "Line 1nLine 2nLine 3";
// In case any of our lines are larger than 70 characters, we should use
wordwrap()
$message = wordwrap($message, 70);
// Send
mail('jondoe@domain.com', 'My Subject', $message);
?>
---------------------------------------------------------------------------
Example 2:
---------------------------------------------------------------------------
<?php
$to = 'nobody@domain.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@' . $_SERVER['SERVER_NAME'] . "rn" .
'Reply-To: webmaster@' . $_SERVER['SERVER_NAME'] . "rn" .
'X-Mailer: PHP/' . phpversion();mail($to, $subject, $message, $headers);
?>
---------------------------------------------------------------------------
Example 3 (using extra parameters to set the envelope sender):
---------------------------------------------------------------------------
<?php
mail('nobody@domain.com', 'the subject', 'the message', null,
'-fwebmaster@' . $_SERVER['SERVER_NAME']);
?>---------------------------------------------------------------------------
Example 4 (sending HTML mail):
---------------------------------------------------------------------------
<?php
$to = 'aidan@domain.com' . ', '; $to .= 'wez@domain.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' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "rn";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "rn";
$headers .= 'Cc: birthdayarchive@domain.com' . "rn";
$headers .= 'Bcc: birthdaycheck@domain.com' . "rn";
mail($to, $subject, $message, $headers);
?>---------------------------------------------------------------------------
These are all basic examples of how to utilize the mail function of PHP. For more information on the mail function, visit http://www.php.net and do a search for "mail".