Email is important in web apps because it helps you communicate with users directly. It allows sending notifications, verifying accounts, and resetting passwords.
0
0
Why email matters in web apps in Flask
Introduction
When you want users to confirm their registration by clicking a link sent to their email.
When you need to send alerts or updates about user activity or app changes.
When users forget their password and need a way to reset it securely.
When you want to send newsletters or promotional messages to users.
When you want to verify user identity for security purposes.
Syntax
Flask
from flask import Flask from flask_mail import Mail, Message app = Flask(__name__) app.config['MAIL_SERVER'] = 'smtp.example.com' app.config['MAIL_PORT'] = 587 app.config['MAIL_USERNAME'] = 'your-email@example.com' app.config['MAIL_PASSWORD'] = 'your-password' app.config['MAIL_USE_TLS'] = True app.config['MAIL_USE_SSL'] = False mail = Mail(app) @app.route('/send-email') def send_email(): msg = Message('Hello from Flask', sender='your-email@example.com', recipients=['user@example.com']) msg.body = 'This is a test email sent from a Flask app.' mail.send(msg) return 'Email sent!'
Flask-Mail is an extension that helps send emails easily in Flask apps.
You need to configure your mail server settings correctly for emails to send.
Examples
Sends a simple welcome email to a new user.
Flask
msg = Message('Welcome!', sender='admin@example.com', recipients=['newuser@example.com']) msg.body = 'Thanks for joining our app!' mail.send(msg)
Sends an HTML email with a password reset link.
Flask
msg = Message('Reset Password', sender='admin@example.com', recipients=['user@example.com']) msg.html = '<b>Click <a href="http://example.com/reset">here</a> to reset your password.</b>' mail.send(msg)
Sample Program
This Flask app sends a welcome email when you visit the /send-welcome URL. It shows how to set up Flask-Mail and send a simple text email.
Flask
from flask import Flask from flask_mail import Mail, Message app = Flask(__name__) app.config['MAIL_SERVER'] = 'smtp.example.com' app.config['MAIL_PORT'] = 587 app.config['MAIL_USERNAME'] = 'your-email@example.com' app.config['MAIL_PASSWORD'] = 'your-password' app.config['MAIL_USE_TLS'] = True app.config['MAIL_USE_SSL'] = False mail = Mail(app) @app.route('/send-welcome') def send_welcome(): msg = Message('Welcome to Our App', sender='your-email@example.com', recipients=['user@example.com']) msg.body = 'Hi there! Thanks for signing up. We are happy to have you.' mail.send(msg) return 'Welcome email sent!' if __name__ == '__main__': app.run(debug=True)
OutputSuccess
Important Notes
Always keep your email credentials safe and do not hardcode them in production apps.
Test email sending with a real SMTP server or services like Gmail, SendGrid, or Mailgun.
Emails can be plain text or HTML for better formatting and links.
Summary
Email helps web apps talk directly to users for important actions.
Flask-Mail makes sending emails easy with simple setup and code.
Use emails for verification, notifications, and password resets to improve user experience.