Consider a Flask app where Flask-Mail is initialized but the SMTP server details are missing or incorrect. What happens when you try to send an email?
from flask import Flask from flask_mail import Mail, Message app = Flask(__name__) app.config['MAIL_SERVER'] = '' # Missing server app.config['MAIL_PORT'] = 587 app.config['MAIL_USE_TLS'] = True app.config['MAIL_USERNAME'] = 'user@example.com' app.config['MAIL_PASSWORD'] = 'password' mail = Mail(app) with app.app_context(): msg = Message('Test', sender='user@example.com', recipients=['test@example.com']) msg.body = 'Hello' mail.send(msg)
Think about what happens if the app tries to connect to a mail server that is not set.
If the MAIL_SERVER config is empty, Flask-Mail cannot connect to any SMTP server, causing a connection error.
Choose the correct Flask-Mail configuration snippet to send emails securely using TLS on port 587.
TLS usually uses port 587 and requires MAIL_USE_TLS to be True.
Port 587 with MAIL_USE_TLS=True is the standard for secure SMTP with TLS. SSL uses port 465 and MAIL_USE_SSL=True.
Given the following code, what is the value of msg.recipients?
from flask_mail import Message msg = Message(subject='Hello', sender='me@example.com') msg.recipients = ['friend@example.com'] msg.add_recipient('colleague@example.com')
Check the Flask-Mail Message API for adding recipients.
The Message class does not have an add_recipient method. Recipients must be assigned or extended directly.
Choose the best explanation for why Flask-Mail requires the Flask app context when sending emails.
Think about what Flask-Mail needs from the Flask app to send emails.
Flask-Mail uses the app's configuration and context to access settings like MAIL_SERVER and credentials.
Given this code snippet, what error will Flask-Mail raise if the recipient email address is invalid (e.g., missing '@')?
from flask import Flask from flask_mail import Mail, Message app = Flask(__name__) app.config.update({ 'MAIL_SERVER': 'smtp.example.com', 'MAIL_PORT': 587, 'MAIL_USE_TLS': True, 'MAIL_USERNAME': 'user', 'MAIL_PASSWORD': 'pass' }) mail = Mail(app) with app.app_context(): msg = Message('Subject', sender='user@example.com', recipients=['invalid-email']) msg.body = 'Test' mail.send(msg)
Consider what happens at the SMTP server level when an invalid recipient is given.
The SMTP server rejects invalid recipient addresses, causing smtplib.SMTPRecipientsRefused error during sending.