PHPMailer is causing 504 timeout error on my Digital Ocean server using nginx PHPMailer is causing 504 timeout error on my Digital Ocean server using nginx nginx nginx

PHPMailer is causing 504 timeout error on my Digital Ocean server using nginx


SMTP timeouts are pretty long (at least 5 mins). The 504 you're getting is because the timeout between nginx and your PHP cgi (I'd assume you're running FPM) is shorter, so by the time PHP generates an error, nginx has already dropped the connection, so you're getting no feedback.

It's fairly likely this is a DNS or firewall issue on your host - check out the troubleshooting docs.


Digital Ocean states in their support center how to enable services like smtp to prioritize connections over IPv4 whilst still maintaining accessible IPv6 functionality to your droplet otherwise.

You can give priority to IPv4 addresses over IPv6 so that you can continue to send out email without disabling IPv6. You would do that by editing the Droplet's /etc/gai.conf file and removing the comment (#) from the following line:

Default Configuration: #precedence ::ffff:0:0/96 100

Configuration with Priority to IPv4: precedence ::ffff:0:0/96 100

This has been checked and confirmed working by me for the PHPMailer-issue where request would time out (504 Gateway Time-out) but where the mail is still eventually delivered (Ubuntu 16.04 LEMP).

This is because Ngninx drops the connection to PHP-FPM whilst the php-script is still running in the background trying to resolve an IPv6 SMTP-address, before finally moving over to IPv4 on no-success.