FastAPI

Mailtrap can be integrated with FastAPI apps and projects for email sending, find out how to do it.

Before we start, you'll need to:

Send emails using FastAPI and Mailtrap

To integrate Mailtrap and send emails via FastAPI, simply copy/paste the following script into your configuration:

fastapi-example.py
import mailtrap as mt
from typing import Dict
from fastapi import FastAPI

app = FastAPI()

@app.post("/")
def send_mail() -> Dict:
    mail = mt.Mail(
        sender=mt.Address(email="YOUR-EMAIL-HERE", name="Mailtrap Test"),
        to=[mt.Address(email="RECIPIENT-EMAIL-HERE")],
        subject="Hello World",
        html="<strong>it works!</strong>",
    )

    client = mt.MailtrapClient(token="YOUR-MAILTRAP-API-KEY-HERE")
    response = client.send(mail)

    return response

Once you copy the script, make sure to insert your Mailtrap API token in the token= field and enter your and your recipient's emails in the sender= and to= fields.

To learn more about API integration, click here.

Last updated

Was this helpful?