0
0
Flaskframework~20 mins

Flask-Mail setup - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask-Mail Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of sending an email with Flask-Mail configured incorrectly?

Consider a Flask app where Flask-Mail is initialized but the SMTP server details are missing or incorrect. What happens when you try to send an email?

Flask
from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)
app.config['MAIL_SERVER'] = ''  # Missing server
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'user@example.com'
app.config['MAIL_PASSWORD'] = 'password'

mail = Mail(app)

with app.app_context():
    msg = Message('Test', sender='user@example.com', recipients=['test@example.com'])
    msg.body = 'Hello'
    mail.send(msg)
ARaises a TypeError because the message body is missing
BRaises a TimeoutError due to slow server response
CRaises a ConnectionRefusedError because the mail server is not specified
DSends the email successfully without errors
Attempts:
2 left
💡 Hint

Think about what happens if the app tries to connect to a mail server that is not set.

📝 Syntax
intermediate
2:00remaining
Which Flask-Mail configuration is correct for sending emails over TLS?

Choose the correct Flask-Mail configuration snippet to send emails securely using TLS on port 587.

Aapp.config.update({ 'MAIL_SERVER': 'smtp.example.com', 'MAIL_PORT': 465, 'MAIL_USE_SSL': True, 'MAIL_USERNAME': 'user', 'MAIL_PASSWORD': 'pass' })
Bapp.config.update({ 'MAIL_SERVER': 'smtp.example.com', 'MAIL_PORT': 587, 'MAIL_USE_TLS': True, 'MAIL_USERNAME': 'user', 'MAIL_PASSWORD': 'pass' })
Capp.config.update({ 'MAIL_SERVER': 'smtp.example.com', 'MAIL_PORT': 587, 'MAIL_USE_SSL': True, 'MAIL_USERNAME': 'user', 'MAIL_PASSWORD': 'pass' })
Dapp.config.update({ 'MAIL_SERVER': 'smtp.example.com', 'MAIL_PORT': 465, 'MAIL_USE_TLS': True, 'MAIL_USERNAME': 'user', 'MAIL_PASSWORD': 'pass' })
Attempts:
2 left
💡 Hint

TLS usually uses port 587 and requires MAIL_USE_TLS to be True.

state_output
advanced
2:00remaining
What is the value of msg.recipients after this Flask-Mail message setup?

Given the following code, what is the value of msg.recipients?

Flask
from flask_mail import Message

msg = Message(subject='Hello', sender='me@example.com')
msg.recipients = ['friend@example.com']
msg.add_recipient('colleague@example.com')
A['friend@example.com', 'colleague@example.com']
B['colleague@example.com']
C['friend@example.com']
DRaises AttributeError because add_recipient does not exist
Attempts:
2 left
💡 Hint

Check the Flask-Mail Message API for adding recipients.

🧠 Conceptual
advanced
2:00remaining
Why should Flask-Mail be initialized with the Flask app context?

Choose the best explanation for why Flask-Mail requires the Flask app context when sending emails.

ABecause Flask-Mail needs access to app configuration and context variables to send emails properly
BBecause Flask-Mail only works inside request handlers and cannot be used elsewhere
CBecause Flask-Mail requires the app context to create new Flask app instances dynamically
DBecause Flask-Mail uses the app context to store email content in the session
Attempts:
2 left
💡 Hint

Think about what Flask-Mail needs from the Flask app to send emails.

🔧 Debug
expert
3:00remaining
What error occurs when sending a Flask-Mail message with an invalid recipient address format?

Given this code snippet, what error will Flask-Mail raise if the recipient email address is invalid (e.g., missing '@')?

Flask
from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)
app.config.update({
    'MAIL_SERVER': 'smtp.example.com',
    'MAIL_PORT': 587,
    'MAIL_USE_TLS': True,
    'MAIL_USERNAME': 'user',
    'MAIL_PASSWORD': 'pass'
})

mail = Mail(app)

with app.app_context():
    msg = Message('Subject', sender='user@example.com', recipients=['invalid-email'])
    msg.body = 'Test'
    mail.send(msg)
Asmtplib.SMTPRecipientsRefused error indicating invalid recipient address
BValueError raised by Flask-Mail for invalid email format
CNo error; email is sent despite invalid recipient
DTypeError because recipients must be a list of strings
Attempts:
2 left
💡 Hint

Consider what happens at the SMTP server level when an invalid recipient is given.