-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_mail.py
30 lines (26 loc) · 1003 Bytes
/
send_mail.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
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv
load_dotenv()
username = os.getenv('MY_USERNAME')
password = os.getenv('MY_PASSWORD')
def send_mail(text="Email body", subject="Hello Wolrd", from_email="Zajjaj Khan <[email protected]>", to_emails=None):
assert isinstance(to_emails, list)
msg = MIMEMultipart("alternative")
msg['From'] = from_email
msg['To'] = ",".join(to_emails)
msg['Subject'] = subject
txt_part = MIMEText(text, 'plain')
msg.attach(txt_part)
html_part = MIMEText('<h1>This is a Heading</h1>', 'html')
# msg.attach(html_part) // this is a html part if you wana include that...
msg_str = msg.as_string()
# Login to my smtp server
server = smtplib.SMTP(host="smtp.gmail.com", port=587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(from_email, to_emails, msg_str)
server.quit()