Sending Email using FastAPI Framework in Python
Before jumping into the topic directly, lets have an small intro about the technologies we are going to use. As the name suggests, we will be using FastAPI, a Python language framework.
FastAPI is a python framework to develop REST Apis. It is very easy to build, high performance, easy to learn and ready to deploy code. Though we are using FastAPI, explaining about it would be out of scope for the current article. For more details about FastAPI, refer below resources.
Official documentation – https://fastapi.tiangolo.com/
Tutorial for using Fast API in Python – https://www.geeksforgeeks.org/creating-first-rest-api-with-fastapi/
In the current article, we will we be covering how to send an email using FastAPI framework. We will sending the mail using fastapi-mail library. Fastapi-mail simple lightweight mail system, sending emails and attachments.
You can install library using
pip install fastapi-mail
We can use following article with and without login authentication. We will not be using any signup/authentication process as it is out of scope of the current article.
Creating the Project
Import the required libraries to send an email
from fastapi import FastAPI from fastapi_mail import FastMail, MessageSchema,ConnectionConfig from starlette.requests import Request from starlette.responses import JSONResponse from pydantic import EmailStr, BaseModel from typing import List app = FastAPI()
Now create a pydantic class to take email as input.
pydantic is a module that helps us to write validations to the inputs/requests.
class EmailSchema(BaseModel): email: List[EmailStr]
Setup the configuration to send an email using ConnectionConfig
conf = ConnectionConfig( MAIL_USERNAME=from_, MAIL_PASSWORD="************", MAIL_PORT=587, MAIL_SERVER="smtp.gmail.com", MAIL_TLS=True, MAIL_SSL=False )
For MAIL_SERVER argument,
if you would like to use gmail you can give “smtp.gmail.com” as input.
If you would like to use outlook as sender, you can give “smtp.office365.com” as input
Let’s setup the Message Schema.
It contains of the arguments like what to send and whom to send.
message = MessageSchema( subject="Fastapi-Mail module", recipients=email.dict().get("email"), # List of recipients, as many as you can pass body=template, subtype="html" )
After defining the configuration and MessageSchema,
we can send the email.
fm = FastMail(conf) await fm.send_message(message)
The entire code looks like this,
start the application using
uvicorn main:app --reload
After successful start of the server, use the following link to lookout the list of apis. In our case, we will have only a sending mail api(a POST request) when you can give input directly from the docs page itself.
Now execute the api by click on try it out and entering the desired email as shown below.
Once the api executes, it will returns the success output as mentioned in the return statement of api.
"message": "email has been sent"
And finally the output in the terminal,
it returns the status code(200), if the mail is sent successfully.
The output it is showing is the template we are sending through email.
You can even send the mail with attachments etc., Checkout Documentation for the same – https://pypi.org/project/fastapi-mail/
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course