-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_mail.py
executable file
·57 lines (43 loc) · 1.57 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
def send_mail(fromaddress, toaddress, subject, text, files):
# instance of MIMEMultipart
msg = MIMEMultipart()
# storing the senders email address
msg['From'] = fromaddress
# storing the receivers email address
msg['To'] = toaddress
# storing the date
msg['Date'] = formatdate(localtime=True)
# storing the subject
msg['Subject'] = subject
# attach the body with the msg instance
msg.attach(MIMEText(text))
# open the file to be sent
filename = "User Activity.xlsx"
attachment = open(files, "rb")
# instance of MIMEBase and named as part
part = MIMEBase('application', "octet-stream")
# To change the payload into encoded form
part.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(part)
part.add_header('Content-Disposition',
"attachment; filename= %s" % filename)
msg.attach(part)
# creates SMTP session
smtp_session = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
smtp_session.starttls()
# Authentication
smtp_session.login(fromaddress, "*****")
# Converts the Multipart msg into a string
text = msg.as_string()
# sending the mail
smtp_session.sendmail(fromaddress, toaddress, text)
# terminating the session
smtp_session.quit()