Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import smtplib
|
| 2 |
+
from email.mime.text import MIMEText
|
| 3 |
+
from email.mime.multipart import MIMEMultipart
|
| 4 |
+
import os
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from pydantic import BaseModel, EmailStr
|
| 7 |
+
|
| 8 |
+
# --- Configuration ---
|
| 9 |
+
class SMTPSettings:
|
| 10 |
+
# Use os.getenv for all settings, with default values for non-sensitive ones
|
| 11 |
+
SMTP_HOST: str = os.getenv('SMTP_HOST', 'smtp.gmail.com')
|
| 12 |
+
SMTP_PORT: int = int(os.getenv('SMTP_PORT', 587))
|
| 13 |
+
SMTP_USER: str = os.getenv('SMTP_USER')
|
| 14 |
+
SMTP_PASS: str = os.getenv('SMTP_PASS')
|
| 15 |
+
RECIPIENT_EMAIL: str = os.getenv('RECIPIENT_EMAIL')
|
| 16 |
+
SENDER_NAME: str = os.getenv('SENDER_NAME', 'FastAPI Mailer')
|
| 17 |
+
SENDER_EMAIL: str = os.getenv('SENDER_EMAIL')
|
| 18 |
+
|
| 19 |
+
# Basic validation to ensure required fields are set
|
| 20 |
+
if not all([SMTP_USER, SMTP_PASS, RECIPIENT_EMAIL, SENDER_EMAIL]):
|
| 21 |
+
raise EnvironmentError("Missing one or more required environment variables: SMTP_USER, SMTP_PASS, RECIPIENT_EMAIL, SENDER_EMAIL")
|
| 22 |
+
|
| 23 |
+
settings = SMTPSettings()
|
| 24 |
+
|
| 25 |
+
# --- Pydantic Models ---
|
| 26 |
+
class EmailRequest(BaseModel):
|
| 27 |
+
subject: str
|
| 28 |
+
body: str
|
| 29 |
+
recipient_email: EmailStr = settings.RECIPIENT_EMAIL # Default to the configured recipient
|
| 30 |
+
|
| 31 |
+
# --- Email Utility Function ---
|
| 32 |
+
def send_email(subject: str, body: str, recipient_email: str):
|
| 33 |
+
try:
|
| 34 |
+
# Create a multipart message and set headers
|
| 35 |
+
message = MIMEMultipart()
|
| 36 |
+
message['From'] = f"{settings.SENDER_NAME} <{settings.SENDER_EMAIL}>"
|
| 37 |
+
message['To'] = recipient_email
|
| 38 |
+
message['Subject'] = subject
|
| 39 |
+
|
| 40 |
+
# Add body to email
|
| 41 |
+
message.attach(MIMEText(body, 'plain'))
|
| 42 |
+
|
| 43 |
+
# Connect to the SMTP server
|
| 44 |
+
with smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT) as server:
|
| 45 |
+
server.starttls() # Secure the connection
|
| 46 |
+
server.login(settings.SMTP_USER, settings.SMTP_PASS)
|
| 47 |
+
server.sendmail(settings.SENDER_EMAIL, recipient_email, message.as_string())
|
| 48 |
+
|
| 49 |
+
return {"message": "Email sent successfully"}
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Error sending email: {e}")
|
| 53 |
+
raise HTTPException(status_code=500, detail=f"Failed to send email: {e}")
|
| 54 |
+
|
| 55 |
+
# --- FastAPI Application ---
|
| 56 |
+
app = FastAPI(title="FastAPI SMTP Server")
|
| 57 |
+
|
| 58 |
+
@app.get("/")
|
| 59 |
+
def read_root():
|
| 60 |
+
return {"message": "FastAPI SMTP Server is running."}
|
| 61 |
+
|
| 62 |
+
@app.post("/send-test-email")
|
| 63 |
+
def send_test_email_endpoint():
|
| 64 |
+
subject = "FastAPI SMTP Test Email"
|
| 65 |
+
body = "This is a test email sent from the FastAPI server using the configured SMTP settings."
|
| 66 |
+
try:
|
| 67 |
+
send_email(subject, body, settings.RECIPIENT_EMAIL)
|
| 68 |
+
return {"status": "success", "message": f"Test email sent to {settings.RECIPIENT_EMAIL}"}
|
| 69 |
+
except HTTPException as e:
|
| 70 |
+
raise e
|
| 71 |
+
|
| 72 |
+
@app.post("/send-email")
|
| 73 |
+
def send_email_endpoint(email_request: EmailRequest):
|
| 74 |
+
try:
|
| 75 |
+
send_email(email_request.subject, email_request.body, email_request.recipient_email)
|
| 76 |
+
return {"status": "success", "message": f"Email sent to {email_request.recipient_email}"}
|
| 77 |
+
except HTTPException as e:
|
| 78 |
+
raise e
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
import uvicorn
|
| 82 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|