-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
127 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from fastapi import FastAPI, BackgroundTasks, UploadFile, File, Form | ||
from starlette.responses import JSONResponse | ||
from .templates import html, template,bulkmail | ||
from .schema import EmailSchema, EmailStr | ||
from starlette.requests import Request | ||
from fastapi_mail import FastMail, MessageSchema,ConnectionConfig | ||
|
||
|
||
conf = ConnectionConfig( | ||
MAIL_USERNAME = "your_username", | ||
MAIL_PASSWORD = "strong_password", | ||
MAIL_FROM = "[email protected]", | ||
MAIL_PORT = 587, | ||
MAIL_SERVER = "your mail server", | ||
MAIL_TLS = True, | ||
MAIL_SSL = False | ||
) | ||
|
||
app = FastAPI() | ||
|
||
#test email standart sending mail | ||
@app.post("/email") | ||
async def simple_send(email: EmailSchema) -> JSONResponse: | ||
|
||
message = MessageSchema( | ||
subject="Fastapi-Mail module", | ||
recipients=email.dict().get("email"), | ||
body=html, | ||
subtype="html" | ||
) | ||
|
||
fm = FastMail(conf) | ||
await fm.send_message(message) | ||
return JSONResponse(status_code=200, content={"message": "email has been sent"}) | ||
|
||
|
||
|
||
#this mail sending using starlettes background tasks, faster than the above one | ||
@app.post("/emailbackground") | ||
async def send_in_background(background_tasks: BackgroundTasks,email: EmailSchema) -> JSONResponse: | ||
|
||
message = MessageSchema( | ||
subject="Fastapi mail module", | ||
recipients=email.dict().get("email"), | ||
body="Simple background task ", | ||
) | ||
|
||
fm = FastMail(conf) | ||
|
||
background_tasks.add_task(fm.send_message,message) | ||
|
||
return JSONResponse(status_code=200, content={"message": "email has been sent"}) | ||
|
||
|
||
|
||
#an example of sending attachments | ||
@app.post("/file") | ||
async def send_file(background_tasks: BackgroundTasks,file: UploadFile = File(...),email:EmailStr = Form(...)) -> JSONResponse: | ||
|
||
message = MessageSchema( | ||
subject="Fastapi mail module", | ||
recipients=[email], | ||
body="Simple background task ", | ||
attachments=[file] | ||
) | ||
|
||
fm = FastMail(conf) | ||
|
||
background_tasks.add_task(fm.send_message,message) | ||
|
||
return JSONResponse(status_code=200, content={"message": "email has been sent"}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import os | ||
import pytest | ||
import fakeredis.aioredis | ||
from pytest_mock import mock | ||
|
||
from fastapi_mail.email_utils import DefaultChecker | ||
from fastapi_mail.config import ConnectionConfig | ||
|
||
|
@@ -29,14 +27,17 @@ async def redis_checker(scope="redis_config"): | |
def mail_config(): | ||
env = { | ||
"MAIL_USERNAME": "[email protected]", | ||
"MAIL_PASSWORD": "strong password", | ||
"MAIL_PASSWORD":"strong", | ||
"MAIL_FROM": "[email protected]", | ||
"MAIL_PORT": "587", | ||
"MAIL_SERVER": "smtp.gmail.com", | ||
"MAIL_TLS": "True", | ||
"MAIL_SSL": "False", | ||
"SUPPRESS_SEND": "1", | ||
"USE_CREDENTIALS": "True" | ||
"MAIL_FROM_NAME": "example", | ||
"MAIL_PORT": 25, | ||
"MAIL_SERVER": "localhost", | ||
"MAIL_TLS": False, | ||
"MAIL_SSL": False, | ||
"MAIL_DEBUG": 0, | ||
"SUPPRESS_SEND": 1, | ||
"USE_CREDENTIALS": False, | ||
"TEMPLATE_FOLDER": None, | ||
} | ||
|
||
yield env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
from fastapi_mail import FastMail, MessageSchema,ConnectionConfig | ||
|
||
@pytest.mark.asyncio | ||
async def test_connection(mail_config): | ||
message = MessageSchema( | ||
subject="test subject", | ||
recipients=["[email protected]"], | ||
body="test", | ||
subtype="plain" | ||
) | ||
conf = ConnectionConfig(**mail_config) | ||
|
||
fm = FastMail(conf) | ||
await fm.send_message(message) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,10 @@ | |
def test_configuration(mail_config): | ||
conf = ConnectionConfig(**mail_config) | ||
assert conf.MAIL_USERNAME == "[email protected]" | ||
assert conf.MAIL_PORT == 587 | ||
assert conf.MAIL_PORT == 25 | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters