Flask Mail - Flask Web Development with Python 29

sentdex · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

The video demonstrates how to use Flask-Mail for emailing from within a Flask web application, including installation, configuration, and sending emails with simple text formatting and HTML templates.

Full Transcript

what's going on everybody welcome to another flask tutorial video in this video what we're going to be talking about is flask mail so flas mail is actually pretty powerful uh for mailing within flas so powerful that honestly I would probably use it even if I didn't have a web application I don't know of any other mailing with python uh service or module that you can use that is as simple to use as this yet as powerful so this is the link to I'll try to remember to put it in the description if I forget uh you can either ask or you can just Google flashmail and uh it's definitely something you should look at but we'll kind of Zoom through uh just the basics sending an email doing you know subjects texts and HTML and the email uh just keep that in mind to head there if you have anything else you want to look for so the first thing you have to do is you got to install it so if you're installing the the virtual someone asked a question how does uh how do how how does our server know whether or not we're in the virtual environment right like remember we we set up the virtual environment in the beginning well for most of this tutorial we haven't actually been using the virtual environment I mean it's there if you want to use it uh but we haven't really been using it so you can either install to the virtual environment if you want or uh just to your server for me I've always just used servers and I rare like if I'm going to move a server I generally take a snapshot of the server and then move it so I never really had the need for a virtual environment but if you're going to host it to GitHub or something like that and you want people to just be able to download it uh a virtual environment can be a better option so anyways um moving along but but on GitHub there is a requirements so someone copied it they could automatically install the python libraries anyways anyway moving along so you'll need to do you know your pseudo pip install uh flask dmail I can't remember if that Caps or not but yeah it does it's capital F capital M anyway install flash dmail flash dmail I already have it so uh this won't really do anything but uh so you'll get that and then you're ready to rumble from this point so um the first thing that we'll do is we'll head to the init.py and once that's up what you'll want to do is um SMTP lib I believe we were using that to notify using an older emailing tactic I have that one is just really easy to send a quick email but you can't really customize very much uh so anyway just in case you were curious what you shouldn't have to import SMTP lib uh if you're not using my emailing thing so we've got import SMTP lib but then what we're going to want to do is we're going to do uh from flask mail import mail and then we're also going to wind up needing message so import message as well that's one thing I don't like about this documentation if you go through this documentation you see um that you have to import flask of course you got to import the mail and then they've got mail app and then you get all the way down here to sending messages and if you forget to do the whole flask in part message you're going to get an error that message not defined um first time I went through this I didn't realize that the other thing that I didn't like is let's see up here yeah when you define mail here um you can configure this in a in a variety of ways but generally I at least from what I've seen most people configure it within their application so this again throws people for a loop and I'll explain why uh here in a second so um moving this out of the way you'll want to do again from flask mail import uh the mail and message next what we want to do is uh underneath the definition of app equals FL ask name what you're going to want to have is some some configuration now I've got this on the um the sample T sample code the tutorial textbase tutorial python program.net so I'll link to that in the description so I'm just going to copy and paste because there's really no not much point to write all this out and let me just explain it so debug is is just set to True uh I'm fairly certain the only thing that that is is basically if the email fails for whatever reason the error that will be displayed is the actual error so on a live server you might not want debug true because it might show like if somebody's trying to figure something out it'll show them the exact error and that's helpful for for debugging right uh and that's also helpful for for hacking into something so you would want to set that eventually to false uh at if you're going to use it in production and just set it to True when you're just simply trying to debug mail server I have it set to the Gmail server because I was going to I use a Gmail account and then also I you know using a Gmail account just seems to be the best also Gmail rarely goes to spam as opposed to a lot of other emails that you might send automatically they'll they wind up in spam very often uh mailport 465 there is another one I forget what the other one is it's like 26 maybe or 500 something I don't know you can look those up but 465 should work no problem use SSL cool and then here is where you you would enter your actual email address and your actual password if you wanted you could use encryption to encrypt this password and then have your little key decrypt it right as it went to send really doesn't matter um it's just if you wanted to hide it basically in your script you could do that after all of that you need to say mail equals mail app you have to do this definition after the configuration otherwise you're going to get some problems now what you would want to do is come down some where and start the mailing uh so like if you wanted to mail let's say we'll just copy this uh you would come down here and paste and let's say let's just call this sendor mail and then let's say the URL you were going to visit was send mail and basically you Pro you should have try and accept here especially if you've got de bugging exception as e return string e and uh what you're going to try to do is you're going to try to send an email and for example I think what I'll do probably yeah I'm just going to copy and paste this again no real point in wasting a bunch of time writing that out and then return you got to return something otherwise you're going to get a 500 error so we'll just say mail sent so a lot of times you're going to send mail and you're going to return an actual you know HTML template so uh I'll talk about a real example that I've been using anyway here in a second but at the very basic level this is all you would need to do to send an email so here you've got the the the message right the message is uh basically class okay so you're it's an object from a class and so you've got message and you're just saying that is the equivalent of the message uh it's a message object from flask mail this is the subject of the email then you've got sender this would be who it's coming from then you've You' got recipients these will this could be just a single recipient or you could put a huge list of recipients in there although if you put a massive list you may wind up in spam and then uh this is the body so this would be simple text right so you can use like a new line to create a new line but that's about as fancy as you're going to get as far as uh you know styling your email so when whenever you're ready to send it just mail. send message and if everything was successful it'll say mail sent otherwise you'll get a an exception now here's the problem sometimes what you're going to find is the uh things just don't work out for you so for example let's go to my website using uh this let's see let's save that uh come over here and let's restart here PSE sudo there we go okay so then we'll head to let me go to uh this okay so I'm on the the server and then if we go slash send mail uh well at least oh I don't have the right username and password in there but you'll you'll get an error that looks an awful lot like that okay so probably the more popular error that you're going to get is not that your password is wrong but you'll get an error that looks like this let me just do this okay uh and it's going to be like hey you know please log into your web browser blah blah blah learn more at this link here okay so you'll go to that and it'll basically be in like some information on why you might not be able to log in uh and that would be here so this gives you a bunch of options you can um if you've got two two Factor authentication you need to set up an app password which Google will tell you the same thing but obviously that kind of nullifies is your TFA okay I mean it's not total it's it's a little more secure than not having TFA but at the end of the day You' got an app password that uh circumvents two Factor authentication so I would not use this on any email you want to have tofa on next up you've got this um this just says sometimes if you just sign into Google you can access the app or or you can basically run it through your app again that's really never been the case for me but you can try it uh then also there's this you can turn off display unlock capture basically what I understand this to be is Google realizes a program so it sends it tries to get it to solve a capture and that's like a security measure for your account so people don't just brute force your account but uh in this case uh we don't want that we don't want that capture cuz we're not solving that capture so you want to turn that off uh then next you have to always allow less secure apps to access the account that's always the case on Gmail know uh and then otherwise there's these two things which have never been the case now even if you do all of that you might still be shut out of your account and uh that's what's happening to me on this server and I think I don't know for sure Google doesn't really tell you this but uh my guess it has nothing to do with the the email itself the Gmail because I use the exact same Gmail on my other servers no problem but on this one I I think the problem is this is like a this is a you know it's just first of all it's just an IP there's no uh domain that's associated with it there's really no uh there's nothing for there like no reputation to the server and I think that's why Google is saying no and it's probably for like anti-spam so obviously if you can send emails via a program uh spammers that's what spammers want to do it's really easy to sign up for a Gmail so this is kind of Gmail's way of making it easy for legit people to sign up but hard for programs to sign up so anyway uh that's that so if you're trying to do this and for whatever reason it's not working it might be your server try to like maybe link a domain name to it a lot of times as long as there's a domain name it works but anyway uh that aside moving along I'm going to show you the code that I run with um pythonprogramming.net the actual code so let me get rid of this uh line here and basically what what I do is uh this would be from the forgot password uh from python program.net so basically what we send here is oops we need to go over one more uh and that's coming from here let me go to the pythonprogramming.net and should you go to login and for whatever reason you can't figure it out you go to forgot password and uh you just fill in your username and it sends you an email with a special link and stuff like that so the way that works is here uh once everything checks out that username exists and all that the sender is going to be pythonprogramming.net gmail.com the recipients will be whatever that person's email address we find that in the database and then we come down here the message body is just going to be uh this is this is if the user does not because you'll notice there's a body and an HTML body is for if uh if the user for whatever reason is on a device that can't read HTML so if they're on like a really old school phone or something like that like if if you're if your clientele is like Emerging Markets uh you might want to think about having both of these otherwise you probably don't need both but it's always good to have them just in case uh and then after that we got message HTML this appears to me to be the default uh no matter what I always send these two and the one that I've always seen is this what's neat about the HTML as you probably have seen as time has gone on is a lot of companies have very goodlook emails they've got pictures and it's they've got stylesheets going with them and stuff uh and you can do that too so uh sending this uh HTML it renders this template mails reset password. HTML and that HTML is actually really really really really simple uh but you you can do some pretty Advanced HTML but this HTML is that I use is pretty simple I'm just going to paste it on in here uh like that so it is it's just like any other template you can pass variables to it this one just uses paragraphs uh and it also uses a dynamic link right so or a link that you can actually click right uh and so it's not like the most advanced uh HTML but that's just a basic example of that yeah yep you can have a template and load simple HTML obviously if you go too crazy you have too many images and stuff I wouldn't rely on that because a lot of uh a lot of emails are going to just naturally not show the images and the user has to select he has to show images so I wouldn't really rely too heavily on images but as far as styling is concerned and divs and all kinds of cool stuff uh you can make your emails look look pretty darn cool so anyways uh that's just a quick flask email kind of tutorial it's I think it's like I said it's like the best mailing that I've ever found with with python because it's just so simple you've got your subject you send receive your body HTML and setting it up is as easy as this right here right so super easy super quick super reliable um the only problems I've had is the email just simply not going through even though everything's right and like I said I think that really obviously you have to go through the other options you will have to turn on less secure apps and display unlock capture uh but if you go through all that and it's still not working it's probably just your server and it's the anti-spam spam measures from Google anyway other than that if you guys are still having trouble or whatever feel free to leave comment below and I'll do my best to help you out uh in the next tutorial we're going to be talking about returning files so you know we return templates all the time but what if you have like some sort of download button or something like that where you want the user to be able to download something or maybe you've got like a PDF uh and you want to return a PDF uh how would you actually do that so that's what we're going to be talking about in the next tutorial so stay tuned for that otherwise thanks for watching

Original Description

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. https://pythonhosted.org/Flask-Mail/ Tutorial sample code and text-based version: http://pythonprogramming.net/flask-email-tutorial/ Flask-mail: http://pythonprogramming.net https://twitter.com/sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video teaches how to use Flask-Mail for emailing from within a Flask web application, including installation, configuration, and sending emails with simple text formatting and HTML templates. It covers the basics of Flask-Mail and how to use it for sending emails.

Key Takeaways
  1. Install Flask-Mail using pip install flask-mail
  2. Import the mail and message modules
  3. Configure Flask-Mail within the application or using a configuration file
  4. Create a message object from Flask-Mail
  5. Set message subject, sender, recipients, and body
  6. Send the email using the mail.send method
💡 Flask-Mail is a powerful mailing module for Flask that can be used to send emails with simple text formatting and HTML templates, but may be blocked by Gmail due to lack of domain reputation.

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →