Challenge - 5 Problems
Flask Email Attachment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this Flask email sending code do?
Consider this Flask snippet that sends an email with an attachment. 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('Hello', sender='user@example.com', recipients=['friend@example.com']) msg.body = 'See attached file.' with open('document.txt', 'rb') as f: msg.attach('document.txt', 'text/plain', f.read()) mail.send(msg)
Attempts:
2 left
💡 Hint
Check how the file is opened and how attach() is used.
✗ Incorrect
The file is opened in binary mode ('rb'), so its content is correctly read and attached. The attach() method is used properly with filename, MIME type, and data. Hence, the email will have the attachment.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in this Flask email attachment code
Which option shows the correct way to attach a PDF file in Flask-Mail? The code snippet is:
msg = Message('Report', sender='me@example.com', recipients=['you@example.com'])
msg.body = 'Please find the report attached.'
with open('report.pdf', 'rb') as pdf:
msg.attach('report.pdf', 'application/pdf', pdf.read())
Which option has a syntax error?
Attempts:
2 left
💡 Hint
Look carefully at how the file content is passed to attach().
✗ Incorrect
Option A passes the method pdf.read without calling it (missing parentheses), so it passes a method object instead of bytes, causing a runtime error.
🔧 Debug
advanced2:30remaining
Why does this Flask email with attachment fail to send?
This Flask code tries to send an email with an attachment but fails silently (no error, no email sent). What is the most likely cause?
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('Data', sender='user@example.com', recipients=['friend@example.com'])
msg.body = 'Attached data file.'
with open('data.csv', 'rb') as f:
msg.attach('data.csv', 'text/csv', f.read())
mail.send(msg)
What is the most probable reason no email is sent?
Attempts:
2 left
💡 Hint
Check SMTP credentials and server response.
✗ Incorrect
If credentials are wrong or server rejects login, mail.send() may fail silently depending on configuration. TLS and port are correct, and app context is present.
❓ state_output
advanced2:00remaining
What is the content type of the attachment in this Flask email?
Given this code snippet:
msg = Message('Invoice', sender='me@example.com', recipients=['client@example.com'])
msg.body = 'Invoice attached.'
with open('invoice.xlsx', 'rb') as file:
msg.attach('invoice.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', file.read())
What will be the MIME type of the attached file in the sent email?
Attempts:
2 left
💡 Hint
Check the second argument of attach() method.
✗ Incorrect
The MIME type passed to attach() is 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', which is the official MIME type for .xlsx files.
🧠 Conceptual
expert3:00remaining
Which Flask-Mail method ensures attachments are included in multipart emails?
In Flask-Mail, when sending an email with attachments, which method or property ensures the email is sent as a multipart message so attachments are included properly?
Attempts:
2 left
💡 Hint
Check Flask-Mail documentation on how attachments affect email format.
✗ Incorrect
Flask-Mail automatically converts the message to multipart when you call attach(), so no manual setting is needed.