forked from hngprojects/hng_boilerplate_python_fastapi_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_case1.py
45 lines (37 loc) · 1.28 KB
/
test_case1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, EmailStr
from aiosmtplib import send
from email.message import EmailMessage
app = FastAPI()
# Email configuration
EMAIL = "[email protected]"
PASSWORD = "j*orWasSatc^TrdT7k7BGZ#"
SMTP_HOST = "work.timbu.cloud"
SMTP_PORT = 465
# Define a Pydantic model for the request body
class EmailRequest(BaseModel):
to_email: EmailStr
subject: str = "Test Email"
body: str = "This is a test email from FastAPI"
@app.post("/send-tinbu-mail")
async def send_email(email_request: EmailRequest):
# Create the email message
message = EmailMessage()
message["From"] = EMAIL
message["To"] = email_request.to_email
message["Subject"] = email_request.subject
message.set_content(email_request.body)
# SMTP configuration
smtp_settings = {
"hostname": SMTP_HOST,
"port": SMTP_PORT,
"username": EMAIL,
"password": PASSWORD,
"use_tls": True, # Use SSL/TLS for secure connection
}
try:
# Send the email
await send(message, **smtp_settings)
return {"message": f"Email sent to {email_request.to_email} successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}")