How to work with PHP mail function? Print

  • 0

The mail function sends email.


Example #1 Sending a letter.

Using the mail() function to send a simple letter:

<?php
// Message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case some line of the letter is longer than 70 characters, we use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
mail('caffeinated@example.com', 'My Subject', $message);
?>

 

Example #2 Sending an email with additional headers.

Adding simple headers that tell the mail agent 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);
?>

 


Example #3 Sending a letter with additional headers passed in an array

This example sends the same email as the example above, but the additional headers are specified as an array (available as of PHP 7.2.0).

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = array(
'From' => 'webmaster@example.com',
'Reply-To' => 'webmaster@example.com',
'X-Mailer' => 'PHP/'. phpversion()
);

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

 


Example #4 Sending an HTML message

Using the mail() function you can also send an HTML letter.

<?php
// several recipients
$to = 'johny@example.com, sally@example.com'; // pay attention to the comma

// letter subject
$subject = 'Birthday Reminders for August';

// text of the letter
$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>Johny</td><td>10th</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 an HTML email, 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: Mary <mary@example.com>, Kelly <kelly@example.com>';
$headers[] = 'From:Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc:birthdayarchive@example.com';
$headers[] = 'Bcc:birthdaycheck@example.com';

// Send
mail($to, $subject, $message, implode("\r\n", $headers));

The mail function sends email.


Example #1 Sending a letter.

Using the mail() function to send a simple letter:

<?php
// Message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case some line of the letter is longer than 70 characters, we use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
mail('caffeinated@example.com', 'My Subject', $message);
?>

 

Example #2 Sending an email with additional headers.

Adding simple headers that tell the mail agent 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);
?>

 


Example #3 Sending a letter with additional headers passed in an array

This example sends the same email as the example above, but the additional headers are specified as an array (available as of PHP 7.2.0).

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = array(
'From' => 'webmaster@example.com',
'Reply-To' => 'webmaster@example.com',
'X-Mailer' => 'PHP/'. phpversion()
);

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

 


Example #4 Sending an HTML message

Using the mail() function you can also send an HTML letter.

<?php
// several recipients
$to = 'johny@example.com, sally@example.com'; // pay attention to the comma

// letter subject
$subject = 'Birthday Reminders for August';

// text of the letter
$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>Johny</td><td>10th</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 an HTML email, 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: Mary <mary@example.com>, Kelly <kelly@example.com>';
$headers[] = 'From:Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc:birthdayarchive@example.com';
$headers[] = 'Bcc:birthdaycheck@example.com';

// Send
mail($to, $subject, $message, implode("\r\n", $headers));


Was this answer helpful?

« Back