-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (54 loc) · 2.08 KB
/
Copy pathmain.py
File metadata and controls
66 lines (54 loc) · 2.08 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import List, Literal
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import aiosmtplib
from email.message import EmailMessage
from dotenv import load_dotenv
import os
load_dotenv()
app = FastAPI()
class EmailSchema(BaseModel):
email: List[str]
subject: str
body: dict
template: Literal['greeting', 'news', 'verification']
def read_html_template(file_path: str) -> str:
with open(file_path, 'r') as file:
template = file.read()
return template
async def send_email(email: EmailSchema):
if email.template == 'greeting':
template = read_html_template('templates/greeting.html')
html_content = template.replace('{{ name }}', email.body['name'])
elif email.template == 'news':
template = read_html_template('templates/news.html')
html_content = template.replace('{{ news }}', email.body['news'])
elif email.template == 'verification':
template = read_html_template('templates/verification.html')
html_content = template.replace('{{ code }}', email.body['code'])
else:
raise HTTPException(status_code=400, detail="Invalid email body type")
message = EmailMessage()
message["From"] = "[email protected]"
message["Subject"] = email.subject
message.add_alternative(html_content, subtype='html')
try:
for recipient in email.email:
message["To"] = recipient
await aiosmtplib.send(
message,
hostname="smtp.gmail.com",
port=465,
use_tls=True,
username="[email protected]",
password=os.getenv('GMAIL_KEY', "xxxxxxxx")
)
except aiosmtplib.SMTPException as smtp_error:
raise HTTPException(
status_code=500, detail=f"SMTP error: {smtp_error}")
except Exception as e:
raise HTTPException(status_code=500, detail=f"General error: {e}")
@app.post("/send-email/")
async def send_email_endpoint(email: EmailSchema):
await send_email(email)
return {"message": "Email has been sent"}