Subversion Repositories SmartDukaan

Rev

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

Rev 6016 Rev 6906
Line 1... Line 1...
1
from email.mime.text import MIMEText
1
from email.mime.text import MIMEText
2
from shop2020.clients.HelperClient import HelperClient
2
from shop2020.clients.HelperClient import HelperClient
-
 
3
from shop2020.clients.TransactionClient import TransactionClient
3
from shop2020.thriftpy.utils.ttypes import Mail
4
from shop2020.thriftpy.utils.ttypes import Mail
4
from shop2020.utils.daemon import Daemon
5
from shop2020.utils.daemon import Daemon
5
import optparse
6
import optparse
6
import sched
7
import sched
7
import smtplib
8
import smtplib
8
import sys
9
import sys
9
import time
10
import time
-
 
11
import os
10
from email.mime.multipart import MIMEMultipart
12
from email.MIMEMultipart import MIMEMultipart
-
 
13
from email.MIMEBase import MIMEBase
-
 
14
from email.MIMEText import MIMEText
-
 
15
from email import Encoders
11
 
16
 
12
 
17
 
13
 
18
 
14
class EmailSender(Daemon):
19
class EmailSender(Daemon):
15
    def __init__(self, logfile='/var/log/utils/emailsender/emailsender.log', pidfile='/var/run/email-sender.pid'):
20
    def __init__(self, logfile='/var/log/utils/emailsender/emailsender.log', pidfile='/var/run/email-sender.pid'):
Line 49... Line 54...
49
            print e
54
            print e
50
        except Exception as e:
55
        except Exception as e:
51
            print sys.exc_info()[0]
56
            print sys.exc_info()[0]
52
            print e
57
            print e
53
            
58
            
-
 
59
    def get_attachment_part(self, document, filename):
-
 
60
        part = MIMEBase('application', 'octet-stream')
-
 
61
        part.set_payload(document)
-
 
62
        Encoders.encode_base64(part)
-
 
63
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % filename + "-policy.pdf")
-
 
64
        return part
-
 
65
 
54
    def sendMail(self, mail):
66
    def sendMail(self, mail):
55
        msg = MIMEMultipart()
67
        msg = MIMEMultipart()
56
    
68
    
57
        msg['From'] = "help@saholic.com"
69
        msg['From'] = "help@saholic.com"
58
        msg['To'] = ', '.join(mail.emailTo)
70
        msg['To'] = ', '.join(mail.emailTo)
Line 60... Line 72...
60
        #msg['Bcc'] = ', '.join(mail.bcc)
72
        #msg['Bcc'] = ', '.join(mail.bcc)
61
        msg['Subject'] = mail.subject
73
        msg['Subject'] = mail.subject
62
        
74
        
63
        html_msg = MIMEText(mail.body, 'html')
75
        html_msg = MIMEText(mail.body, 'html')
64
        msg.attach(html_msg)
76
        msg.attach(html_msg)
-
 
77
 
-
 
78
        if mail.emailType == "DeliverySuccess":
-
 
79
            tclient = TransactionClient().get_client()
-
 
80
            document = tclient.getDocument(1, int(mail.source))
-
 
81
            if document:
-
 
82
                msg.attach(self.get_attachment_part(document, mail.source))
65
        
83
            
66
        try:
84
        try:
67
            self.setMailServerAndPassword(mail)
85
            self.setMailServerAndPassword(mail)
68
            rs = self.mailServer.sendmail(self.password, mail.emailTo + mail.cc + mail.bcc, msg.as_string())
86
            rs = self.mailServer.sendmail(self.password, mail.emailTo + mail.cc + mail.bcc, msg.as_string())
69
        except smtplib.SMTPServerDisconnected as e:
87
        except smtplib.SMTPServerDisconnected as e:
70
            print 'Server Disconnected at', time.time()
88
            print 'Server Disconnected at', time.time()