How do I set the name of an email sender via PHP

How do I set the name of an email sender via PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How do I set the name of an email sender via PHP

When you open a message in your inbox, the "From" field usually displays one of two things: a raw email address like noreply@example.com or a friendly display name like "Jack Sparrow". Achieving that specific format—"Jack Sparrow Via somesite"—is a common requirement for transactional emails, but it requires understanding how the underlying protocols handle headers versus content.

The confusion often stems from mixing up PHP's mail() function with the concepts of SMTP headers and MIME encoding. The mail() function in PHP is essentially a wrapper around the system's mail transfer agent (MTA). It takes an email address as the recipient, a message body, and a list of headers. To inject a custom display name, you must explicitly set the From header. However, simply appending a name to the address string won't work; you need to separate the actual routing address from the display name using standard RFC 2822 syntax.

The correct approach involves constructing a comma-separated list where the first element is the email address and the second is the display name in double quotes. In PHP, this looks like the following:

$from = '"Jack Sparrow Via somesite" <noreply@somesite.com>';
$headers = "From: {$from}\r\nReply-To: noreply@somesite.com\r\n";

mail($to, $subject, $body, $headers);

Notice the space between the quoted name and the angle-bracketed address. This is critical. Without that whitespace, many mail clients will interpret the entire string as a malformed email address rather than a display name followed by a route. The carriage return and line feed (\r\n) are also mandatory; PHP's mail() function is strict about header formatting, and missing these breaks often results in the headers being ignored entirely or appended to the message body.

A significant pitfall with the native mail() function is that it relies on the server's configuration for how emails are delivered. If your server is using a generic MTA like Postfix or Exim configured to strip headers or enforce SPF records strictly, your custom display name might get rejected by modern email providers like Gmail or Outlook.com. These services often flag emails with mismatched From and Reply-To headers as potential phishing attempts if the domain does not have proper DNS records (SPF/DKIM) set up.

For production environments where you need more control over these headers, relying solely on mail() is generally discouraged. Instead, using an SMTP library or a framework-specific mailer is safer. In Laravel, for instance, the mailer system handles header construction more robustly and integrates seamlessly with services like Mailgun or SendGrid. If you are working within a Laravel application, you can pass these headers directly to the message builder:

$message = new Illuminate\Mail\Mailable();
// Or using the facade
Mail::raw('Your content here', function ($message) {
    $message->from('"Jack Sparrow Via somesite"', 'noreply@somesite.com');
});

The trade-off here is complexity versus portability. The raw mail() function with manually crafted headers works on almost any shared hosting environment without installing additional packages, but it places the burden of compliance on you. If you choose to use a dedicated email service provider (ESP), they often provide their own SDKs that abstract away these header details entirely, ensuring your emails land in the inbox rather than the spam folder.

Ultimately, setting the display name is a matter of syntax precision. The space between the quoted string and the address is non-negotiable. Furthermore, ensure that the domain used in the angle brackets matches the SPF records for that domain. If you use "Jack Sparrow" <noreply@somesite.com>, but your domain somesite.com does not authorize the sending IP, the display name will likely be stripped by the receiving server, and you will be left with only the raw address. Always test these changes in a real environment, as behavior can vary significantly between different email clients.

Our network