Skip to main content

Simple lightwigh mail sending for FastApi

Project description

Fastapi-mail

Fastapi with fastapi-mail simple lightweight

Fastapi mail system sending mails(individual, bulk) attachments(individual, bulk)

Installation

 $ pip install fastapi-mail

Email Send example

(this based on sending emails with gmail account, if you want your customize your config see below the example)
Before you dive in make sure you have already created App password for more information;
from fastapi import FastAPI
from starlette.responses import JSONResponse
from fastapi_mail import FastMail
from starlette.background import BackgroundTask
from test_examples.templates import  html, backgorund_task,bulkmail
from fastapi import Header,File, Body,Query, UploadFile

from pydantic import BaseModel
app = FastAPI()



class EmailSchema(BaseModel):
    email: str



#test email standart sending mail 
@app.post("/email")
async def my_awesome_func_1(email: EmailSchema) -> JSONResponse:



    mail = FastMail("your_account@gmail.com","*********",tls=True)

    await  mail.send_message(email.email,"Test email from fastapi-mail", html, text_format="html")

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


#this mail sending using starlettes background tasks, faster than the above one
@app.post("/emailbackground")
async def my_awesome_func_2(email: dict = Body(...)) -> JSONResponse:

    email = email.get("email")

    mail = FastMail("your_account@gmail.com","*********",tls=True)

    task = BackgroundTask(mail.send_message, email,"Test email from fastapi-mail with background task",backgorund_task,text_format="html")

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


#this an example of sending bulk mails
@app.post("/bulkemail")
async def my_awesome_func_3(email1: str=Body(...,embed=True),email2: str=Body(...,embed=True)) -> JSONResponse:

    email = ["someaddress@gmail.com","address2@gmail.com"]
    mail = FastMail("your_account@gmail.com","*********",tls=True)


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

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


#an example of sending bulk mails attaching files 
@app.post("/bulkfile")
async def my_awesome_func_4(file: UploadFile = File(...), file2: UploadFile = File(...)) -> JSONResponse:

    email = ["someaddress@gmail.com","address2@gmail.com"]
    mail = FastMail("your_account@gmail.com","*********",tls=True)


    task = BackgroundTask(mail.send_message, email,"Bulk mail from fastapi-mail with background task","Bulk mail Test",text_format="plain",bulk=True,file=[file,file2])

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


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

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.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

fastapi_mail-0.2.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi-mail-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.3

File hashes

Hashes for fastapi-mail-0.2.0.tar.gz
Algorithm Hash digest
SHA256 72a4241d1e16edf8c4bf76f3c6b33f484fb3c099f5f082571c0b641cab3d7b89
MD5 9527da1748b36011379ce4120999ff03
BLAKE2b-256 618e6f59ce66caab7e9fd9d035c8060932e8fa007df4691c839c0e71e741c3cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_mail-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.3

File hashes

Hashes for fastapi_mail-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56035d8594eadc06fc5b9d87ec7b2c9c4b9eba0f79c9333a104028da2aaede5b
MD5 831d5186f42cb5e20b96e8ebf3a0d39b
BLAKE2b-256 8b36b3f0c43f58311aa093ebee2e0401ed69aba7a28ec3a60b74bef9ee4ba95a

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