PHP E-mail Form Sender Name Instead Of E-mail?

PHP E-mail Form Sender Name Instead Of E-mail?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP E-mail Form Sender Name Instead Of E-mail?

When configuring a contact form in PHP, it is common to encounter a situation where the recipient receives an email that lists the sender's address as both the name and the From header. This usually happens because the mail() function defaults to using the raw value of the From header if no specific headers are provided, or if the headers are constructed incorrectly. In your specific case, the code explicitly sets the From header to $email_from, which is why the recipient sees j.doe@website.com instead of "John Doe".

To display a custom sender name like "John Doe" while keeping the actual sending address as j.doe@website.com, you need to manipulate the SMTP headers before passing them to the mail() function. The standard way to achieve this in PHP is by using the Reply-To header for the display name or, more accurately for modern protocols, by constructing a custom header that combines both the address and the name.

However, there is a critical distinction between what you want to display and how the underlying protocol works. If you simply change the $email_from variable in your code to include the name (e.g., "John Doe <j.doe@website.com>"), some older mail clients might truncate this or ignore the part before the angle brackets. A more robust approach involves setting up a custom header that instructs the mail client to display the name while ensuring the From address remains valid for replies.

Here is how you can modify your existing code snippet to fix the issue:

// ... validation logic ...

$email_from = $_POST['email']; // The actual email address used for sending
$full_name = $_POST['full_name']; // The display name
$email_to = "ricky@rickytsang.ca";
$email_subject = $_REQUEST['subject'];

// Construct the From header with the display name and the real address
// Format: "Display Name <actual@example.com>"
$formatted_from = "$full_name <$email_from>";

$headers = "From: $formatted_from\r\n";
$headers .= "Reply-To: $email_from\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

// Send the email
mail($email_to, $email_subject, $email_message, $headers);

The Technical Trade-off: Reply-To vs. From

While setting the From header to "Name <address>" works in most modern clients like Apple Mail, Outlook, and Gmail, relying solely on this can sometimes lead to deliverability issues with strict spam filters if the domain does not have proper SPF records configured for that specific email address used in the display name.

A safer alternative often used by developers is to keep the From header strictly as the raw email address (which ensures bounce-backs go to the right place) and use a custom Sender or Disposition-Notification-To header, but these are less universally supported for simple "sender name" display purposes.

The most reliable method that preserves functionality across all clients is to keep the From header as the address (as shown in the code above) but ensure the Reply-To header points back to the same address if you want replies to go there, or a different one if desired. The key is the syntax "Name <address>".

Validation and Cleaning

Your existing validation logic for $full_name using the regular expression /^[A-Za-z .'-]+$/ is a good start. It ensures no special characters slip through that might break email header formatting. However, when constructing the From header, you must be careful with spaces and quotes. If a user enters "John Doe Jr.", the resulting header string must be properly escaped if it contains commas or quotes, though simple names usually pass without issue.

The clean_string() function in your original code is useful for preventing injection attacks via headers like Content-Type or To:. Ensure you apply similar sanitization if you ever decide to dynamically generate the display name from a database field containing HTML entities, although for form submissions, raw POST data is generally safer after basic validation.

Security Considerations

One potential pitfall when allowing users to define their own "From" names or addresses is spoofing. In your current implementation, $email_from comes directly from $_POST['email']. If a user inputs an email address they do not control, the receiving mail server might reject the message if the SPF record for that domain does not authorize your server to send mail on its behalf.

To mitigate this without adding complexity, you could enforce that the $email_from must match a known list of domains or, strictly speaking, only allow the user's own email address as the sender while displaying their chosen name. If you require users to sign in with a verified account, store their actual email in the database and use that for the From header, leaving only the display name dynamic.

// Example of enforcing a known domain for sending
$allowed_domains = ['website.com', 'company.org'];
$parts = explode('@', $email_from);
if (!empty($parts) && !in_array($parts[1], $allowed_domains)) {
    died('The email address provided is not authorized for this form.');
}

$formatted_from = "$full_name <$email_from>";

By adjusting the header construction to include the name within angle brackets, you resolve the immediate display issue while maintaining the integrity of the email transmission process.

Our network