Rev 1560 | Rev 3400 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
from shop2020.thriftpy.utils.ttypes import Mailfrom shop2020.clients.HelperClient import HelperClientfrom email.mime.text import MIMETextimport sched, time, smtplibclass EmailSender():def __init__(self):self.email_schedular = sched.scheduler(time.time, time.sleep)self.time_to_sleep = 20print "EmailSender initiated at ", time.time()def sendMail(self, mail):if mail.sender:mail.data = "This mail is sent by " + mail.sender + "\n" + mail.datamsg = MIMEText(mail.data, 'html')msg['To'] = mail.toprint msg['To']if mail.sender:msg['From'] = mail.senderelse:msg['From'] = "help@saholic.com"msg['Subject'] = mail.subjectmail.sender = "help@shop2020.in"mail.password = "5h0p2o2o"mailServer = smtplib.SMTP("smtp.gmail.com", 587)mailServer.ehlo()mailServer.starttls()mailServer.ehlo()mailServer.login(mail.sender, mail.password)rs = mailServer.sendmail(mail.password, [mail.to], msg.as_string())# Should be mailServer.quit(), but that crashes...mailServer.close()return rsdef send(self):print "Despatch stared at ", time.time()helper_client = HelperClient().get_client()emails_to_be_dispatched = helper_client.getEmailsToBeSent("TransactionInfo")print len(emails_to_be_dispatched)for email in emails_to_be_dispatched:mail = Mail(email.emailTo, email.subject, email.body, None, None, None)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()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()