Welcome to another Flask web development tutorial, in this tutorial we're going to be discussing how to utilize Flask-Mail for emailing from within your app.
To start, we need to grab Flask-Mail: sudo pip install Flask-Mail
. Next, from within your __init__.py
, add the following import to the top:
from flask_mail import Mail, Message
Next, along with the app definition, we add the following:
app = Flask(__name__) app.config.update( DEBUG=True, #EMAIL SETTINGS MAIL_SERVER='smtp.gmail.com', MAIL_PORT=465, MAIL_USE_SSL=True, MAIL_USERNAME = 'your@gmail.com', MAIL_PASSWORD = 'yourpassword' ) mail = Mail(app)
If you are not using gmail, you will need to use a different mailserver.
Now let's make a quick mail sending function in our __init__.py
file:
@app.route('/send-mail/') def send_mail(): try: msg = Message("Send Mail Tutorial!", sender="yoursendingemail@gmail.com", recipients=["recievingemail@email.com"]) msg.body = "Yo!\nHave you heard the good word of Python???" mail.send(msg) return 'Mail sent!' except Exception, e: return(str(e))
Next, visit this URL to send the email. Using a gmail may require you to enable insecure apps in your account. You will get an email from Google if that is the case when you actually run the application, and you will also get a return on the page that looks like: (534, '5.7.14 Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 s84sm12251698qki.14 - gsmtp')
. If you get this with Gmail, go to "My Account," then "Sign-in and security," then the "connected apps & sites." On this page, you are looking for the "Allow less secure apps:" and then turn that on to allow the emails to go through.
Sometimes (usually), this STILL wont be enough. You may send one email successfully, but then the others will error again, and the error will contain: (534, '5.7.9 Please log in with your web browser and then try again. Learn more at\n5.7.9 https://support.google.com/mail/answer/78754 f189sm12187803qhe.1 - gsmtp')
, where what matters it the URL: https://support.google.com/mail/answer/78754. Head to here to turn off captcha. That should keep you all set, but just keep reading the errors if you keep getting them. Sometimes, you still wont get anywhere. Generally, at least from my findings, this happens when you use a server that has no reputation as a site. If you've done all of the steps for Google, and you still get errors, it might be your server. If you don't have an associated website, this might be the reason. I assume this is to fight people who are trying to spam, but I really do not know.
With this, we can send simple emails. We can actually take this further, sending more than simple text-based emails. As an example, I will share my "forgot password" emailing snippet:
msg = Message("Forgot Password - PythonProgramming.net", sender="pythonprogrammingnet@gmail.com", recipients=[email_addr]) msg.body = 'Hello '+username+',\nYou or someone else has requested that a new password be generated for your account. If you made this request, then please follow this link:'+link msg.html = render_template('/mails/reset-password.html', username=username, link=link) mail.send(msg)
Here, you can see there is a text-based version, with some variables and new lines. You can also see an additional attribute, msg.html
. Here, we can actually use a template. My template here is:
<p>Hello {{username}},</p> <p>You or someone else has requested that a new password be generated for your account. If you made this request, then please click this link: <a href={{link}}><strong>reset password</strong></a>. If you did not make this request, then you can simply ignore this email.</p> <p>Best,</p> <p>Harrison</p> <p>Harrison@pythonprogramming.net</p>
You can get far more fancy with your HTML, but this is a nice basic example. Should someone be using an email that wont support the HTML, the plain-text body can be shown.
Learn more about Flask-Mail here. Otherwise, in the next tutorial, we're going to be talking about how to return files with Flask.