Bird
Raised Fist0
Flaskframework~20 mins

Sending simple emails in Flask - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. Step 1: Understand the role of Message object

    The Message object is designed to store the email details like subject, sender, recipients, and body.
  2. 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.
  3. Final Answer:

    To hold the email's subject, sender, recipients, and body content -> Option B
  4. 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

  1. 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.
  2. Step 2: Identify incorrect method names

    Options like mail.send_message, send.mail, and msg.send() are not valid Flask-Mail methods.
  3. Final Answer:

    mail.send(msg) -> Option A
  4. 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?
from flask_mail import Message
msg = Message(subject='Hello', sender='me@example.com', recipients=['you@example.com'], body='Hi there!')
mail.send(msg)
medium
A. An email with subject 'Hello' is sent from me@example.com to you@example.com with body 'Hi there!'
B. SyntaxError because Message requires a different parameter order
C. No email sent because body is missing
D. NameError because mail is not defined

Solution

  1. Step 1: Check code completeness

    The snippet uses mail.send(msg) but does not show the mail object being created or initialized.
  2. Step 2: Understand Flask-Mail usage

    Without initializing mail with Flask app and configuration, calling mail.send() will cause a NameError because mail is undefined.
  3. Final Answer:

    NameError because mail is not defined -> Option D
  4. Quick Check:

    Undefined mail object = NameError [OK]
Hint: Ensure mail is initialized before calling mail.send() [OK]
Common Mistakes:
  • Assuming mail is auto-created
  • Thinking parameter order causes syntax error
  • Ignoring missing body error
4. Identify the error in this Flask-Mail email sending code:
msg = Message('Greetings', sender='admin@example.com', recipients='user@example.com', body='Welcome!')
mail.send(msg)
medium
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

  1. Step 1: Check recipients parameter type

    The recipients argument must be a list of email addresses, but here it is a string.
  2. Step 2: Validate other parameters

    The sender email is valid, subject can be positional, and mail.send() takes one argument (the Message object).
  3. Final Answer:

    The recipients parameter should be a list, not a string -> Option A
  4. 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

  1. 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:.
  2. 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.
  3. Final Answer:

    if recipients and body: msg = Message(subject='Hi', sender='me@example.com', recipients=recipients, body=body) mail.send(msg) -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard