DigitalOcean error sending via Google SMTP

After moving to DigitalOcean, I suddenly couldn't send mail via SMTP. I thought it may be a PHP 7 thing (I've moved to PHP 7 at the same time when moving servers) . Turns out, it is a DigitalOcean thing.

By default, when you send email using Google's SMTP servers, it'll try to connect via IPv6. SMTP via IPv6 is currently disabled on DigitalOcean (for whatever reason) so you need to change a config file to prefer IPv4 over IPv6.

First, take a copy of /etc/gai.conf e.g. sudo cp /etc/gai.conf /etc/gai.conf.bk

Edit /etc/gai.conf and find the line precedence ::ffff:0:0/96 100 and uncomment it.

Reboot your server and voilĂ ! Done.