For anyone coming back to this, here is how I got the SMTPHandler working with Gmail:
eh = SMTPHandler(mailhost=('smtp.gmail.com', 587),
fromaddr=from_addr,
toaddrs=to_addrs,
subject=subject,
credentials=(username, password),
secure=())
The to_addrs
variable is a string in my example. I am not sure if it can be an array or is supposed to be a space or comma-delimited string. The username
variable includes the domain, like this: foo@gmail.com
.
Most guides said to use port 465, here is the difference if you are curious. However, when I tried to use port 465, I got a SMTP timeout error:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/smtplib.py", line 386, in getreply
line = self.file.readline(_MAXLINE + 1)
File "/usr/local/lib/python3.5/socket.py", line 571, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/logging/handlers.py", line 972, in emit
smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
File "/usr/local/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python3.5/smtplib.py", line 337, in connect
(code, msg) = self.getreply()
File "/usr/local/lib/python3.5/smtplib.py", line 390, in getreply
+ str(e))
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: timed out
I switched to port 587 and Google successfully authenticated. The messages were sent.