-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmass.py
96 lines (80 loc) · 2.61 KB
/
mass.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
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
#TODO: Take emailid,password,filename as inputs
#TODO: Clean the data before writing to log.txt
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from time import sleep
import re
import io
# Turn ON less secure apps for Gmail (https://www.google.com/settings/security/lesssecureapps)
try:
s = smtplib.SMTP_SSL('smtp.domain.com', 465)
# Gmail client identification
# for TLS use port 587 and use smtplib.SMTP
s.ehlo()
# If using TLS, uncomment the line below.
# s.starttls()
s.login('[email protected]','password')
s.set_debuglevel(1)
except IOError:
print IOError
subject = """Dear Prof. """
body1 = """,
Lorem Ipsum Dolor"""
body2 = """,
Lorem Ipsum Dolor"""
sender = 'Name <[email protected]>'
names = []
field = []
recipients = []
# format Name,field,emailID
mailTxt = open("listOfRecipients.txt", 'r')
# Recipients being loaded
for line in mailTxt:
line = line.replace('\n',"")
names.append(re.split(',', line)[0])
field.append(re.split(',',line)[1])
recipients.append(re.split(',',line)[2])
for k in range(len(recipients)):
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipients[k]
f = "file.extension"
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=f
)
# For Attaching files
part['Content-Disposition'] = 'attachment; filename="%s"',f
msg.attach(part)
#TODO: Write code for html as well
part1 = MIMEText(salutation+names[k] + body1+field[k]+body2, 'text')
msg.attach(part1)
print "Sending..."
try:
s.sendmail(sender, recipients[k], msg.as_string())
# Basic error handling: sometimes the SSL/TLS connection is interrupted which will stop the script. When this happens, it will try to reconnect, send the message, and continue the loop.
except Exception, e:
#TODO: Write a function for establishing connection
print str(e) + "error: logging in and continuing loop with next address..."
s = smtplib.SMTP_SSL('smtp.domain.com',465)
s.ehlo()
s.login('[email protected]','yourpassword')
s.set_debuglevel(1)
continue
with io.open('log.txt', 'a', encoding='utf-8') as f:
try:
f.write(unicode(msg))
# As the code stands, it has trouble with accented characters. You might want to figure out a way to remove them or change the encoding of the script.
except:
f.write("Error handling ASCII encoded names: "+ unicode(recipients[k]))
print ("[*] "+ msg['To'])
#Sleeping to avoid the mail being spent in spam
print "Sleeping for 20 seconds..."
sleep(20)
print "Mails Sent."
f.close()
s.quit()