How to Send Email Using Flask and Flask-Mail in an Easy Way

How to Send Email Using Flask? Are you looking to enhance your Flask application by incorporating dynamic email functionality? Whether it’s sending welcome emails, notifications, or newsletters, the ability to send personalized and dynamic emails can greatly enhance user engagement and overall user experience. In this guide, we’ll walk you through the process of integrating email functionality into your Flask application.

  • Setting Up Flask and Flask-Mail
$ pip install flask
$ pip install Flask-Mail

Once installed, proceed to configure Flask-Mail within your Flask application. Below is an example of a basic configuration setup.

from flask import Flask
from flask_mail import Mail

app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.example.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'your-email@example.com'
app.config['MAIL_PASSWORD'] = 'your-email-password'

mail = Mail(app)

Ensure to replace the configuration values with the relevant SMTP server details and email credentials for your specific setup.

  • Crafting an Email Template

For dynamic email generation, it’s advantageous to create email templates utilizing HTML and CSS. Flask-Mail seamlessly supports the rendering of Jinja2 templates for emails. Begin by designing an HTML template for your email, such as email_template.html.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic Email</title>
</head>
<body>
    <h1>Hello {{ name }},</h1>
    <p>This is a dynamic email sent from Flask!</p>
</body>
</html>
  • Sending Dynamic Emails

Now that Flask-Mail is set up and your email template is ready, it’s time to start sending emails from your Flask application. Below, you’ll find an example route showcasing this process.

from flask import render_template
from flask_mail import Message

@app.route('/send_email')
def send_email():
    recipient = 'recipient@example.com'
    subject = 'Welcome to Our Platform'
    message = render_template('email_template.html', name='John')
    msg = Message(subject, sender=app.config['MAIL_USERNAME'], recipients=[recipient])
    msg.html = message
    mail.send(msg)
    return 'Email sent successfully!'

Substitute recipient@example.com with the actual email address of the recipient. Moreover, you have the flexibility to inject dynamic content into the email template by adjusting the render_template function as needed.

Once you’ve integrated email functionality into your Flask application, it’s imperative to execute comprehensive testing procedures. Ensure that emails are dispatched successfully and that their content renders accurately across a diverse array of email clients.


Leave a Reply

Your email address will not be published. Required fields are marked *

DarkLight
×