Subversion Repositories SmartDukaan

Rev

Rev 5828 | Rev 5924 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5828 Rev 5864
Line 5... Line 5...
5
import optparse
5
import optparse
6
import sched
6
import sched
7
import smtplib
7
import smtplib
8
import sys
8
import sys
9
import time
9
import time
-
 
10
from email.mime.multipart import MIMEMultipart
10
 
11
 
11
 
12
 
12
 
13
 
13
class EmailSender(Daemon):
14
class EmailSender(Daemon):
14
    def __init__(self, logfile='/var/log/utils/emailsender/emailsender.log', pidfile='/var/run/email-sender.pid'):
15
    def __init__(self, logfile='/var/log/utils/emailsender/emailsender.log', pidfile='/var/run/email-sender.pid'):
Line 49... Line 50...
49
        except Exception as e:
50
        except Exception as e:
50
            print sys.exc_info()[0]
51
            print sys.exc_info()[0]
51
            print e
52
            print e
52
            
53
            
53
    def sendMail(self, mail):
54
    def sendMail(self, mail):
54
        if mail.sender:
55
        msg = MIMEMultipart()
55
            mail.data = "This mail is sent by " + mail.sender + "\n" + mail.data
-
 
56
        
56
    
57
        msg = MIMEText(mail.data, 'html')
57
        msg['From'] = "help@saholic.com"
58
        
-
 
59
        msg['To'] = ';'.join(mail.to)
58
        msg['To'] = ', '.join(mail.emailTo)
60
        
-
 
61
        if mail.sender:
-
 
62
            msg['From'] = mail.sender
59
        msg['Cc'] = ', '.join(mail.cc)
63
        else:
-
 
64
            msg['From'] = "help@saholic.com"
60
        #msg['Bcc'] = ', '.join(mail.bcc)
65
        msg['Subject'] = mail.subject
61
        msg['Subject'] = mail.subject
66
        
62
        
-
 
63
        html_msg = MIMEText(mail.body, 'html')
-
 
64
        msg.attach(html_msg)
-
 
65
        
67
        try:
66
        try:
68
            self.setMailServerAndPassword(mail)
67
            self.setMailServerAndPassword(mail)
69
            rs = self.mailServer.sendmail(self.password, mail.to, msg.as_string())
68
            rs = self.mailServer.sendmail(self.password, mail.emailTo + mail.cc + mail.bcc, msg.as_string())
70
        except smtplib.SMTPServerDisconnected as e:
69
        except smtplib.SMTPServerDisconnected as e:
71
            print 'Server Disconnected at', time.time()
70
            print 'Server Disconnected at', time.time()
72
            self.authenticate()
71
            self.authenticate()
73
            rs = self.mailServer.sendmail(self.password, mail.to, msg.as_string())
72
            rs = self.mailServer.sendmail(self.password, mail.to, msg.as_string())
74
        
73
        
Line 95... Line 94...
95
        
94
        
96
        emails_to_be_dispatched = helper_client.getEmailsToBeSent()
95
        emails_to_be_dispatched = helper_client.getEmailsToBeSent()
97
        print len(emails_to_be_dispatched)
96
        print len(emails_to_be_dispatched)
98
        
97
        
99
        for email in emails_to_be_dispatched:
98
        for email in emails_to_be_dispatched:
100
            toList = []
-
 
101
            for to in email.emailTo.split(';') :
-
 
102
                if to.strip() :
-
 
103
                    toList.append(to.strip())
-
 
104
            mail = Mail( toList, email.subject, email.body, None, None, None)
-
 
105
            
-
 
106
            try:
99
            try:
107
                send_result = self.sendMail(mail)
100
                send_result = self.sendMail(email)
108
            except Exception as e:
101
            except Exception as e:
109
                print sys.exc_info()[0]
102
                print sys.exc_info()[0]
110
                print e
103
                print e
111
                print 'Error occurred sending emails. Will retry.'
104
                print 'Error occurred sending emails. Will retry.'
112
                continue
105
                continue