##// END OF EJS Templates
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
marcink -
r981:c45c9107 beta
parent child Browse files
Show More
@@ -1,6 +1,20 b''
1 # -*- coding: utf-8 -*-
2 """
3 rhodecode.lib.smtp_mailer
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 Simple smtp mailer used in RhodeCode
7
8 :created_on: Sep 13, 2011
9 :copyright: (c) 2011 by marcink.
10 :license: LICENSE_NAME, see LICENSE_FILE for more details.
11 """
12
1 import logging
13 import logging
2 import smtplib
14 import smtplib
3 import mimetypes
15 import mimetypes
16 from socket import sslerror
17
4 from email.mime.multipart import MIMEMultipart
18 from email.mime.multipart import MIMEMultipart
5 from email.mime.image import MIMEImage
19 from email.mime.image import MIMEImage
6 from email.mime.audio import MIMEAudio
20 from email.mime.audio import MIMEAudio
@@ -10,14 +24,15 b' from email.utils import formatdate'
10 from email import encoders
24 from email import encoders
11
25
12 class SmtpMailer(object):
26 class SmtpMailer(object):
13 """simple smtp mailer class
27 """SMTP mailer class
14
28
15 mailer = SmtpMailer(mail_from, user, passwd, mail_server, mail_port, ssl, tls)
29 mailer = SmtpMailer(mail_from, user, passwd, mail_server, mail_port, ssl, tls)
16 mailer.send(recipients, subject, body, attachment_files)
30 mailer.send(recipients, subject, body, attachment_files)
17
31
18 :param recipients might be a list of string or single string
32 :param recipients might be a list of string or single string
19 :param attachment_files is a dict of {filename:location}
33 :param attachment_files is a dict of {filename:location}
20 it tries to guess the mimetype and attach the file
34 it tries to guess the mimetype and attach the file
35
21 """
36 """
22
37
23 def __init__(self, mail_from, user, passwd, mail_server,
38 def __init__(self, mail_from, user, passwd, mail_server,
@@ -42,6 +57,7 b' class SmtpMailer(object):'
42 smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port)
57 smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port)
43
58
44 if self.tls:
59 if self.tls:
60 smtp_serv.ehlo()
45 smtp_serv.starttls()
61 smtp_serv.starttls()
46
62
47 if self.debug:
63 if self.debug:
@@ -50,7 +66,10 b' class SmtpMailer(object):'
50 smtp_serv.ehlo()
66 smtp_serv.ehlo()
51
67
52 #if server requires authorization you must provide login and password
68 #if server requires authorization you must provide login and password
53 smtp_serv.login(self.user, self.passwd)
69 #but only if we have them
70 if self.user and self.passwd:
71 smtp_serv.login(self.user, self.passwd)
72
54
73
55 date_ = formatdate(localtime=True)
74 date_ = formatdate(localtime=True)
56 msg = MIMEMultipart()
75 msg = MIMEMultipart()
@@ -67,7 +86,13 b' class SmtpMailer(object):'
67
86
68 smtp_serv.sendmail(self.mail_from, recipients, msg.as_string())
87 smtp_serv.sendmail(self.mail_from, recipients, msg.as_string())
69 logging.info('MAIL SEND TO: %s' % recipients)
88 logging.info('MAIL SEND TO: %s' % recipients)
70 smtp_serv.quit()
89
90 try:
91 smtp_serv.quit()
92 except sslerror:
93 # sslerror is raised in tls connections on closing sometimes
94 pass
95
71
96
72
97
73 def __atach_files(self, msg, attachment_files):
98 def __atach_files(self, msg, attachment_files):
General Comments 0
You need to be logged in to leave comments. Login now