import uvicorn
from fastapi import FastAPI, BackgroundTasks, UploadFile, File, Form
from starlette.responses import JSONResponse
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from pydantic import BaseModel, EmailStr
from typing import List, Text
class EmailSchema(BaseModel):
email: List[EmailStr]
html: Text
conf = ConnectionConfig(
MAIL_USERNAME="qq123456",
MAIL_PASSWORD="password",
MAIL_FROM="qq123456@qq.com",
MAIL_PORT=587,
MAIL_SERVER="smtp.qq.com",
MAIL_TLS=True,
MAIL_SSL=False,
USE_CREDENTIALS=True,
VALIDATE_CERTS=True,
)
app = FastAPI()
html = """
<p>Thanks for using Fastapi-mail</p>
"""
@app.post("/email")
async def simple_send(email: EmailSchema) -> JSONResponse:
message = MessageSchema(
subject="Fastapi-Mail module",
recipients=email.dict().get(
"email"
),
body=email.dict().get("html", html),
subtype="html",
)
fm = FastMail(conf)
await fm.send_message(message)
return JSONResponse(status_code=200, content={"message": "email has been sent"})
@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"})
if __name__ == '__main__':
uvicorn.run('main:app', reload=True, host='127.0.0.1', port=8000)