PHP: Use Google's SMTP relay with SwiftMailer

Viewed 1543

I want to use Google's SMTP relay service as a SMTP server in PHP.

The IP address is whitelisted for the SMTP relay service, and I am using the SwiftMailer as a PHP library to send emails.

I configured the SMTP service to use TLS encryption, and set the following parameters for the SwiftMailer:

transport: smtp
host: smtp-relay.gmail.com
port: 587
encryption: tls
auth_mode: login

I was able to send emails via telnet, but when I try to send emails with SwiftMailer with the above configuration, I receive the following error:

[CLOSED] IMAP connection broken (server response) (errflg=2)

How can I configure the SwiftMailer to use Google SMTP relay service as its SMTP server? Is it ever possible?

2 Answers

had similar issue, when I provided the sender_address (that I'm checking on google relay server), it worked. (I'm not using auth mode though, just IP source defined in google gsuite)

swiftmailer: transport: smtp host: smtp-relay.gmail.com port: 587 encryption: tls sender_address: support@example.com

Since this seems to be not that well documented here is what worked for me after reading through different sources. Gmail itself wrote that it's important to send the HELO- or EHLO command, which can be done by setting the local domain (supported since swiftmailer-bundle 2.4.0). Otherwise you might run into DoS issues. See the official Google documentation for more details.

transport:    smtp
host:         smtp-relay.gmail.com
port:         587
encryption:   tls
auth_mode:    login
username:     contact@yourdomain.com
password:     'yourpassword'
local_domain: yourdomain.com

Additionally I did add the IPv4 and the IPv6 address of my server within the Google Admin Console within the SMTP-Relay settings and activated TLS + SMTP-authentication, since I definitely want to have an encrypted connection between my server and gmail.

Related