-
Notifications
You must be signed in to change notification settings - Fork 5
/
send.py
120 lines (94 loc) · 4.11 KB
/
send.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
# -*- coding: utf-8
import csv
import sys
import json
import smtplib
import os
import time
from email.mime.text import MIMEText
import getpass
def read_recipient_file(fname, recipient_id):
with open(fname, "rb") as file:
for row in csv.reader(file, delimiter="\t"):
email_addr,id = row
if id == recipient_id:
return email_addr
return None
def make_msg(sender_addr, recipient_addr, title, body):
msg = MIMEText(body, "plain", "utf-8")
msg["Subject"] = title
msg["From"] = sender_addr
msg["To"] = recipient_addr
return msg
def send(conf, email_addr, msg_lines):
smtps_host = conf["smtps_host"]
conn = smtplib.SMTP_SSL(smtps_host)
conn.ehlo_or_helo_if_needed()
smtp_user = conf["smtp_user"]
#smtp_password = conf["smtp_password"]
smtp_password = getpass.getpass()
conn.login(smtp_user, smtp_password)
conn.sendmail(conf["sender_address"], email_addr, msg_lines)
conn.quit()
def get_connection(conf):
# Function for establishing and returning an smtp connection
# to given host
smtps_host = conf["smtps_host"]
conn = smtplib.SMTP_SSL(smtps_host)
conn.ehlo_or_helo_if_needed()
smtp_user = conf["smtp_user"]
smtp_password = conf["smtp_password"]
# smtp_password = getpass.getpass()
conn.login(smtp_user, smtp_password)
return conn
def read_config(conf_file_name):
return json.load(open(conf_file_name, "rb"))
def main():
args = sys.argv[1:]
if len(args) >= 2:
conf = read_config(args[0])
email_file = conf["email_file"]
# Get SMTP connection:
conn = get_connection(conf)
dir_name = args[1]
for fname in os.listdir(dir_name):
if fname.endswith(".txt"):
path = os.path.join(dir_name, fname)
recipient_id = fname.replace(".txt","")
title = conf["title"] % {"id":recipient_id}
body = open(path, "rb").read()
recipient_addr = read_recipient_file(email_file, recipient_id)
print >> sys.stderr, "Sending", recipient_id, recipient_addr
if recipient_addr:
msg = make_msg(conf["sender_address"], recipient_addr, title, body)
while True:
try:
#send(conf, recipient_addr, msg.as_string()) # Replaced with send_v2:
#send_v2(conn, conf, recipient_addr, msg.as_string())
# TBD: check if conn is connected
conn.sendmail(conf["sender_address"], recipient_addr, msg.as_string())
print >> sys.stderr, "Sent", recipient_id, recipient_addr
break # No need to retry
except smtplib.SMTPRecipientsRefused, e:
error_code = e.recipients.values()[0][0]
if error_code == 501:
print >> sys.stderr, "Invalid recipient, not retrying", recipient_id, recipient_addr, e
break # Break retry loop
elif error_code == 451:
print >> sys.stderr, "Throttled, retrying later", recipient_id, recipient_addr, e
else:
print >> sys.stderr, "Unknown error, not retrying", recipient_id, recipient_addr, e
break # Break retry loop
except Exception, e:
print >> sys.stderr, "Unknown exception type, not retrying", recipient_id, recipient_addr, e
break # Break retry loop
time.sleep(60)
else:
print >> sys.stderr, "No e-mail address for", recipient_id
# End SMTP connection
conn.quit()
print "SMTP connection closed"
else:
print "Usage: send.py conf-file recipient-id"
if __name__ == '__main__':
main()