habulaj commited on
Commit
a006411
·
verified ·
1 Parent(s): d6c00d5

Create emails.py

Browse files
Files changed (1) hide show
  1. routes/emails.py +49 -0
routes/emails.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import smtplib
2
+ from email.message import EmailMessage
3
+ from fastapi import APIRouter, HTTPException
4
+ from pydantic import BaseModel
5
+
6
+ router = APIRouter()
7
+
8
+ # Configurações de SMTP
9
+ SMTP_HOST = "smtp.gmail.com"
10
+ SMTP_PORT = 465
11
+ SMTP_USERNAME = "[email protected]"
12
+ SMTP_PASSWORD = "dinn yvdv kevl jmds" # De preferência, use variáveis de ambiente!
13
+
14
+ SENDER_EMAIL = "[email protected]"
15
+ SENDER_NAME = "Ameddes"
16
+
17
+ class EmailSchema(BaseModel):
18
+ to: str
19
+ subject: str
20
+ content: str
21
+ html: bool = False # Define se é um e-mail em HTML
22
+
23
+ def send_email(to_email: str, subject: str, content: str, is_html: bool = False):
24
+ msg = EmailMessage()
25
+ msg["Subject"] = subject
26
+ msg["From"] = f"{SENDER_NAME} <{SENDER_EMAIL}>"
27
+ msg["To"] = to_email
28
+
29
+ if is_html:
30
+ msg.add_alternative(content, subtype='html')
31
+ else:
32
+ msg.set_content(content)
33
+
34
+ try:
35
+ with smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT) as smtp:
36
+ smtp.login(SMTP_USERNAME, SMTP_PASSWORD)
37
+ smtp.send_message(msg)
38
+ except Exception as e:
39
+ raise HTTPException(status_code=500, detail=f"Erro ao enviar email: {str(e)}")
40
+
41
+ @router.post("/send-email")
42
+ def send_email_route(email_data: EmailSchema):
43
+ send_email(
44
+ to_email=email_data.to,
45
+ subject=email_data.subject,
46
+ content=email_data.content,
47
+ is_html=email_data.html
48
+ )
49
+ return {"detail": "E-mail enviado com sucesso!"}