In this example we will send an email through a POST type endpoint that will receive the information from the recipient
pip install fastapi uvicorn
# main.py
from fastapi import FastAPI
from fastapi.exceptions import HTTPException
from pydantic import BaseModel
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
app = FastAPI()
OWN_EMAIL = "hello@example.com"
OWN_EMAIL_PASSWORD = "your-password"
class EmailBody(BaseModel):
to: str
subject: str
message: str
@app.post("/email")
async def send_email(body: EmailBody):
try:
msg = MIMEText(body.message, "html")
msg['Subject'] = body.subject
msg['From'] = f'Denolyrics <{OWN_EMAIL}>'
msg['To'] = body.to
port = 465 # For SSL
# Connect to the email server
server = SMTP_SSL("mail.privateemail.com", port)
server.login(OWN_EMAIL, OWN_EMAIL_PASSWORD)
# Send the email
server.send_message(msg)
server.quit()
return {"message": "Email sent successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=e)
Run project
uvicorn main:app --reload --port 8000
Top comments (1)
Great, Thanks!