Change the sender name in PHP mail instead of sitename@hostname.com
When you look at the raw headers of an email sent via mail(), you often see a discrepancy between the From header and what the recipient actually sees in their inbox. In your specific case, the email client displays sisters@rsx02.justhost.com as the sender name, even though your code attempts to inject "Suraj Hazarika". This behavior is not a bug in your PHP logic; it is a direct result of how shared hosting providers manage SMTP connections.
The core issue lies in the headers you are constructing. You have correctly set up the From header by concatenating the display name and the email address:
$from_mail = $full_name.'<'.$email_from.'>';
// Result: "Suraj Hazarika"<info@7sisters.in>
You also included a Reply-To header that mirrors this format. However, PHP's built-in mail() function does not send these headers directly to the mail server. Instead, it passes them to the system's Mail Transfer Agent (MTA). On shared hosting environments like JustHost, the default MTA is often configured to override or ignore the From header provided by the application script. It forces the envelope sender to be the server's actual hostname (rsx02.justhost.com) and often replaces the display name with that hostname if no explicit SMTP authentication is used.
To fix this, you must bypass the system MTA entirely and handle the SMTP handshake yourself. The standard solution involves using PHP's SMTP class or an external library like phpmailer/phpmailer. This approach allows you to authenticate with the server using your account credentials and explicitly set the sender information before the mail is handed off for delivery.
Here is how you can refactor the code using the native SMTP class, which requires no external dependencies:
$to = 'it@7sisters.in';
$email_from = "info@7sisters.in";
$full_name = 'Suraj Hazarika';
$subject = "testing sender name";
$message = '<p><strong>This is only a test. Please do not reply.</strong></p>';
// Create an instance of the SMTP class
$smtp = new PHPMailer\PHPMailer\PHPMailer(true);
try {
// Server settings
$smtp->isSMTP();
$smtp->Host = 'mail.justhost.com'; // Replace with your provider's SMTP host
$smtp->SMTPAuth = true;
$smtp->Username = 'info@7sisters.in'; // Your email username
$smtp->Password = 'YOUR_SECURE_PASSWORD'; // Your email password or app-specific token
$smtp->Port = 465; // Use 587 if your provider uses STARTTLS
$smtp->SMTPSecure = 'ssl';
// Sender settings
$smtp->setFrom($email_from, $full_name);
// Recipient and message setup
$smtp->addAddress($to, '');
$smtp->Subject = $subject;
$smtp->Body = $message;
// Send the email
$smtp->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$smtp->ErrorInfo}";
}
Note: The phpmailer library is widely adopted and adds robust error handling to this process. You can install it via Composer (composer require phpmailer/phpmailer) or download the source code.
If you are moving to a Laravel application, the framework handles SMTP configuration elegantly within the .env file. You would define your SMTP credentials there, ensuring they aren't hardcoded in the repository. Once configured, you can use the Mail::send() helper with an explicit from parameter that includes both the name and address:
Mail::raw('This is only a test.', function ($message) {
$message->subject('testing sender name')
->to('it@7sisters.in')
->from('info@7sisters.in', 'Suraj Hazarika');
});
The trade-off here is complexity versus control. Using the raw mail() function is simple but gives you zero control over the envelope sender on shared hosting. Switching to SMTP adds a few lines of code and requires secure password management, but it guarantees that the From header reflects exactly what you intended, without the hosting provider stripping your display name. Always verify the headers in your inbox after making these changes, as some clients may still cache old settings or strip headers if the SPF records for your domain do not authorize the new sending source.