Php mail function

The mail() function allows you to send emails directly from a script. This function returns TRUE if the email was successfully accepted for delivery, otherwise it returns FALSE.

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

To send email using PHP, you use the mail() function. This accepts 5 parameters as follows (the last 2 are optional)

to Required. The recipient’s email address.
subject Required. The email’s subject line.
message Required. The actual email body.
headers Optional. Additional header fields such as “From”, “Cc”, “Bcc” etc.
parameters Optional. Any additional parameters.

You can email as a text type mail or html mail.
A simple text type mail example :

<?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);
?>

A simple html mail example :

// multiple recipients
$to = ‘nobody@example.com’ . ‘, ‘; // note the comma
$to .= ‘admin@example.com’;

// 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>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>
‘;

// 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”;

// Additional headers
$headers .= ‘To: nobody , nobody ‘ . “\r\n”;
$headers .= ‘From: Birthday Reminder ‘ . “\r\n”;
$headers .= ‘Cc: admin1@example.com’ . “\r\n”;
$headers .= ‘Bcc: admin2@example.com’ . “\r\n”;

// Mail it
mail($to, $subject, $message, $headers);
?>

Related posts:

  1. Sessions in php
  2. Arrays in php
  3. Email validation in PHP
  4. What’s the difference between include and require?
  5. What is PHP ?

Leave a Reply

XHTML: You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>