Subversion Repositories SmartDukaan

Rev

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

from shop2020.thriftpy.utils.ttypes import Mail
from shop2020.clients.HelperClient import HelperClient

from email.mime.text import MIMEText
import sched, time, smtplib

class EmailSender():
    def __init__(self):
        print 'EmailSender initiated at', time.time()
        self.email_schedular = sched.scheduler(time.time, time.sleep)
        self.time_to_sleep = 2
        self.authenticate()

    def authenticate(self):
        try:
            print "Attempting authentication at", time.time()
            self.mailServer = smtplib.SMTP("smtp.sendgrid.net", 587)
            
            self.sender = "shop2020mail"
            self.password = "shop2020"
            self.mailServer.ehlo()
            self.mailServer.starttls()
            self.mailServer.ehlo()
            self.mailServer.login(self.sender, self.password)
            print "Authentication successful", time.time()
            
        except smtplib.SMTPConnectError as e:
            print 'Specified host did not respond correctly', e
        
        except smtplib.SMTPHeloError as e:
            print 'The server did not reply properly to the HELO greeting.', e
        
        except smtplib.SMTPAuthenticationError as e:
            print 'The server did not accept the username/password combination.', e
        
        except smtplib.SMTPException as e:
            print e
            
    def sendMail(self, mail):
        if mail.sender:
            mail.data = "This mail is sent by " + mail.sender + "\n" + mail.data
        
        msg = MIMEText(mail.data, 'html')
        
        msg['To'] = mail.to
        
        if mail.sender:
            msg['From'] = mail.sender
        else:
            msg['From'] = "help@saholic.com"
        msg['Subject'] = mail.subject
        
        try:
            rs = self.mailServer.sendmail(self.password, [mail.to], msg.as_string())
            
        except smtplib.SMTPServerDisconnected as e:
            print 'Server Disconnected at', time.time()
            self.authenticate()
            rs = self.mailServer.sendmail(self.password, [mail.to], msg.as_string())
        
        print msg['To']
        # Should be mailServer.quit(), but that crashes...
        return rs

    def send(self):
        print "Despatch stared at ", time.time()
        helper_client = HelperClient().get_client()
        
        emails_to_be_dispatched = helper_client.getEmailsToBeSent()
        print len(emails_to_be_dispatched)
        
        for email in emails_to_be_dispatched:
            mail = Mail(email.emailTo, email.subject, email.body, None, None, None)
            
            try:
                send_result = self.sendMail(mail)
                
                if len(send_result) == 0:
                    print "Sent at", time.time()
                    helper_client.markEmailAsSent(email.id)
                else:
                    print "Failed sending email. Id:", " Time:", time.time()
            except Exception:
                print 'Error occurred sending emails. Will retry.'
            
        self.email_schedular.enter(self.time_to_sleep, 1, self.send, ())

    def start(self):
        self.email_schedular.enter(self.time_to_sleep, 1, self.send, ())
        self.email_schedular.run()

EmailSender().start()