Subversion Repositories SmartDukaan

Rev

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