forked from libertytechforce/http2smtp
27 lines
743 B
Python
27 lines
743 B
Python
from fastapi import FastAPI
|
|
from fastapi.responses import JSONResponse, RedirectResponse
|
|
|
|
from http2smtp.models import Email
|
|
from http2smtp.message import BodyRequiredError, build_message
|
|
from http2smtp.smtp import send_email
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get('/')
|
|
async def index() -> RedirectResponse:
|
|
return RedirectResponse(url='/docs')
|
|
|
|
|
|
@app.post('/smtp')
|
|
async def smtp_send(email: Email):
|
|
try:
|
|
message = build_message(email)
|
|
except BodyRequiredError as e:
|
|
return JSONResponse(status=400, content={'error': str(e)})
|
|
try:
|
|
send_email(email.from_email, email.password, message)
|
|
except Exception as e:
|
|
return JSONResponse(status=400, content={'error': str(e)})
|
|
return {"status": "success"}
|