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.