forked from bhanu-lab/PythonScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailNotification.py
More file actions
112 lines (91 loc) · 3.66 KB
/
EmailNotification.py
File metadata and controls
112 lines (91 loc) · 3.66 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
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
'''
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
'''
# 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('C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/resources/from_mail.txt') # get my account details
#print(my_address)
#print(my_password)
names, emails = get_contacts('C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/resources/test_contacts.txt') # read contacts
# print(emails)
# return
message_template = get_message_template('C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/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())
print(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
#Todo - Take Command Line Argument to send files - may be path to files
path = 'C:/Users/Rajesh.Gundupalli/Desktop/PythonScripts/resources/EMailAttachments/' #ToDo
files = os.listdir(path) #f.name for f in os.scandir(path) if f.is_file() #os.listdir(path)
'''for fname in os.scandir(path):
if(fname.is_file()):
print fname'''
#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')
# 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" % name)
msg.attach(part)
attachment.close()
#send the mail
failed_rcpts = server.sendmail(my_address, email, msg.as_string().encode())
del msg
# failed receipt will be printed here
print(failed_rcpts)
server.quit() # stopping SMTP server
if __name__ == '__main__':
main()