Skip to content

Example

Aniket Sarkar edited this page Jan 14, 2023 · 1 revision

Example

Sending emails using Flask-Mailing

List of Examples

Basic configuration

from flask import Flask, jsonify
from flask_mailing import Mail, Message

mail = Mail()

def create_app():
    app = Flask(__name__)


    app.config['MAIL_USERNAME'] = "[email protected]"
    app.config['MAIL_PASSWORD'] = "world_top_secret_password"
    app.config['MAIL_PORT'] = 587
    app.config['MAIL_SERVER'] = "your-email-server.com"
    app.config['MAIL_USE_TLS'] = True
    app.config['MAIL_USE_SSL'] = False
    mail.init_app(app)

    return app

#send a simple email using flask_mailing module.

app = create_app()

@app.get("/email")
async def simple_send():

    message = Message(
        subject="Flask-Mailing module",
        recipients=["[email protected]"],
        body="This is the basic email body",
        )

    
    await mail.send_message(message)
    return jsonify(status_code=200, content={"message": "email has been sent"})

Add recipient using add_recipient method

message.add_recipient("[email protected]")

Send a simple html message

html = """
<p>Hi this test mail, thanks for using Flask-Mailing</p> 
"""

@app.get("/html-email")
async def html_email():

    message = Message(
        subject="Flask-Mailing module test html mail",
        recipients=["[email protected]"],
        body=html,
        subtype="html"
        )

    
    await mail.send_message(message)
    return jsonify(status_code=200, content={"message": "email has been sent"})

Sending files

@app.get("/mail-file")
async def mail_file():
    message = Message(
        subject = "attachments based email",
        recipients = ["[email protected]"],
        body = "email with attachments, email body.",
        attachments = ['attachments/attachment.txt']
    )
    await mail.send_message(message)
    return jsonify(message="email sent")

Sending files using attach method

with app.open_resource("attachments/example.txt") as fp:
    message.attach("example.txt", fp.read())

Using Jinja2 HTML Templates

You can enable Jinja2 HTML Template emails by setting the TEMPLATE_FOLDER configuration option, and supplying a value (which is just the name of the template file within the TEMPLATE_FOLDER dir) for the template_name parameter in Mail.send_message(). You then can pass a Dict as the template_body property of your Message object. If you haven't provided the TEMPLATE_FOLDER configuration option, then the module will take the app's jinja2 environment for templating and you can use templates from the app's default template folder:

from pathlib import Path

app.config["TEMPLATE_FOLDER"] = Path(__file__).parent / 'email-templates'
"""
Don't use this configuration if you want to use the default jinja2 environment.
"""
@app.get("/mail-html")
async def mail_html():
    
    message = Message(
        subject = "html template based email",
        recipients = ["[email protected]"],
        template_body = {
                        "first_name": "Hare",
                        "last_name": "Krishna"
                        }
        # attachments = ['attachments/attachment.txt']
    )
    #or
    message = Message(
        subject = "html template based email",
        recipients = ["[email protected]"],
        template_params = {
                        "first_name": "Hare",
                        "last_name": "Krishna"
                        }
        # attachments = ['attachments/attachment.txt']
    )

    await mail.send_message(message, template_name="test.html")
    return jsonify(message="email sent")

For example, assume we pass a template_body of:

{
  "first_name": "Hare",
  "last_name": "Krishna"
}

We can reference the variables in our Jinja templates as per normal:

...
<span>Hello, {{ first_name }}!</span>
...

Legacy Behaviour

The original behavior was to wrap the Dict you provide in a variable named body when it was provided to Jinja behind the scenes. In these versions, you can then access your dict in your template like so:

...
<span>Hello,  body.first_name !</span>
...

As you can see our keys in our dict are no longer the top level, they are part of the body variable. Nesting works as per normal below this level also.

Customizing attachments by headers and MIME type

Used for example for referencing Content-ID images in html of email

message = Message(
    subject='Flask-Mailing module',
    recipients=recipients,
    html="<img src='cid:logo_image'>",
    subtype='html',
    attachments=[
            {
                "file": "/path/to/file.png"),
                "headers": {"Content-ID": "<logo_image>"},
                "mime_type": "image",
                "mime_subtype": "png",
            }
        ],
)

fm = FastMail(conf)
await fm.send_message(message)

Guide for email utils

The utility allows you to check temporary email addresses, you can block any email or domain. You can connect to Redis to save and check email addresses. If you do not provide a Redis configuration, then the utility will save it in the list or set it by default.

Check the disposable email address

async def default_checker():
    checker = DefaultChecker()  # you can pass source argument for your own email domains
    await checker.fetch_temp_email_domains() # require to fetch temporary email domains
    return checker

@app.get('/email/dispasoble')
async def simple_send():
    domain = "gmail.com, 
    checker = await default_checker()
    if await checker.is_dispasoble(domain):
        return jsonify(status_code=400, content={'message': 'this is dispasoble domain'})

    return jsonify(status_code=200, content={'message': 'email has been sent'})

Add disposable email address

@app.get('/email/disposable)
async def add_disp_domain():
    domains: list = ["gmail.com"]
    checker: DefaultChecker = await default_checker()

    res = await checker.add_temp_domain(domains)

    return jsonify(status_code=200, content={'result': res})

Add domain to the blocked list

@app.get('/email/blocked/domains')
async def block_domain():
    domain: str = "gmail.com"
    checker: DefaultChecker = await default_checker()

    await checker.blacklist_add_domain(domain)

    return jsonify(status_code=200, content={'message': f'{domain} added to blacklist'})

Check domain is blocked or not

@app.get('/email/blocked/check-domains')
async def get_blocked_domain():
    domain: str ='gmail.com'
    checker: DefaultChecker = await default_checker()
    res = await checker.is_blocked_domain(domain)

    return jsonify(status_code=200, content={"result": res})

Add email address to the blocked list

@app.get('/email/blocked/address')
async def block_address():
    email: str ='[email protected]'
    checker: DefaultChecker = await default_checker()
    await checker.blacklist_add_email(email)

    return jsonify(status_code=200, content={"result": True})

Check email is blocked or not

@app.get('/email/blocked/address')
async def get_block_address():
    email: str ='[email protected]'
    checker: DefaultChecker = await default_checker()
    res = await checker.is_blocked_address(email)

    return jsonify(status_code=200, content={"result": res})

Check MX record

@app.get("/email/check-mx")
async def check_mx_record():
    checker = await default_checker()
    domain = "gmail.com"
    res = await checker.check_mx_record(domain, False)
    
    return jsonify(status_code=200, content={'result': res})

Remove email address from the blocked list

@app.get('/email/blocked/address')
async def del_blocked_address():
    checker = await default_checker()
    email = "[email protected]"
    res = await checker.blacklist_rm_email(email)

    return jsonify(status_code=200, content={"result": res})

Remove domain from the blocked list

@app.get('/email/blocked/domains')
async def del_blocked_domain():
    checker = await default_checker()
    domain = "gmail.com"
    res = await checker.blacklist_rm_domain(domain)

    return jsonify(status_code=200, content={"result": res})

Remove a domain from the temporary list

@app.get('/email/dispasoblee')
async def del_disp_domain():
    checker = await default_checker()
    domains = ["gmail.com"]
    res = await checker.blacklist_rm_temp(domains)

    return jsonify(status_code=200, content={'result': res})

WhoIsXmlApi

from flask_mailing.utils import WhoIsXmlApi

who_is = WhoIsXmlApi(token="Your access token", email="[email protected]")

print(who_is.smtp_check_())    #check smtp server
print(who_is.is_dispasoble()) # check email is disposable or not
print(who_is.check_mx_record()) # check domain mx records 
print(who_is.free_check) # check email domain is free or not