I am following Flask Web Development book by Miguel Grinberg and I ran into an issue with his Email chapter.
These are his configurations:
import os
# ...
app.config["MAIL_SERVER"] = "smtp.googlemail.com"
app.config["MAIL_PORT"] = 587
app.config["MAIL_USE_TLS"] = True
app.config["MAIL_USERNAME"] = os.environ.get("MAIL_USERNAME")
app.config["MAIL_PASSWORD"] = os.environ.get("MAIL_PASSWORD")
After I set my environment variables I go into shell and try to run the following code:
(venv) $ flask shell
>>> from flask_mail import Message
>>> from hello import mail
>>> msg = Message("test email", sender="you@example.com", recipients=["you@example.com])
>>> msg.body = "This is plain text body"
>>> msg.html = "This is <b>HTML</b> body"
>>> with app.app_context():
... mail.send(msg)
...
My gmail is set up correctly, I followed the tutorial and did all the steps mentioned.
My code produced the following error:
Traceback (most recent call last):
File "<console>", line 2, in <module>
File "c:\...\flasky\venv\lib\site-packages\flask_mail.py", line 491, in send
with self.connect() as connection:
File "c:\...\flasky\venv\lib\site-packages\flask_mail.py", line 144, in __enter__
self.host = self.configure_host()
File "c:\...\flasky\venv\lib\site-packages\flask_mail.py", line 158, in configure_host
host = smtplib.SMTP(self.mail.server, self.mail.port)
File "C:\...\Python\Python37\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\...\Python\Python37\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\...\Python\Python37\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\...\Python\Python37\lib\socket.py", line 727, in create_connection
raise err
File "C:\...\Python\Python37\lib\socket.py", line 716, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
I found another question that was raised about the same thing and I tried a couple of things that were suggested in there:
I changed the line
app.config["MAIL_SERVER"] = "smtp.googlemail.com"
to
app.config["MAIL_SERVER"] = "smtp.gmail.com"
and I hard coded "MAIL_USERNAME" and "MAIL_PASSWORD" variables but I got the same error again.
Due to nothing working and the previous question about this being very old (4 years) I thought it might be worth raising this again.
If anybody knows what I am doing wrong please let me know.
Thanks