Challenge - 5 Problems
Email Sending Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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')
Attempts:
2 left
💡 Hint
Remember Flask-Mail requires app context to send emails.
✗ Incorrect
The code correctly creates an app context with 'with app.app_context()' before sending the email. Flask-Mail uses this context to access configuration. Thus, the email is sent and 'Email sent' is printed.
📝 Syntax
intermediate1: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
Attempts:
2 left
💡 Hint
Check the Flask-Mail Message class attributes.
✗ Incorrect
The 'body' attribute is used to set the plain text content of the email. Methods like set_body or setText do not exist in Flask-Mail's Message class.
🔧 Debug
advanced2: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')
Attempts:
2 left
💡 Hint
Flask-Mail requires an active app context to access config during send.
✗ Incorrect
Without 'with app.app_context()', Flask-Mail cannot access the app config properly, so sending silently fails or raises an error depending on environment.
❓ state_output
advanced1: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>'
Attempts:
2 left
💡 Hint
Flask-Mail Message has separate attributes for plain text and HTML content.
✗ Incorrect
The 'html' attribute holds the HTML content of the email. Setting it assigns the given string. 'body' is separate for plain text.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
TLS encrypts the connection between your app and mail server.
✗ Incorrect
Setting MAIL_USE_TLS to True enables TLS encryption, which secures the email transmission. MAIL_USE_SSL is for SSL encryption, which is different. MAIL_DEBUG and MAIL_SUPPRESS_SEND control logging and sending behavior but not encryption.