0
0
Flaskframework~20 mins

Email with attachments in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Email Attachment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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)
ASends an email without any attachment because attach() is called incorrectly.
BSends an email with subject 'Hello' and attaches 'document.txt' as a plain text file.
CRaises a FileNotFoundError because 'document.txt' does not exist in the code.
DSends an email but the attachment will be empty because the file is opened in text mode.
Attempts:
2 left
💡 Hint
Check how the file is opened and how attach() is used.
📝 Syntax
intermediate
1: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?
Amsg.attach('report.pdf', 'application/pdf', pdf.read)
Bmsg.attach('report.pdf', 'application/pdf', pdf.read())
C))(daer.fdp ,'fdp/noitacilppa' ,'fdp.troper'(hcatta.gsm
Dsg.attach('report.pdf', 'application/pdf', pdf.read())
Attempts:
2 left
💡 Hint
Look carefully at how the file content is passed to attach().
🔧 Debug
advanced
2: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?
AThe MAIL_USE_TLS should be False to send attachments.
BThe app context is missing, so mail.send() does nothing.
CThe MAIL_PORT 587 requires MAIL_USE_TLS=True, which is set correctly, so no issue here.
DThe MAIL_USERNAME and MAIL_PASSWORD are likely incorrect or SMTP server rejects authentication silently.
Attempts:
2 left
💡 Hint
Check SMTP credentials and server response.
state_output
advanced
2: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?
Aapplication/octet-stream
Bapplication/vnd.ms-excel
Capplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Dtext/plain
Attempts:
2 left
💡 Hint
Check the second argument of attach() method.
🧠 Conceptual
expert
3: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?
AUsing msg.attach() automatically converts the email to multipart if needed.
BYou must set msg.content_subtype = 'multipart' manually before attaching files.
CAttachments require setting msg.is_multipart = True explicitly before sending.
DFlask-Mail does not support multipart emails with attachments.
Attempts:
2 left
💡 Hint
Check Flask-Mail documentation on how attachments affect email format.