| 4795 |
mandeep.dh |
1 |
from email.mime.text import MIMEText
|
|
|
2 |
from shop2020.clients.HelperClient import HelperClient
|
| 6906 |
rajveer |
3 |
from shop2020.clients.TransactionClient import TransactionClient
|
| 1425 |
varun.gupt |
4 |
from shop2020.thriftpy.utils.ttypes import Mail
|
| 4909 |
amar.kumar |
5 |
from shop2020.utils.daemon import Daemon
|
| 5299 |
mandeep.dh |
6 |
import optparse
|
| 4795 |
mandeep.dh |
7 |
import sched
|
|
|
8 |
import smtplib
|
|
|
9 |
import sys
|
| 5299 |
mandeep.dh |
10 |
import time
|
| 6906 |
rajveer |
11 |
import os
|
|
|
12 |
from email.MIMEMultipart import MIMEMultipart
|
|
|
13 |
from email.MIMEBase import MIMEBase
|
|
|
14 |
from email.MIMEText import MIMEText
|
|
|
15 |
from email import Encoders
|
| 1425 |
varun.gupt |
16 |
|
|
|
17 |
|
| 4909 |
amar.kumar |
18 |
class EmailSender(Daemon):
|
| 8056 |
rajveer |
19 |
def __init__(self, logfile='/var/log/utils/emailsender/emailsender.log', pidfile='/var/run/email-sender.pid', count = 0):
|
| 4909 |
amar.kumar |
20 |
Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
|
| 3425 |
varun.gupt |
21 |
print 'EmailSender initiated at', time.time()
|
| 1425 |
varun.gupt |
22 |
self.email_schedular = sched.scheduler(time.time, time.sleep)
|
| 3425 |
varun.gupt |
23 |
self.time_to_sleep = 2
|
| 8056 |
rajveer |
24 |
self.count = count
|
| 3425 |
varun.gupt |
25 |
self.authenticate()
|
|
|
26 |
|
|
|
27 |
def authenticate(self):
|
| 3400 |
varun.gupt |
28 |
try:
|
| 5299 |
mandeep.dh |
29 |
print "Attempting authentication at ", time.time()
|
|
|
30 |
self.sendGridMailServer = smtplib.SMTP("smtp.sendgrid.net", 587)
|
| 6016 |
rajveer |
31 |
self.sendGridSender = "euser"
|
| 5299 |
mandeep.dh |
32 |
self.sendGridPassword = "shop2020"
|
|
|
33 |
self.sendGridMailServer.ehlo()
|
|
|
34 |
self.sendGridMailServer.starttls()
|
|
|
35 |
self.sendGridMailServer.ehlo()
|
|
|
36 |
self.sendGridMailServer.login(self.sendGridSender, self.sendGridPassword)
|
|
|
37 |
|
|
|
38 |
self.gmailServer = smtplib.SMTP("smtp.gmail.com", 587)
|
|
|
39 |
self.gmailSender = "help@shop2020.in"
|
|
|
40 |
self.gmailPassword = "5h0p2o2o"
|
|
|
41 |
self.gmailServer.ehlo()
|
|
|
42 |
self.gmailServer.starttls()
|
|
|
43 |
self.gmailServer.ehlo()
|
|
|
44 |
self.gmailServer.login(self.gmailSender, self.gmailPassword)
|
| 3425 |
varun.gupt |
45 |
print "Authentication successful", time.time()
|
| 3400 |
varun.gupt |
46 |
|
|
|
47 |
except smtplib.SMTPConnectError as e:
|
| 4795 |
mandeep.dh |
48 |
print 'Specified host did not respond correctly', e
|
| 3400 |
varun.gupt |
49 |
except smtplib.SMTPHeloError as e:
|
|
|
50 |
print 'The server did not reply properly to the HELO greeting.', e
|
|
|
51 |
except smtplib.SMTPAuthenticationError as e:
|
|
|
52 |
print 'The server did not accept the username/password combination.', e
|
|
|
53 |
except smtplib.SMTPException as e:
|
|
|
54 |
print e
|
| 4795 |
mandeep.dh |
55 |
except Exception as e:
|
|
|
56 |
print sys.exc_info()[0]
|
|
|
57 |
print e
|
| 3425 |
varun.gupt |
58 |
|
| 6906 |
rajveer |
59 |
def get_attachment_part(self, document, filename):
|
|
|
60 |
part = MIMEBase('application', 'octet-stream')
|
|
|
61 |
part.set_payload(document)
|
|
|
62 |
Encoders.encode_base64(part)
|
| 6947 |
rajveer |
63 |
part.add_header('Content-Disposition', 'attachment; filename=%s' % filename + "-insurance-policy.pdf")
|
| 6906 |
rajveer |
64 |
return part
|
|
|
65 |
|
| 1425 |
varun.gupt |
66 |
def sendMail(self, mail):
|
| 5864 |
rajveer |
67 |
msg = MIMEMultipart()
|
|
|
68 |
|
|
|
69 |
msg['From'] = "help@saholic.com"
|
|
|
70 |
msg['To'] = ', '.join(mail.emailTo)
|
|
|
71 |
msg['Cc'] = ', '.join(mail.cc)
|
|
|
72 |
#msg['Bcc'] = ', '.join(mail.bcc)
|
|
|
73 |
msg['Subject'] = mail.subject
|
| 1425 |
varun.gupt |
74 |
|
| 5864 |
rajveer |
75 |
html_msg = MIMEText(mail.body, 'html')
|
|
|
76 |
msg.attach(html_msg)
|
| 6906 |
rajveer |
77 |
|
|
|
78 |
if mail.emailType == "DeliverySuccess":
|
|
|
79 |
tclient = TransactionClient().get_client()
|
|
|
80 |
document = tclient.getDocument(1, int(mail.source))
|
| 7040 |
amar.kumar |
81 |
if document != bin(0):
|
| 6906 |
rajveer |
82 |
msg.attach(self.get_attachment_part(document, mail.source))
|
|
|
83 |
|
| 3425 |
varun.gupt |
84 |
try:
|
| 5299 |
mandeep.dh |
85 |
self.setMailServerAndPassword(mail)
|
| 7550 |
anupam.sin |
86 |
rs = self.mailServer.sendmail(self.password, mail.emailTo + mail.cc + mail.bcc + ['backup@saholic.com'], msg.as_string())
|
| 3425 |
varun.gupt |
87 |
except smtplib.SMTPServerDisconnected as e:
|
|
|
88 |
print 'Server Disconnected at', time.time()
|
|
|
89 |
self.authenticate()
|
| 8065 |
amar.kumar |
90 |
rs = self.mailServer.sendmail(self.password, mail.emailTo, msg.as_string())
|
| 3425 |
varun.gupt |
91 |
|
|
|
92 |
print msg['To']
|
| 1425 |
varun.gupt |
93 |
# Should be mailServer.quit(), but that crashes...
|
|
|
94 |
return rs
|
|
|
95 |
|
| 5299 |
mandeep.dh |
96 |
def setMailServerAndPassword(self, mail):
|
| 5828 |
rajveer |
97 |
self.mailServer = self.sendGridMailServer
|
|
|
98 |
self.password = self.sendGridPassword
|
|
|
99 |
|
|
|
100 |
# self.mailServer = self.gmailServer
|
|
|
101 |
# self.password = self.gmailPassword
|
|
|
102 |
#
|
|
|
103 |
# for receiver in mail.to:
|
|
|
104 |
# if not receiver.endswith('@saholic.com') and not receiver.endswith('@shop2020.in'):
|
|
|
105 |
# self.mailServer = self.sendGridMailServer
|
|
|
106 |
# self.password = self.sendGridPassword
|
|
|
107 |
# break
|
| 5299 |
mandeep.dh |
108 |
|
| 1425 |
varun.gupt |
109 |
def send(self):
|
| 8056 |
rajveer |
110 |
if self.count > 10:
|
|
|
111 |
print str(self.count)
|
|
|
112 |
print "Number of failed attempts is more than 10. Exiting"
|
|
|
113 |
sys.exit()
|
| 1425 |
varun.gupt |
114 |
print "Despatch stared at ", time.time()
|
|
|
115 |
helper_client = HelperClient().get_client()
|
|
|
116 |
|
| 3086 |
rajveer |
117 |
emails_to_be_dispatched = helper_client.getEmailsToBeSent()
|
| 1425 |
varun.gupt |
118 |
print len(emails_to_be_dispatched)
|
|
|
119 |
|
|
|
120 |
for email in emails_to_be_dispatched:
|
| 3977 |
varun.gupt |
121 |
try:
|
| 5864 |
rajveer |
122 |
send_result = self.sendMail(email)
|
| 4796 |
mandeep.dh |
123 |
except Exception as e:
|
|
|
124 |
print sys.exc_info()[0]
|
|
|
125 |
print e
|
| 8020 |
rajveer |
126 |
print 'Error occurred sending emails.'
|
| 8056 |
rajveer |
127 |
self.count = self.count + 1
|
| 4971 |
mandeep.dh |
128 |
|
|
|
129 |
if len(send_result) == 0:
|
|
|
130 |
print "Sent at", time.time()
|
|
|
131 |
helper_client.markEmailAsSent(email.id)
|
|
|
132 |
else:
|
|
|
133 |
print "Failed sending email. Id:", " Time:", time.time()
|
| 8056 |
rajveer |
134 |
self.count = self.count + 1
|
| 3977 |
varun.gupt |
135 |
|
| 1425 |
varun.gupt |
136 |
self.email_schedular.enter(self.time_to_sleep, 1, self.send, ())
|
|
|
137 |
|
| 4909 |
amar.kumar |
138 |
def run(self):
|
| 1425 |
varun.gupt |
139 |
self.email_schedular.enter(self.time_to_sleep, 1, self.send, ())
|
|
|
140 |
self.email_schedular.run()
|
|
|
141 |
|
| 4909 |
amar.kumar |
142 |
if __name__ == "__main__":
|
|
|
143 |
parser = optparse.OptionParser()
|
|
|
144 |
parser.add_option("-l", "--logfile", dest="logfile",
|
|
|
145 |
type="string",
|
|
|
146 |
help="Log all output to LOG_FILE",
|
|
|
147 |
)
|
|
|
148 |
parser.add_option("-i", "--pidfile", dest="pidfile",
|
|
|
149 |
type="string",
|
|
|
150 |
help="Write the PID to pidfile")
|
|
|
151 |
(options, args) = parser.parse_args()
|
| 8056 |
rajveer |
152 |
daemon = EmailSender(options.logfile, options.pidfile, 0)
|
| 4909 |
amar.kumar |
153 |
if len(args) == 0:
|
|
|
154 |
daemon.run()
|
|
|
155 |
elif len(args) == 1:
|
|
|
156 |
if 'start' == args[0]:
|
|
|
157 |
daemon.start()
|
|
|
158 |
elif 'stop' == args[0]:
|
|
|
159 |
daemon.stop()
|
|
|
160 |
elif 'restart' == args[0]:
|
|
|
161 |
daemon.restart()
|
|
|
162 |
else:
|
|
|
163 |
print "Unknown command"
|
|
|
164 |
sys.exit(2)
|
|
|
165 |
sys.exit(0)
|
|
|
166 |
else:
|
|
|
167 |
print "usage: %s start|stop|restart" % sys.argv[0]
|
|
|
168 |
sys.exit(2)
|