Skip to main content

Simple lightwigh mail sending for FastApi

Project description

Fastapi-mail

The fastapi-mail simple lightweight mail system, sending emails and attachments(individual && bulk)

Downloads

Installation

 $ pip install fastapi-mail

notes

In order to run the application use command below

uvicorn test_examples.main:app --reload  --port 8001

Standart email sending example

from fastapi import FastAPI, BackgroundTasks
from starlette.responses import JSONResponse
from fastapi_mail import FastMail
from fastapi import Header,File, Body,Query, UploadFile
from test_examples.schema import  EmailSchema
from pydantic import BaseModel,EmailStr

app = FastAPI()


html = """
<html> 
<body>
<p>Hi This test mail
<br>Thanks for using Fastapi-mail</p> 
</body> 
</html>
"""

template = """
<html> 
<body>
<p>Hi This test mail using BackgroundTasks
<br>Thanks for using Fastapi-mail</p> 
</body> 
</html>
"""


class EmailSchema(BaseModel):
    email: EmailStr



@app.post("/email")
async def awesome_fastapi_func_1(email: EmailSchema) -> JSONResponse:
    #as gmail requires TLS connection, therefore you require to set tls to True
    mail = FastMail(email="your_account@gmail.com",password="your_pass",tls=True,port="587",service="gmail")

    await  mail.send_message(recipient=email.email,subject="Test email from fastapi-mail", body=html, text_format="html")
    
    return JSONResponse(status_code=200, content={"message": f"email has been sent {email.email} address"})

        

Sending email as background task

   
@app.post("/emailbackground")
async def awesome_fastapi_func_2(background_tasks: BackgroundTasks,email: str = Body(...,embed=True)) -> JSONResponse:

    #this mail sending using fastapi background tasks, faster than the above one
    #Using Postman you can send post request, adding email in the body

    {
        "email": "recipient@gmail.com"
    }

    #for more information about background-tasks see the link below
    #https://fastapi.tiangolo.com/tutorial/background-tasks/

    mail = FastMail(email="your_account@gmail.com",password="your_pass",tls=True,port="587",service="gmail")


    background_tasks.add_task(mail.send_message, recipient=email,subject="testing HTML",body=template,text_format="html")
    

    return JSONResponse(status_code=200, 
                        content={"message": f"email has been sent {email} address"})

Sending bulk mail

@app.post("/bulkemail")
async def awesome_fastapi_func_3(background_tasks: BackgroundTasks,emails: str=Body(...,embed=True)) -> JSONResponse:
    
   #this an example of sending bulk emails 
   ##Using Postman you can send post request, adding email in the body #example below
    {
        "emails": ["recipient1@gmail.com","recipient2@gmail.com"]
    }
    mail = FastMail(email="your_account@gmail.com",password="your_pass",tls=True,port="587",service="gmail")

    background_tasks.add_task(mail.send_message,recipient=[email1,email2],subject="Bulk mail from fastapi-mail with background task",body="Bulk mail Test",text_format="plain",bulk=True)


    return JSONResponse(status_code=200, content={"message": f"email has been sent to these {email1,email2} addresses"})

Sending bulk mails && files

@app.post("/bulkfile")
async def awesome_fastapi_func_4(background_tasks: BackgroundTasks,request: Request) -> JSONResponse:
    
    #this example of sending bulk email and sending multiple files
    mail = FastMail(email="your_account@gmail.com",password="your_pass",tls=True,port="587",service="gmail")

    temp = await  request.form()
    files= []

    for  value in temp.values():
        files.append(value)
 

    email = ["account_1@gmail.com","account_2@gmail.com"]
    mail = FastMail("******@gmail.com","******",tls=True)
    
    background_tasks.add_task(mail.send_message, recipient=email,subject="Bulk mail from fastapi-mail with background task",body="Bulk mail Test",text_format="plain",bulk=True,file=files)


    return JSONResponse(status_code=200, content={"message": f"email has been sent to these {email} addresses"})

Custom Mail server example

@app.post("/custom")
async def awesome_fastapi_func_5(email: EmailSchema) -> JSONResponse:
    #In order to use custom mail service, you require to send services as keyword, and argument your mail server name. You must provide other information according to your mail server
    mail = FastMail(email="your_account@gmail.com",password="your_pass",tls=True,ssl=True,port="465",custom=True,services="info.v1.example.com")

    await  mail.send_message(recipient=email.email,subject="Test email from fastapi-mail", body=html, text_format="html")
    
    return JSONResponse(status_code=200, content={"message": f"email has been sent {email.email} address"})

Contributing

Fell free to open issue and send pull request.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-mail-0.2.7.5.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

fastapi_mail-0.2.7.5-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-mail-0.2.7.5.tar.gz.

File metadata

  • Download URL: fastapi-mail-0.2.7.5.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

File hashes

Hashes for fastapi-mail-0.2.7.5.tar.gz
Algorithm Hash digest
SHA256 d19ab8083bd2e6da6eb8cf8261ecd9e77b0a1fb62d4ea53da2175835e796f041
MD5 19ba83852d88068aac77e9cda901b26c
BLAKE2b-256 975952a43293de595d4622d813d550590e121fd8d88083d5111e0b054a17d0ec

See more details on using hashes here.

File details

Details for the file fastapi_mail-0.2.7.5-py3-none-any.whl.

File metadata

  • Download URL: fastapi_mail-0.2.7.5-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

File hashes

Hashes for fastapi_mail-0.2.7.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b7c5884a21354d73e559d27c3e79d006f78f4649c4ab9b03d295d10d702d3740
MD5 237f0fde481e8d5a87c4a3dedbbb2656
BLAKE2b-256 d3643971457e9864007bab1a0c025d16cab14c5242f8cd87e4c1293e562423d2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page