-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_mail.py
executable file
·134 lines (104 loc) · 3.32 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from __future__ import print_function
import time
import sys
# Import smtplib for the actual sending function
import smtplib
# For guessing MIME type
import mimetypes
# Import the email modules we'll need
import email
import email.mime.application
import pdb
final_data = []
if len(sys.argv) == 1:
print ("\nSYNTAX ERROR\nCorrect Syntax: python send_mail.py MAIL_PASSWORD_HERE\n")
quit()
else:
psswrd = sys.argv[1]
########################################
# IMPORTANT CONSTANTS
# File with e-mail address of targets
target_csv = 'target.csv'
# Sender E-Mail Address
email_id = '[email protected]'
# Enter your name/organisation name for identification
identification = 'E-Cell IIIT Hyderabad'
# Mailing Server
SMTP_server = 'mail.iiit.ac.in'
# Mail's Subject
subject = "You CA code for Megathon"
attachment_path_and_name = ""
########################################
def get_data():
# target.csv contains list of [S.No, Names, Email Addresses]
with open(target_csv) as f:
data = f.readlines()
data = [i.split(',') for i in data]
fi = []
for row in data[1:]:
fi.append({
'email': row[2],
'name': row[1].title(),
'attachment': attachment_path_and_name
})
return fi
mail_details = {
'email' : email_id,
'identity': identification,
'password' : psswrd,
'SMTP-server' : SMTP_server
}
def SEND_MAIL(name, to_EMAIL, attachment):
print("SENDING to %s" %(to_EMAIL))
TO_EMAIL = to_EMAIL
# Create a text/plain message
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = subject
msg['From'] = mail_details['identity'] + " <" + mail_details['email'] + ">"
msg['To'] = TO_EMAIL
msg.preamble = 'This is a multi-part message in MIME format.'
# The main body is just another attachment
with open('salutation.html', 'r') as f:
salutation = "".join(f.readlines())
with open('body.html', 'r') as f:
body = "".join(f.readlines())
with open('conclusion.html', 'r') as f:
conclusion = "".join(f.readlines())
content = salutation.strip() + " " + name.strip() + body.strip() + " " + conclusion.strip()
body = email.mime.Text.MIMEText(content, 'html')
msg.attach(body)
# Attachment
# filename=attachment
# with open("./" + filename) as fp:
# att = email.mime.application.MIMEApplication(fp.read(),_subtype="jpg")
# att.add_header('Content-Disposition','attachment',filename=filename)
# msg.attach(att)
# send via Gmail server
# NOTE: my ISP, Centurylink, seems to be automatically rewriting
# port 25 packets to be port 587 and it is trashing port 587 packets.
# So, I use the default port 25, but I authenticate.
s = smtplib.SMTP(mail_details['SMTP-server'])
s.starttls()
s.login(mail_details['email'], mail_details['password'])
s.sendmail(mail_details['email'], TO_EMAIL, msg.as_string())
s.quit()
def wait():
print("\n#########################################################")
print("############# SLOW DOWN! Give me a minute. ###########")
print("#########################################################\n")
time.sleep(60)
return
if __name__=="__main__":
final_data = get_data()
for i, data in enumerate(final_data):
print("%d/%d: "%(i+1, len(final_data)),end="")
try:
SEND_MAIL(data['name'], data['email'], data['attachment'])
except (smtplib.SMTPRecipientsRefused), err:
wait()
continue
if not i%100 and i!=0:
wait()
else:
time.sleep(2)
print("All Done!")