-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_utils.py
executable file
·34 lines (29 loc) · 972 Bytes
/
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
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv
import os
load_dotenv()
def send_email(subject, body):
sender_email = os.getenv('SENDER_EMAIL')
receiver_email = os.getenv('RECEIVER_EMAIL')
email_password = os.getenv('EMAIL_PASSWORD')
# create email msg headers
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
# create email msg body
msg.attach(MIMEText(body, 'plain'))
# connect to the server and send the email
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, email_password)
text = msg.as_string()
server.sendmail(sender_email, receiver_email, text)
print("email sent successfully")
except Exception as e:
print(f"failed to send email: {e}")
finally:
server.quit()