Setting custom 'From' address in PHPMailer with Gmail SMTP

I’m having trouble configuring PHPMailer to use a custom ‘From’ address when sending emails through Gmail SMTP. Even though I’ve set the ‘From’ field to ‘[email protected]’, the emails are still showing my Gmail account as the sender.

Here’s a simplified version of my code:

$mailer = new CustomMailer();

$mailer->setupSMTP('smtp.gmail.com', 465, true);
$mailer->setCredentials('[email protected]', 'mypassword');

$mailer->setFrom('[email protected]');
$mailer->addRecipient('[email protected]');
$mailer->setReplyTo('[email protected]', 'No Reply');

$mailer->composeEmail('Email Subject', 'Email Body');
$mailer->send();

When I check the email headers, I see:

Return-Path: <[email protected]>

But I want it to show ‘[email protected]’ instead. Any ideas on how to fix this? Thanks for your help!

I’ve encountered this issue as well. Unfortunately, Gmail’s SMTP server enforces strict sender policies to prevent email spoofing. One potential solution is to use Google Workspace (formerly G Suite) instead of a regular Gmail account. With Google Workspace, you can configure custom domains and have more control over sender addresses. Alternatively, consider using a different SMTP provider that allows custom ‘From’ addresses, such as SendGrid or Amazon SES. These services offer more flexibility and are designed for transactional emails, which may better suit your needs.

I’ve run into this exact issue when using Gmail SMTP. Gmail inherently forces the sender to be the authenticated account, so when you set a different ‘From’ address, it gets overridden as a security measure against spoofing. In my experience, one workaround is to configure the Gmail account to send mail using the ‘Send mail as’ feature by adding your desired alias in the settings. Alternatively, consider switching to an SMTP provider that allows full control over the ‘From’ address, which might suit your requirements better.

hey there, i’ve dealt with this before. gmail’s pretty strict about sender addresses. have you tried adding your custom address as an alias in your gmail settings? that might work. if not, you could look into other smtp providers that are more flexible. good luck!