I’m trying to send an email from one address to another using Gmail’s SMTP server. I’ve set up the PHP code with PHPMailer, but I’m running into an issue. Even though I’ve specified a custom ‘From’ address, the email still shows up as being sent from my Gmail account. Here’s a snippet of what I’ve tried:
$mailer = new PHPMailer();
$mailer->setFrom('[email protected]', 'Custom Sender');
$mailer->addAddress('[email protected]', 'Recipient Name');
$mailer->addReplyTo('[email protected]', 'Reply Handler');
Despite this, the ‘From’ field in the received email still shows my Gmail address. How can I make it display the custom address I’ve set? Is there something I’m missing in the configuration? Any help would be appreciated!
Unfortunately, you can’t easily change the ‘From’ address when using Gmail’s SMTP server. This is a security measure implemented by Gmail to prevent email spoofing; even if you set a custom ‘From’ address in your code, Gmail overrides it by using the email address associated with your account. As a workaround, you could use Gmail’s “Send mail as” feature to add your custom address to your account, use a different SMTP service that permits custom addresses, or configure your own SMTP server. Keep in mind that modifying the ‘From’ address may affect email deliverability and could violate email policies.
I’ve encountered this issue before, and it can be frustrating. While Gmail’s SMTP server is convenient, it’s quite restrictive when it comes to sender addresses. One solution I’ve found effective is setting up SPF and DKIM records for your custom domain. This can improve deliverability and sometimes allows the custom ‘From’ address to display correctly.
Another approach that’s worked for me is using an SMTP relay service like SendGrid or Mailgun. These services often provide more flexibility with sender addresses and can integrate seamlessly with your existing code.
If you’re committed to using Gmail’s SMTP, you might consider creating a separate Gmail account specifically for your application and configuring it to send as your custom address. This way, you’re not tied to your personal Gmail account for sending.
hey liam, i feel ur pain. gmail’s kinda strict bout this stuff. have u tried using the ‘send mail as’ option in gmail settings? u can add ur custom address there. it’s a bit of a hassle but it works. otherwise, maybe look into other smtp services that r more flexible? just a thought!