Subversion Repositories SmartDukaan

Rev

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

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