📖 Scenario: You are building a small Flask web app that can send a simple email when a user submits a form. This is useful for contact forms or notifications.
🎯 Goal: Create a Flask app that sets up email configuration, defines a route to send an email, and sends a simple email message.
📋 What You'll Learn
Create a Flask app instance
Configure email server settings using Flask-Mail
Define a route /send-email that sends an email
Send a simple email with subject, sender, recipient, and body
💡 Why This Matters
🌍 Real World
Sending emails is common in web apps for notifications, password resets, and contact forms.
💼 Career
Understanding how to send emails programmatically is a key skill for backend web developers.
Progress0 / 4 steps
1
Set up Flask app and Flask-Mail
Import Flask and Mail from flask_mail. Create a Flask app instance called app. Then create a Mail instance called mail using app.
Flask
Hint
Use app = Flask(__name__) to create the app. Then pass app to Mail().
2
Configure email server settings
Add these configuration settings to app.config: MAIL_SERVER set to 'smtp.example.com', MAIL_PORT set to 587, MAIL_USE_TLS set to True, MAIL_USERNAME set to 'user@example.com', and MAIL_PASSWORD set to 'password123'.
Flask
Hint
Set each config key on app.config exactly as shown.
3
Create a route to send an email
Import Message from flask_mail. Define a route /send-email with a function send_email. Inside it, create a Message object with subject 'Hello', sender 'user@example.com', recipients list with 'friend@example.com', and body 'This is a test email.'. Then send the message using mail.send(msg).
Flask
Hint
Use @app.route('/send-email') to define the route. Create the Message with the exact subject, sender, recipients, and body. Then call mail.send(msg).
4
Run the Flask app
Add the standard Flask app run block at the bottom: if __name__ == '__main__': and inside it call app.run(debug=True).
Flask
Hint
Add the standard Flask run block at the end to start the app in debug mode.
Practice
(1/5)
1. What is the main purpose of the Message object in Flask-Mail when sending emails?
easy
A. To configure the mail server settings
B. To hold the email's subject, sender, recipients, and body content
C. To start the Flask application
D. To receive incoming emails
Solution
Step 1: Understand the role of Message object
The Message object is designed to store the email details like subject, sender, recipients, and body.
Step 2: Differentiate from other components
Mail server settings are configured separately, and Flask app startup is unrelated to Message. Receiving emails is not handled by Message.
Final Answer:
To hold the email's subject, sender, recipients, and body content -> Option B
Quick Check:
Message object = email content holder [OK]
Hint: Message object stores email details like subject and body [OK]
Common Mistakes:
Confusing Message with mail server configuration
Thinking Message starts the Flask app
Assuming Message receives emails
2. Which of the following is the correct way to send an email using Flask-Mail after creating a Message object named msg?
easy
A. mail.send(msg)
B. mail.send_message(msg)
C. send.mail(msg)
D. msg.send()
Solution
Step 1: Recall Flask-Mail send syntax
The correct method to send an email is mail.send(msg), where mail is the Mail instance and msg is the Message object.
Step 2: Identify incorrect method names
Options like mail.send_message, send.mail, and msg.send() are not valid Flask-Mail methods.
Final Answer:
mail.send(msg) -> Option A
Quick Check:
Send email = mail.send(msg) [OK]
Hint: Use mail.send() with Message object to send email [OK]
Common Mistakes:
Using incorrect method names like send_message
Calling send on the Message object
Reversing mail and send order
3. Given the following Flask-Mail code snippet, what will be the output or result?
A. The recipients parameter should be a list, not a string
B. The sender email is missing '@' symbol
C. The Message subject must be passed as a keyword argument
D. mail.send() requires two arguments
Solution
Step 1: Check recipients parameter type
The recipients argument must be a list of email addresses, but here it is a string.
Step 2: Validate other parameters
The sender email is valid, subject can be positional, and mail.send() takes one argument (the Message object).
Final Answer:
The recipients parameter should be a list, not a string -> Option A
Quick Check:
Recipients must be list = The recipients parameter should be a list, not a string [OK]
Hint: Recipients must always be a list, even for one email [OK]
Common Mistakes:
Passing recipients as a string
Misplacing subject argument
Expecting mail.send() to take multiple arguments
5. You want to send an email with Flask-Mail only if the recipient list is not empty and the body text is not empty. Which code snippet correctly implements this check before sending?
hard
A. msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg)
B. if recipients or body:
msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg)
C. if recipients and body:
msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg)
D. if not recipients and not body:
msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg)
Solution
Step 1: Understand the condition logic
We want to send email only if both recipients and body are not empty, so use if recipients and body:.
Step 2: Analyze each option
if recipients and body:
msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg) uses correct and condition. if recipients or body:
msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg) uses or which sends email if either is present. if not recipients and not body:
msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg) sends email if both are empty (wrong). msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg) sends email unconditionally.
Final Answer:
if recipients and body:
msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg) -> Option C
Quick Check:
Send only if both present = if recipients and body:
msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body)
mail.send(msg) [OK]
Hint: Use 'and' to check both recipients and body before sending [OK]
Common Mistakes:
Using 'or' instead of 'and' for condition
Sending email without any checks
Checking for empty strings incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.