I’m trying to send an email using PHP and Gmail’s SMTP server. The goal is to have the email appear as if it’s coming from a different address than my Gmail account. Here’s what I’ve tried:
$mailer = new Emailer();
$mailer->setSender('[email protected]');
$mailer->setRecipient('[email protected]');
$mailer->setSubject('Important Message');
$mailer->setBody('This is the email content.');
$mailer->send();
However, when the email arrives, it still shows my Gmail address ([email protected]) as the sender. How can I make it display the custom address I set? Any help would be appreciated!
I’ve encountered a similar problem with Gmail’s SMTP. Gmail enforces strict policies that prevent spoofing the sender address, so even if you specify a custom sender, it is usually overridden by the authenticated account. One approach is to use the “Send mail as” feature in Gmail settings, which allows you to add and verify your custom domain email. Alternatively, you might consider using an external email service or setting up your own SMTP server to have complete control over the sender information. Be cautious about deliverability and ensure proper authentication with SPF and DKIM.
As someone who’s dealt with this problem extensively, I can tell you it’s a bit tricky with Gmail. They’re pretty strict about sender addresses for security reasons. One workaround I’ve found effective is setting up aliases in your Gmail account. Go to Settings > Accounts and Import > Add another email address. Verify ownership, and you can then use this alias as your ‘From’ address.
Another option is to use Gmail’s ‘Send mail as’ feature, but make sure to configure your DNS properly with SPF and DKIM records. This helps with deliverability and reduces the chance of your emails landing in spam.
If these don’t work for you, consider using a transactional email service like SendGrid or Mailgun. They offer more flexibility and better deliverability rates, especially for bulk sending. Just remember to always respect anti-spam laws and recipient preferences.
hey alexr1990, i’ve had this issue too.
gmail won’t let you override the sender. try using setFrom instead and verify your domain in gmail settings.
if it still fails, you might need an external mail service.