Your server cannot connect to smtp.gmail.com on port 587. I have the same problem from a testing tool:
Resolving hostname...
Connecting...
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
Message sending failed.
From my local machine, it works perfectly:
Trying 74.125.195.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP be3sm8900765wib.21 - gsmtp
Maybe there are temporary problems at Google from the US (I am in Germany here) or something like this. There is no obvious mistake in your code. You can only try it again later or with another SMTP server.
Furthermore, you can try to connect with 74.125.195.108 directly (just deactivate SSL certificate validation).
EDIT: Just also try tls://smtp.gmail.com
as host.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…