-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEmailNotification.py
More file actions
115 lines (91 loc) · 3.36 KB
/
EmailNotification.py
File metadata and controls
115 lines (91 loc) · 3.36 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from string import Template
from email.mime.base import MIMEBase
from email import encoders
import datetime
import os.path as op
import os
import sys
import logging
'''
Author: @blackram
Contributer: R@jesh
A simple python script to send email notifications for multiple receipeints based on the
SMPT lib available in python.
***requirements***
need to install email package for this script
'''
# Setting up logger
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# function for reading names and emails from text file
def get_contacts(file_name):
names = []
emails = []
with open(file_name, mode='r') as contacts_file:
for a_contact in contacts_file:
names.append(a_contact.split()[0])
emails.append(a_contact.split()[1])
return names, emails
# function for reading a message template
def get_message_template(file_name):
with open(file_name, mode='r') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
# function to get my account details to send mail
def get_my_account_info(file_name):
with open(file_name, mode='r') as my_details:
for my_det in my_details:
my_address = my_det.split()[0]
my_password = my_det.split()[1]
return my_address, my_password
# main function
def main():
my_address, my_password = get_my_account_info('resources/from_mail.txt') # get my account details
#print(my_address)
#print(my_password)
names, emails = get_contacts('resources/test_contacts.txt') # read contacts
message_template = get_message_template('resources/email_template.txt') # read template
# using gmail smtp
server = smtplib.SMTP(host='smtp.gmail.com', port='587')
server.starttls()
# Log into server
server.login(my_address, my_password)
failed_rcpts = {}
for name, email in zip(names, emails):
msg = MIMEMultipart()
message = message_template.substitute(PERSON_NAME=name.title())
logger.info("Message being sent is : "+message)
msg['From'] = my_address
msg['To'] = email
msg['Subject'] = "Email Notification Test "+str(datetime.date.today())
msg.attach(MIMEText(message, 'plain'))
part = MIMEBase('application', "octet-stream")
# adding attachement to the mail message
# param_length = sys.argv[0]
attachment = True
#Todo - Take Command Line Argument to send files - may be path to files
# attaching only if file name mentioned in parameter
if attachment:
path = 'resources/EMailAttachments/'
files = os.listdir(path)
#Attaching all the files in the directory
for name in files:
print(name)
filePath = path + name
attachment = open(filePath, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
part.add_header('Content-Disposition', "attachment; filename= %s" % name)
msg.attach(part)
# send the mail
failed_rcpts = server.sendmail(my_address, email, msg.as_string())
del msg
# failed receipt will be printed here
logger.info("failed receipeints are "+ failed_rcpts)
server.quit() # stopping SMTP server
if __name__ == '__main__':
main()