Sending mail from PHP

The php mail() function is described as follows:

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

This basically means that the first three parameters (to, subject and message) are requiered in order for the function to work. But we also have the option to send additional headers, and in it is in these additional headers that we can specify things like the “from” and “reply-to”.

For example:

$to = 'someone@example.com';
$subject = 'the subject';
$message = 'the message';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
 
mail($to, $subject, $message, $headers);

If you were sending HTML messages you would also use the addidional headers to specify the MIME version, Content-Type and character set.

For example:

// 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: Someone <someone@example.com>, someoneelse <someoneelse@example.com>' . "\r\n";
$headers .= 'From: John Smith <john@example.com>' . "\r\n";
$headers .= 'Cc: carboncopy@example.com' . "\r\n";
$headers .= 'Bcc: blindcarboncopy@example.com' . "\r\n";

More info on mail() check out the official documentation.