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

sailingbyte's avatar

Laravel Mail using Swift SMTP

Hello,

I have been fighting lately with sending emails via SMTP using Mail. Situation as follows: I have been using one server as mail server and second server for website and Laravel sending emails. SPF and DKIM valid, encryption valid, password valid, host valid.

All was working form localhost, but did not work on regular mail server. Error message was "550 Bad HELO - Host impersonating domain name" and in message was of course mentioned website/Laravel domain.

Hours of debugging led me to Swiftmailer file located in lib/dependency_maps/transport_deps.php sets 'transport.localdomain' to $_SERVER['SERVER_NAME'] - and that's the reason why email server thinks I am impersonating. This is default value.

Swiftmailer Swift_DependencyContainer class allows to override this values, changing HELO/EHLO message. This approach is used in Symfony; example code is as follows:

$this->get('swiftmailer.transport')->setLocalDomain('your.mail.domain');

I haven't noticed similar ability of Mail class in Laravel. I think it would be nice feature to have.

If someone is having similar trouble, there is workaround possible by using $_SERVER; not very elegant, but working (must be adedd before sending email).

$_SERVER['SERVER_NAME'] = 'your.mail.domain';

If there is such functionality in Laravel, it would be nice to add this into documentation.

Best regards, Lukasz

0 likes
7 replies
leenooks's avatar

I've hit the same problem - except in my case my mail server is complaining about bogus HELO name used: [127.0.0.1] - which seems to be a swiftmailer default.

I cannot figure out how to call setLocalDomain() from Mail::class?

twofish's avatar

In swift mailer you can do this:

protected function sendMessage($sender_address, $sender_name, $email, $subject, $body)
    {
        $transport = (new Swift_SmtpTransport($this->host, $this->port))
            ->setUsername($this->username)
            ->setPassword($this->password);

        $mailer    = new Swift_Mailer($transport);

        $message   = (new Swift_Message($subject))
            ->setFrom($sender_address, $sender_name)
            ->setTo($email)
            ->setBody($body);


        return $mailer->send($message);
    }

Works fine.

casc-or's avatar

Thanks, @twofish but I'm pretty sure myself and @leenooks are trying to send the "Laravel way" through the Mail facade. The problem is that Laravel isn't sending properly, SwiftMailer does send properly.

I found the earlier thread you both posted on before I posted mine but it didn't appear to have an answer.

I thought I might have identified my problem. My VPS didn't have a host name which was a FQDN, but I gave it one, restarted the server, and the mail was still showing HELO=[127.0.0.1].

Moreover, I stepped through the code with debug and the code in SwiftMailer IS setting a domain correctly but then somewhere else the domain isn't set correctly before sending using the facade.

If I find more I'll post but hopefully someone more knowledgeable than me sees this and responds.

Joshuyo's avatar

@sailingbyte thanks for the long explanation. Totally agree with what you have said.

Here is how we solved the problem:

// put this before Mail::send(

$_SERVER[‘SERVER_NAME’] = config(‘mail.host’);

// mail.host should be equal to MAIL_HOST in your .env file

I hope this helps to someone.

laravel_junior's avatar

Hi,

After sites migrations from one server to another we have this problem too. Before, everything was ok. We are using Laravel 5.5.45 version. When I try send email via PHP mail I am getting this error "550 Bad HELO - Host impersonating domain name".

@sailingbyte and @joshuyo solutions work, but the mail is still showing HELO=[127.0.0.1] and then problem is with SPF and says that “550-SPF: is not allowed to send mail from abc.com". I have read that could be caused by duplicate information after site migration and I have to delete all traces from the previous server. However, I don't know where to find this traces.

Everything was working properly on the old server. The new server is a copy of the old one.

Snapey's avatar

spf record in your DNS records say what servers are allowed to send email. This is not a Laravel issue and you should talk to whoever manages your domain name @laravel_junior

laravel_junior's avatar

SPF record is set correctly. Anyway the problem is with Bad HELO. In HELO should be the host name instead localhost [127.0.0.1]. Where can I change helo settings? Is it possible?

Please or to participate in this conversation.