Subversion Repositories SmartDukaan

Rev

Rev 4795 | Rev 4909 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
4795 mandeep.dh 4
import sched
5
import time
6
import smtplib
7
import sys
1425 varun.gupt 8
 
9
 
10
class EmailSender():
11
    def __init__(self):
3425 varun.gupt 12
        print 'EmailSender initiated at', time.time()
1425 varun.gupt 13
        self.email_schedular = sched.scheduler(time.time, time.sleep)
3425 varun.gupt 14
        self.time_to_sleep = 2
15
        self.authenticate()
16
 
17
    def authenticate(self):
3400 varun.gupt 18
        try:
3425 varun.gupt 19
            print "Attempting authentication at", time.time()
3987 mandeep.dh 20
            self.mailServer = smtplib.SMTP("smtp.sendgrid.net", 587)
3400 varun.gupt 21
 
3987 mandeep.dh 22
            self.sender = "shop2020mail"
23
            self.password = "shop2020"
3400 varun.gupt 24
            self.mailServer.ehlo()
25
            self.mailServer.starttls()
26
            self.mailServer.ehlo()
27
            self.mailServer.login(self.sender, self.password)
3425 varun.gupt 28
            print "Authentication successful", time.time()
3400 varun.gupt 29
 
30
        except smtplib.SMTPConnectError as e:
4795 mandeep.dh 31
            print 'Specified host did not respond correctly', e        
3400 varun.gupt 32
        except smtplib.SMTPHeloError as e:
33
            print 'The server did not reply properly to the HELO greeting.', e
34
        except smtplib.SMTPAuthenticationError as e:
35
            print 'The server did not accept the username/password combination.', e
36
        except smtplib.SMTPException as e:
37
            print e
4795 mandeep.dh 38
        except Exception as e:
39
            print sys.exc_info()[0]
40
            print e
3425 varun.gupt 41
 
1425 varun.gupt 42
    def sendMail(self, mail):
43
        if mail.sender:
44
            mail.data = "This mail is sent by " + mail.sender + "\n" + mail.data
45
 
1560 varun.gupt 46
        msg = MIMEText(mail.data, 'html')
1425 varun.gupt 47
 
1566 varun.gupt 48
        msg['To'] = mail.to
49
 
1425 varun.gupt 50
        if mail.sender:
51
            msg['From'] = mail.sender
52
        else:
53
            msg['From'] = "help@saholic.com"
54
        msg['Subject'] = mail.subject
55
 
3425 varun.gupt 56
        try:
57
            rs = self.mailServer.sendmail(self.password, [mail.to], msg.as_string())
58
 
59
        except smtplib.SMTPServerDisconnected as e:
60
            print 'Server Disconnected at', time.time()
61
            self.authenticate()
62
            rs = self.mailServer.sendmail(self.password, [mail.to], msg.as_string())
63
 
64
        print msg['To']
1425 varun.gupt 65
        # Should be mailServer.quit(), but that crashes...
66
        return rs
67
 
68
    def send(self):
69
        print "Despatch stared at ", time.time()
70
        helper_client = HelperClient().get_client()
71
 
3086 rajveer 72
        emails_to_be_dispatched = helper_client.getEmailsToBeSent()
1425 varun.gupt 73
        print len(emails_to_be_dispatched)
74
 
75
        for email in emails_to_be_dispatched:
76
            mail = Mail(email.emailTo, email.subject, email.body, None, None, None)
77
 
3977 varun.gupt 78
            try:
79
                send_result = self.sendMail(mail)
80
 
81
                if len(send_result) == 0:
82
                    print "Sent at", time.time()
83
                    helper_client.markEmailAsSent(email.id)
84
                else:
85
                    print "Failed sending email. Id:", " Time:", time.time()
4796 mandeep.dh 86
            except Exception as e:
87
                print sys.exc_info()[0]
88
                print e
3977 varun.gupt 89
                print 'Error occurred sending emails. Will retry.'
90
 
1425 varun.gupt 91
        self.email_schedular.enter(self.time_to_sleep, 1, self.send, ())
92
 
93
    def start(self):
94
        self.email_schedular.enter(self.time_to_sleep, 1, self.send, ())
95
        self.email_schedular.run()
96
 
97
EmailSender().start()