1. Computing & Technology

Discuss in my forum

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.