1

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

3 Answers3

1

Make sure you have allowed less secure apps to connect to your Google account.
Here is the link: https://myaccount.google.com/lesssecureapps

smallwat3r
  • 1,037
  • 1
  • 8
  • 28
  • I've done that. I also did other steps suggested in https://www.twilio.com/blog/2018/03/send-email-programmatically-with-gmail-python-and-flask.html – Vilius Gudžiūnas Mar 12 '19 at 15:51
0

Try it with the following params:

app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
j2logo
  • 649
  • 4
  • 9
0

Below is an example that works for me:

import os
from flask import Flask, jsonify
from flask_mail import Mail, Message

app = Flask(__name__)

app.config["MAIL_SERVER"]='smtp.gmail.com'
app.config["MAIL_PORT"]=587
app.config["MAIL_USE_TLS"]=False
app.config["MAIL_USE_SSL"]=True
app.config["MAIL_USERNAME"]=os.environ.get("MAIL_USERNAME")
app.config["MAIL_PASSWORD"]=os.environ.get("MAIL_PASSWORD")

mail = Mail(app)    
msg = Message(
              <your subject>,
              sender=os.environ.get("MAIL_USERNAME"),
              recipients=<recipiente e-mail>,
              html=<your html template>   
             )

@app.route('/sendmail')
def publish():    
   mail.send(msg)
   return jsonify({'message': 'Your message has been sent successfully'}), 200

if __name__ == '__main__':
       app.run()
Marcelo
  • 367
  • 3
  • 7