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