PHP Mail Script With The Correct Headers

Correct Way to Set the From and Reply‑To Headers in PHP Mail

When sending email using PHP’s built‑in mail() function, it’s important to correctly set the From and Reply‑To headers so that mail clients (MUAs) know which address the message is from and where replies should be sent.

Below is a simple and correct example of how to define these headers when sending an email using PHP.


PHP Example: Setting From and Reply‑To
<?php
$to      = 'nobody@example.com';
$subject = 'The subject';
$message = 'Hello';

$headers  = "From: webmaster@example.com\r\n";
$headers .= "Reply-To: webmaster@example.com\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

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

This ensures the message is properly formed and recognised by all major mail clients.


Important Notes

  • The From address should be a valid mailbox on your domain.
  • The Reply‑To address can be the same or a different mailbox.
  • Some mail servers require authentication — consider using an SMTP library such as PHPMailer for reliability.
  • Ensure your domain has correct SPF, DKIM, and DMARC records for best deliverability.

For more advanced email sending, using SMTP with authentication is recommended over the basic mail() function.


Learn More About Fast2host

Fast2host has been providing reliable UK hosting, domains, cloud servers and business email since 2002. Find out more about who we are:

About Fast2host →
Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 13144

Need more information or have a question ?