problem with php mail 'From' header

problem with php mail 'From' header

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

Stefan Bogdanescu

Founder & Senior Architect

Problem with PHP Mail 'From' Header

When building a registration system, it is common to send a verification email immediately after a user signs up. The logic in your code appears sound: you define the recipient, set the subject, compose the message body, and construct headers using the mail() function. However, if the resulting email arrives with a sender address of munged@box123.bluehost.com instead of the intended munged@gmail.com, while the Reply-To header correctly reflects your settings, you are encountering a classic limitation of shared hosting environments.

The issue is not that you are using PHP incorrectly; it is that the underlying mail transfer agent (MTA) on your host is overriding specific headers. In your example, the host server replaces the From header but respects the Reply-To header. This behavior stems from how most web hosts configure their mail servers to prevent spam and ensure deliverability.

Web hosting providers typically run an MTA like Postfix or Exim configured with strict policies regarding the envelope sender versus the display headers. When you use the standard mail() function in PHP, it invokes the system's sendmail binary or a similar interface. This process often involves constructing a message for delivery where the "envelope sender"—the address used by SMTP to route the email—is derived from the server's configuration or the domain associated with the hosting account (box123.bluehost.com).

While you can set custom headers like From and Reply-To, many host MTAs strip or rewrite the From header if it does not match a verified address on their network. This is a deliberate security measure to prevent abuse, but it effectively breaks the illusion that your email originated from a Gmail account when sent via a shared hosting server. The fact that your Reply-To header remains intact suggests that the host only enforces strict rewriting on the primary sender identity for outbound traffic originating from their infrastructure.

To achieve the desired outcome where the From address matches your specification, you generally need to bypass the system mailer entirely and use SMTP directly. Libraries like PHPMailer or Symfony Mailer handle this by establishing a direct connection to an SMTP server (like Gmail's) using credentials. When connecting via SMTP AUTH, the library can set both the envelope sender and the display From header to your authenticated domain (munged@gmail.com). This ensures consistency across different mail clients because the email physically originates from the intended address rather than being relayed through a host that modifies it.

If you are constrained to using Laravel or similar frameworks, the built-in mail drivers in versions 8.10 and later allow for configuring SMTP transport specifically to handle these header requirements without modification. Switching your configuration to use an external SMTP provider resolves the mismatch between the envelope sender and the display headers.

The trade-off here is complexity versus control. Using the native mail() function requires zero setup but yields unreliable sender addresses on shared hosting. Using SMTP requires managing credentials and configuring a driver, but it guarantees that the email metadata matches your expectations. For production applications handling user communications, relying on the host's default mailer for custom From addresses is rarely a viable solution due to these unpredictable header rewrites.

Our network