1. Home
  2. Computing & Technology
  3. Email

How to Send Emails with Extra Headers in PHP

By , About.com Guide

Sending emails with PHP is easy. All you need is a function and three arguments.

Add another argument and you can specify extra headers like "X-Mailer", "Cc:" or "From:" (sic), for example. If you specify more than one extra header, make sure you separate them with "\r\n" to make sure they work as intended.

Send Emails with Extra Headers in PHP — Example

An example of a simple message with extra headers could look like this:

<?php
$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$headers = "From: sender@example.com\r\n" .
    "X-Mailer: php";
if (mail($to, $subject, $body, $headers)) {
  echo("<p>Message sent!</p>");
 } else {
  echo("<p>Message delivery failed...</p>");
 }
?>
Explore Email
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Email
  4. How Email Works
  5. Email Programming Tips
  6. How to Send Emails with Extra Headers in PHP - About Email>

©2009 About.com, a part of The New York Times Company.

All rights reserved.