Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

casc-or's avatar

Setting Host Domain In SwiftMailer/SMTP

Laravel Framework version 5.3.31, Swiftmailer v5.4.12 running on a Ubuntu VPS on nginx.

Laravel app is sending mail using SMTP to a shared hosting email server. Worked for two years plus. Hosting company migrated to new servers this week and mail delivery was broken.

Mail being sent is to other domain (e.g. gmail.com) cc to our domain (ourdomain.ca).

Server was delivering cc mail addressed to our domain but discarding mail to any other domain.

Drupal instance on same VPS server sending mail using same SMTP server delivers mail to our domain and all other domains.

Inspection of email from Laravel not delivered to other domain but delivered to our domain shows HELO=[127.0.0.1] which (I think) the SMTP server is discarding because it says "I'm 127.0.0.1, you can't be 127.0.0.1, this is SPAM, off to /dev/null". Hosting company tech support is useless but they inadvertently sent an exim log which shows this. Mail from Drupal shows HELO=ourdomain.ca.

So I start digging into the Laravel site (which has been unchanged in mail sending functionality other than composer update).

I look at swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php. It has a protected $_domain variable defaulted to '[127.0.0.1]' and a function setLocalDomain($domain) which is supposed to be called to set the domain.

The function is never being called that I can find, so the default is being used, and that's why my mail isn't being delivered.

I have changed the default in AbstractSmtpTransport.php but that of course isn't a good long term solution as it will be regularly overwritten.

I need some clues though as to where I can modify in my code to call the setLocalDomain function to get the right domain in the HELO. I'm using the Mail facade and Mailables.

// create mail from Mailable
$email = new LicencePurchased($member, $licence, $sale_type, $request->process_type);

// queue mail now
Mail::to($member->email)->cc('[email protected]')->bcc('[email protected]')->queue($email);

TIA for any help.

0 likes
3 replies
casc-or's avatar

Well, I'm stumped. On my local Homestead box, the HELO shows the server name as it's set there.

On my production server, it never returns the server name and always returns the default in AbstractSmtpTransport.php in Swift_Mailer.

phpinfo() tells me the SERVER_NAME and SERVER_ADDR are set correctly and if I set the value of SERVER_NAME as the default for $_domain in AbstractSmtpTransport.php then the messages are delivered with that in the HELO.

It's as if _LookupHostName never runs.

Once again, any clues appreciated. I've seen enough different reports of this happening (there's at least one other here on Laracasts) to know that this isn't an isolated issue.

VanDyckBrown's avatar

Did you find a solution for this issue? I seem to be running in to the same issue.

MarianoMoreyra's avatar

HI @vandyckbrown !

I had the same problem on a shared hosting and I got it working by adding the following:

config/mail.php

'local_domain' => env('MAIL_LOCAL_DOMAIN', 'localhost'),

right after the 'host' variable.

Then, I've added the MAIL_LOCAL_DOMAINvariable to my .env file with the corresponding domain:

MAIL_LOCAL_DOMAIN=example.com

Hope this helps!

Please or to participate in this conversation.