0
0
Flaskframework~20 mins

Sending simple emails in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Email Sending 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 this Flask email sending code?
Consider this Flask snippet using Flask-Mail to send an email. What will be the result when this code runs?
Flask
from flask_mail import Mail, Message
from flask import Flask

app = Flask(__name__)
app.config.update(
    MAIL_SERVER='smtp.example.com',
    MAIL_PORT=587,
    MAIL_USE_TLS=True,
    MAIL_USERNAME='user@example.com',
    MAIL_PASSWORD='password'
)
mail = Mail(app)

with app.app_context():
    msg = Message(subject='Hello', sender='user@example.com', recipients=['friend@example.com'])
    msg.body = 'Hi there!'
    mail.send(msg)
print('Email sent')
APrints nothing and silently fails to send email
BRaises RuntimeError because app context is missing
CPrints 'Email sent' and sends the email successfully
DRaises AttributeError because 'mail' is not initialized
Attempts:
2 left
💡 Hint
Remember Flask-Mail requires app context to send emails.
📝 Syntax
intermediate
1:30remaining
Which option correctly sets the email body in Flask-Mail?
You want to set the plain text body of an email message in Flask-Mail. Which code snippet is correct?
Flask
from flask_mail import Message

msg = Message(subject='Test', sender='me@example.com', recipients=['you@example.com'])
# Set the body here
A
msg.setText('Hello there!')
B
msg.body = 'Hello there!'
C
msg.body('Hello there!')
D
msg.set_body('Hello there!')
Attempts:
2 left
💡 Hint
Check the Flask-Mail Message class attributes.
🔧 Debug
advanced
2:30remaining
Why does this Flask-Mail email fail to send?
This Flask-Mail code fails to send the email. What is the most likely cause?
Flask
from flask_mail import Mail, Message
from flask import Flask

app = Flask(__name__)
app.config.update(
    MAIL_SERVER='smtp.example.com',
    MAIL_PORT=587,
    MAIL_USE_TLS=True,
    MAIL_USERNAME='user@example.com',
    MAIL_PASSWORD='password'
)
mail = Mail()

msg = Message(subject='Test', sender='user@example.com', recipients=['friend@example.com'])
msg.body = 'Hello!'
mail.send(msg)
print('Done')
AMissing app context when calling mail.send(msg)
BIncorrect MAIL_PORT value causes connection failure
CMAIL_USE_TLS should be False for port 587
DSender email must be in recipients list
Attempts:
2 left
💡 Hint
Flask-Mail requires an active app context to access config during send.
state_output
advanced
1:30remaining
What is the value of msg.html after this code runs?
You want to send an HTML email using Flask-Mail. After running this code, what is the value of msg.html?
Flask
from flask_mail import Message

msg = Message(subject='HTML Test', sender='me@example.com', recipients=['you@example.com'])
msg.body = 'Plain text'
msg.html = '<h1>Heading</h1><p>This is HTML</p>'
A'Plain text'
BNone
CRaises AttributeError
D'<h1>Heading</h1><p>This is HTML</p>'
Attempts:
2 left
💡 Hint
Flask-Mail Message has separate attributes for plain text and HTML content.
🧠 Conceptual
expert
2:00remaining
Which Flask-Mail configuration is essential for sending emails securely?
You want to send emails securely using Flask-Mail. Which configuration option is essential to enable TLS encryption?
AMAIL_USE_TLS = True
BMAIL_USE_SSL = False
CMAIL_DEBUG = True
DMAIL_SUPPRESS_SEND = False
Attempts:
2 left
💡 Hint
TLS encrypts the connection between your app and mail server.