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 File, Body,Query, UploadFile

app = FastAPI()

#test email standart sending mail 



#test email standart sending mail 
@app.post("/email")
async def awesome_fastapi_1(email: dict= Body(...)) -> JSONResponse:

    email = email.get("email")

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

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

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


#this mail sending using starlettes background tasks, faster than the above one
@app.post("/emailbackground")
async def awesome_fastapi_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 awesome_fastapi_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 awesome_fastapi_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)




#in order to use custom make sure you custom to True. Then  pass the  service name
@app.post("/custom")
async def awesome_fastapi_5(email: dict= Body(...)) -> JSONResponse:

    email = email.get("email")

    mail = FastMail("your_account","*********",tls=False,ssl=True,port="465",custom=True,services="your services")

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

    return JSONResponse(status_code=200, content={"message": f"email has been sent {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.1.7.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi-mail-0.1.7.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.1.7.tar.gz
Algorithm Hash digest
SHA256 9f4ce72ec42a642d069a04562284f0ed17f6187b8a36bdc6263aec3f0bb8a79a
MD5 35be8f8f91c4ba6446a1666189a627ac
BLAKE2b-256 47f9005e61bb2d22c019ffb3c6b1cddb92e9e8979a7b42f8aedc9c74910463e4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_mail-0.1.7-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.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 25d094ff201dbdcf74d028fb68d6a328970241d43890fb5e1c571b22ce607449
MD5 ea6c80bb447a332323bf6dcc3a10f2b6
BLAKE2b-256 3f5d2469283d742f7caaad64fe28ab677f5c4e9358ed182ce8c9a2ac1d528e5c

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