Subversion Repositories SmartDukaan

Rev

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

Rev 3400 Rev 3425
Line 4... Line 4...
4
from email.mime.text import MIMEText
4
from email.mime.text import MIMEText
5
import sched, time, smtplib
5
import sched, time, smtplib
6
 
6
 
7
class EmailSender():
7
class EmailSender():
8
    def __init__(self):
8
    def __init__(self):
-
 
9
        print 'EmailSender initiated at', time.time()
9
        self.email_schedular = sched.scheduler(time.time, time.sleep)
10
        self.email_schedular = sched.scheduler(time.time, time.sleep)
10
        self.time_to_sleep = 20
11
        self.time_to_sleep = 2
11
        
12
        self.authenticate()
-
 
13
 
-
 
14
    def authenticate(self):
12
        try:
15
        try:
-
 
16
            print "Attempting authentication at", time.time()
13
            self.mailServer = smtplib.SMTP("smtp.gmail.com", 587)
17
            self.mailServer = smtplib.SMTP("smtp.gmail.com", 587)
14
            
18
            
15
            self.sender = "help@shop2020.in"
19
            self.sender = "help@shop2020.in"
16
            self.password = "5h0p2o2o"
20
            self.password = "5h0p2o2o"
17
            self.mailServer.ehlo()
21
            self.mailServer.ehlo()
18
            self.mailServer.starttls()
22
            self.mailServer.starttls()
19
            self.mailServer.ehlo()
23
            self.mailServer.ehlo()
20
            self.mailServer.login(self.sender, self.password)
24
            self.mailServer.login(self.sender, self.password)
21
 
-
 
22
            print "EmailSender initiated at ", time.time()
25
            print "Authentication successful", time.time()
23
            
26
            
24
        except smtplib.SMTPConnectError as e:
27
        except smtplib.SMTPConnectError as e:
25
            print 'Specified host did not respond correctly', e
28
            print 'Specified host did not respond correctly', e
26
        
29
        
27
        except smtplib.SMTPHeloError as e:
30
        except smtplib.SMTPHeloError as e:
Line 30... Line 33...
30
        except smtplib.SMTPAuthenticationError as e:
33
        except smtplib.SMTPAuthenticationError as e:
31
            print 'The server did not accept the username/password combination.', e
34
            print 'The server did not accept the username/password combination.', e
32
        
35
        
33
        except smtplib.SMTPException as e:
36
        except smtplib.SMTPException as e:
34
            print e
37
            print e
35
    
38
            
36
    def sendMail(self, mail):
39
    def sendMail(self, mail):
37
        if mail.sender:
40
        if mail.sender:
38
            mail.data = "This mail is sent by " + mail.sender + "\n" + mail.data
41
            mail.data = "This mail is sent by " + mail.sender + "\n" + mail.data
39
        
42
        
40
        msg = MIMEText(mail.data, 'html')
43
        msg = MIMEText(mail.data, 'html')
41
        
44
        
42
        msg['To'] = mail.to
45
        msg['To'] = mail.to
43
        print msg['To']
-
 
44
        
46
        
45
        if mail.sender:
47
        if mail.sender:
46
            msg['From'] = mail.sender
48
            msg['From'] = mail.sender
47
        else:
49
        else:
48
            msg['From'] = "help@saholic.com"
50
            msg['From'] = "help@saholic.com"
49
        msg['Subject'] = mail.subject
51
        msg['Subject'] = mail.subject
50
        
52
        
-
 
53
        try:
51
        rs = self.mailServer.sendmail(self.password, [mail.to], msg.as_string())
54
            rs = self.mailServer.sendmail(self.password, [mail.to], msg.as_string())
-
 
55
            
-
 
56
        except smtplib.SMTPServerDisconnected as e:
-
 
57
            print 'Server Disconnected at', time.time()
-
 
58
            self.authenticate()
-
 
59
            rs = self.mailServer.sendmail(self.password, [mail.to], msg.as_string())
-
 
60
        
-
 
61
        print msg['To']
52
        # Should be mailServer.quit(), but that crashes...
62
        # Should be mailServer.quit(), but that crashes...
53
        return rs
63
        return rs
54
 
64
 
55
    def send(self):
65
    def send(self):
56
        print "Despatch stared at ", time.time()
66
        print "Despatch stared at ", time.time()