📖 Scenario: You are building a simple web app where users can sign up with their email addresses. Email is important because it helps identify users, send notifications, and reset passwords.
🎯 Goal: Create a Flask app that stores user emails, checks if an email is valid, and displays a confirmation message.
📋 What You'll Learn
Create a list called user_emails to store email addresses
Create a variable called email_to_add with a sample email string
Use a simple condition to check if email_to_add contains '@' before adding it to user_emails
Add a Flask route /confirm that returns a confirmation message including the added email
💡 Why This Matters
🌍 Real World
Web apps often need to collect and verify user emails to manage accounts and send notifications.
💼 Career
Understanding how to handle emails in web apps is essential for backend and full-stack developers.
Progress0 / 4 steps
1
Set up the email list
Create a list called user_emails and initialize it as empty.
Flask
Hint
Think of user_emails as a container to keep all user email addresses.
2
Add a sample email to check
Create a variable called email_to_add and set it to the string 'user@example.com'.
Flask
Hint
This is the email we want to add to our list if it looks valid.
3
Check and add the email
Use an if statement to check if email_to_add contains the character '@'. If yes, add email_to_add to the user_emails list.
Flask
Hint
This simple check helps us avoid adding invalid emails.
4
Create a confirmation route in Flask
Import Flask and create an app. Add a route /confirm that returns a string confirming the email added, using user_emails[0].
Flask
Hint
This route shows a friendly message to the user confirming their email was saved.
Practice
(1/5)
1. Why is email important in web applications built with Flask?
easy
A. It allows the app to send notifications and verify users.
B. It speeds up the loading time of web pages.
C. It stores user passwords securely.
D. It replaces the need for a database.
Solution
Step 1: Understand the role of email in user communication
Email is used to send important messages like verification and notifications to users.
Step 2: Identify what email does not do in Flask apps
Email does not affect page loading speed, password storage, or replace databases.
Final Answer:
It allows the app to send notifications and verify users. -> Option A
Quick Check:
Email use = notifications and verification [OK]
Hint: Email = user communication tool, not speed or storage [OK]
Common Mistakes:
Thinking email speeds up page loading
Confusing email with password storage
Assuming email replaces databases
2. Which of the following is the correct way to import Flask-Mail in a Flask app?
easy
A. from flask import Mail
B. import flask.mail as Mail
C. import Mail from flask_mail
D. from flask_mail import Mail
Solution
Step 1: Recall the correct import syntax for Flask-Mail
The Flask-Mail extension is imported using 'from flask_mail import Mail'.
Step 2: Identify incorrect import statements
Options A, B, and D use invalid Python import syntax or wrong module paths.
Final Answer:
from flask_mail import Mail -> Option D
Quick Check:
Correct import = from flask_mail import Mail [OK]
Hint: Use 'from flask_mail import Mail' to import Flask-Mail [OK]
Common Mistakes:
Using incorrect import syntax
Trying to import Mail from flask instead of flask_mail
Using 'import Mail from flask_mail' which is invalid
3. What will happen when this Flask-Mail code runs?
from flask_mail import Mail, Message
mail = Mail()
msg = Message(subject='Hello', sender='app@example.com', recipients=['user@example.com'])
msg.body = 'Welcome!'
mail.send(msg)
medium
A. An email with subject 'Hello' and body 'Welcome!' is sent to user@example.com.
B. The email is sent but without a subject.
C. The code raises an error because mail is not initialized with the app.