-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_utils.py
36 lines (30 loc) · 1.21 KB
/
email_utils.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
# from aiosmtplib import SMTP
# from email.message import EmailMessage
# async def send_email(recipient: str, subject: str, body: str):
# message = EmailMessage()
# message["From"] = "[email protected]"
# message["To"] = recipient
# message["Subject"] = subject
# message.set_content(body)
# async with SMTP(hostname="smtp.gmail.com", port=587) as smtp:
# await smtp.login("[email protected]", "hArrYPOTTER@4")
# await smtp.send_message(message)
import aiosmtplib
from email.message import EmailMessage
import os
async def send_email(recipient: str, subject: str, body: str):
message = EmailMessage()
message["From"] = os.getenv("EMAIL_FROM", "[email protected]")
message["To"] = recipient
message["Subject"] = subject
message.set_content(body)
try:
smtp = aiosmtplib.SMTP(hostname="smtp.gmail.com", port=587, use_tls=True)
await smtp.connect()
await smtp.login(os.getenv("EMAIL_USER"), os.getenv("EMAIL_PASSWORD"))
await smtp.send_message(message)
await smtp.quit()
print(f"Email sent successfully to {recipient}")
except Exception as e:
print(f"Failed to send email: {str(e)}")
raise