-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_handler.py
More file actions
27 lines (22 loc) · 994 Bytes
/
email_handler.py
File metadata and controls
27 lines (22 loc) · 994 Bytes
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
from os import getenv
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Get the environment variables from docker-compose
sender_email: str = getenv("SENDER_EMAIL")
sender_password: str = getenv("SENDER_PASSWORD")
def send_html_email(email: str, subject: str, html: str):
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(sender_email, sender_password)
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = sender_email
message["To"] = email
# Turn these into plain/html MIMEText objects
final_message = MIMEText(html, "html")
message.attach(final_message)
# Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, email, message.as_string())