initial commit
This commit is contained in:
42
main.py
Normal file
42
main.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
import os
|
||||
|
||||
from email_sender import EmailSender
|
||||
|
||||
app = FastAPI(title="Email Sender Service")
|
||||
|
||||
class SendRequest(BaseModel):
|
||||
subject: str
|
||||
content: str
|
||||
|
||||
# create a single EmailSender instance for the app
|
||||
es = EmailSender()
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
@app.post("/send")
|
||||
def send_email(req: SendRequest):
|
||||
"""Send an email from configured sender to configured recipient.
|
||||
|
||||
Expects JSON: {"subject": "...", "content": "..."}
|
||||
"""
|
||||
# basic config validation
|
||||
missing = []
|
||||
if not es.email_id:
|
||||
missing.append("email")
|
||||
if not es.apppassword:
|
||||
missing.append("apppassword")
|
||||
if not es.receipt_email_id:
|
||||
missing.append("receipt_email")
|
||||
if missing:
|
||||
raise HTTPException(status_code=500, detail={"error": "missing_config", "missing": missing})
|
||||
|
||||
try:
|
||||
es.send_to_recipient(subject=req.subject, content=req.content)
|
||||
return {"status": "sent", "to": es.receipt_email_id}
|
||||
except Exception as e:
|
||||
# return the error message but avoid leaking sensitive info
|
||||
raise HTTPException(status_code=500, detail={"error": "send_failed", "message": str(e)})
|
||||
Reference in New Issue
Block a user