How to change from-address when using gmail smtp server
Stefan Bogdanescu
Founder & Senior Architect
Mastering Sender Identity: How to Change the 'From' Address When Using Gmail SMTP
Sending emails programmatically is a fundamental task in modern application development. When relying on an external SMTP server like Gmail, developers often encounter subtle but frustrating issues regarding sender identity. You correctly identified the problem: you set the From address in your PHP code, but upon receipt, the email still appears to originate from your personal Gmail account (abc@gmail.com).
As a senior developer, I can tell you that this issue isn't usually a simple syntax error; it stems from how SMTP servers handle authentication, domain reputation, and email header manipulation. Let's dive deep into why this happens and how to properly control the sender identity when using Gmail as an SMTP relay.
The Root of the Problem: Headers vs. Authentication
When you use a PHP library (like PHPMailer) to send mail via an external server, you are primarily manipulating the headers of the message. However, the final perceived sender address is determined by a complex interplay between three factors:
- The
FromHeader: This is what your code explicitly sets ($mail->From). - The SMTP Login Credentials: The email address you use to log into the server (e.g.,
abc@gmail.com). - Server-Side Verification (SPF/DKIM): Gmail’s servers check if the authenticated user sending the email is authorized to send mail on behalf of that specific domain, which often overrides or conflicts with simple header manipulation if proper setup is missing.
If you are authenticating directly with a personal Gmail account for application use, Gmail's security protocols will often prioritize the logged-in identity over the arbitrary From address you specify in the script, especially if you are not using a dedicated Google Workspace account or an App Password.
The Developer Solution: Correctly Setting Sender Information
To successfully change the FROM part, you must ensure that the email you are sending from is properly authenticated and authorized by the SMTP server, regardless of what you set in the PHP script.
Here is how you structure your code using a standard method like PHPMailer to maximize deliverability:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// Server settings (Example for Gmail SMTP)
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_application_email@gmail.com'; // This is the account used to log in
$mail->Password = 'your_app_password'; // Use an App Password, not your main password!
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// --- CRITICAL STEP: Setting the Sender Identity ---
$from_address = 'from@example.com'; // The desired sender address
$from_name = 'Mailer Application'; // The display name
$mail->setFrom($from_address, $from_name);
// Recipient details
$mail->addAddress('recipient@destination.com', 'Recipient Name');
// Content setup
$mail->isHTML(true);
$mail->Subject = 'Test Email with Custom From Address';
$mail->Body = 'This email was sent successfully from the custom address.';
$mail->send();
echo 'Message has been sent successfully.';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Analysis of the Fix
Notice the key line: $mail->setFrom($from_address, $from_name);. By setting this before sending, you are instructing the mail client library to construct the email headers with your desired information.
The real secret lies in the authentication section ($mail->Username and $mail->Password). If you are using a standard Gmail account, you must use an Application-Specific Password (App Password) instead of your regular login password for external applications. This is a critical security measure imposed by Google to prevent unauthorized access, which often resolves conflicts when sending via third-party SMTP services.
Architectural Considerations: Building Trustworthy Systems
When building any system that relies on email delivery, trust and deliverability are paramount. Just as in web development, where robust frameworks like Laravel enforce structure and secure practices, reliable email systems require strict adherence to protocols. If you are managing complex user interactions or critical communications within your application, consider how these external dependencies fit into a cohesive architecture. Understanding these underlying communication layers is key to building scalable solutions, much like how good architectural principles guide the design of applications on platforms like laravelcompany.com.
Conclusion
To successfully change the FROM address when using an SMTP server like Gmail in PHP, focus not just on setting the $mail->From property, but critically examine your SMTP authentication setup. Ensure you are using proper credentials (like App Passwords) and that your application settings align with the security expectations of the mail provider. By mastering these details, you move beyond simple scripting and achieve reliable, professional email delivery.
Note: Blog content is currently available in English.