SetFrom PHPMailer not working

SetFrom PHPMailer not working

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

Stefan Bogdanescu

Founder & Senior Architect

Why SetFrom Fails When Using Gmail SMTP Authentication

If you are configuring PHPMailer to send emails via Gmail's SMTP server, you may encounter a scenario where the email delivers successfully but appears to originate from your authenticated user account rather than the custom address you specified in the $mail->From property. This discrepancy is not a bug in PHPMailer; it is a deliberate security enforcement mechanism implemented by Gmail.

When you authenticate with an SMTP server, that server often requires the "envelope sender" address—the address used by the underlying mail transfer agent to route the message—to match the account being authenticated. In your specific code snippet, you set $mail->Username and $mail->Password to a gmail.com address, but then attempt to override the sender with donotreply@mydomain.com. Gmail's SMTP service detects this mismatch. It sees an authenticated Google account trying to impersonate a third-party domain without explicit authorization. Consequently, it silently corrects the envelope sender to your logged-in Gmail address, ignoring your $mail->From setting for the transmission headers that matter most to receiving mail servers (like Received: from and the actual return path).

This behavior is distinct from the Return-Path header visible in spam filters. The From header displayed to the recipient usually works as expected if you set it correctly, but the underlying envelope sender mismatch triggers Gmail's anti-spoofing protocols. If you are using standard Gmail login credentials (the 16-character app password or main account password), this enforcement is strict.

To resolve this issue, you must ensure that the domain in your $mail->From address matches the domain of the SMTP account you are authenticating with. You cannot use a custom domain email address as the envelope sender while logging in with a personal Gmail account unless you configure DMARC and SPF records on your custom domain to explicitly allow Google's servers to send mail on its behalf, which is rarely feasible for standard users.

The practical solution involves changing your authentication strategy or your sending identity:

Option 1: Match the Envelope Sender to the Authenticated Account

If you must use Gmail SMTP, the most straightforward fix is to set the From address to match your authenticated username. While this solves the technical error, it may not be ideal for branding if you are trying to send from a corporate domain.

Option 2: Use App Passwords with Domain Matching

Gmail allows you to generate "App Passwords" for specific applications. However, even with an app password, the envelope sender must generally align with the account's domain unless you have advanced DNS configurations (SPF/DMARC) in place that authorize Gmail to send on your behalf.

Option 3: Switch to a Dedicated SMTP Service

If sending from donotreply@mydomain.com is critical for your application's workflow, relying on personal Gmail credentials is the wrong architectural choice. Instead, consider using an SMTP service designed for third-party domain sending, such as Mailgun, SendGrid, or Amazon SES. These services act as intermediaries; they accept authentication via API keys or tokens but allow you to set the From address to any domain you have verified with them.

In a Laravel environment, for instance, these services integrate seamlessly into the mail configuration without requiring complex SMTP username/password mismatches. If you are building a larger application where email deliverability and sender reputation are critical, evaluating managed transactional email providers is often superior to raw SMTP implementation. While you can implement custom SMTP logic directly in PHP, frameworks like Laravel provide robust abstractions for these services that handle the underlying DNS verification automatically.

Verifying Your Configuration

Before switching services, verify your current setup against these constraints:

  1. Authentication Domain: Does the username match the domain in $mail->From?
  2. Security Settings: Are you using an App Password instead of your primary login password? Standard passwords are often deprecated for third-party app access.
  3. DNS Records: If you insist on sending from mydomain.com while using Gmail, check if your domain's SPF record includes include _spf.google.com. Note that even with this, Gmail may still enforce envelope sender matching depending on specific security policies active at the time of transmission.

Ultimately, the mismatch between your login credentials and your desired From address is the root cause. Aligning them ensures that the mail server accepts your request without modification, preserving the integrity of your email headers and preventing delivery failures in strict corporate environments.

Our network