comparison rhodecode/lib/smtp_mailer.py @ 1639:95c3e33ef32e

merged upto rev 019026a8cf67
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 02 Nov 2011 22:19:47 +0200
parents 752b0a7b7679 8e77c75bd65a
children
comparison
equal deleted inserted replaced
1637:974d6193b61a 1639:95c3e33ef32e
37 37
38 38
39 class SmtpMailer(object): 39 class SmtpMailer(object):
40 """SMTP mailer class 40 """SMTP mailer class
41 41
42 mailer = SmtpMailer(mail_from, user, passwd, mail_server, 42 mailer = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth
43 mail_port, ssl, tls) 43 mail_port, ssl, tls)
44 mailer.send(recipients, subject, body, attachment_files) 44 mailer.send(recipients, subject, body, attachment_files)
45 45
46 :param recipients might be a list of string or single string 46 :param recipients might be a list of string or single string
47 :param attachment_files is a dict of {filename:location} 47 :param attachment_files is a dict of {filename:location}
48 it tries to guess the mimetype and attach the file 48 it tries to guess the mimetype and attach the file
49 49
50 """ 50 """
51 51
52 def __init__(self, mail_from, user, passwd, mail_server, 52 def __init__(self, mail_from, user, passwd, mail_server, smtp_auth=None,
53 mail_port=None, ssl=False, tls=False, debug=False): 53 mail_port=None, ssl=False, tls=False, debug=False):
54 54
55 self.mail_from = mail_from 55 self.mail_from = mail_from
56 self.mail_server = mail_server 56 self.mail_server = mail_server
57 self.mail_port = mail_port 57 self.mail_port = mail_port
58 self.user = user 58 self.user = user
59 self.passwd = passwd 59 self.passwd = passwd
60 self.ssl = ssl 60 self.ssl = ssl
61 self.tls = tls 61 self.tls = tls
62 self.debug = debug 62 self.debug = debug
63 self.auth = smtp_auth
63 64
64 def send(self, recipients=[], subject='', body='', attachment_files=None): 65 def send(self, recipients=[], subject='', body='', attachment_files=None):
65 66
66 if isinstance(recipients, basestring): 67 if isinstance(recipients, basestring):
67 recipients = [recipients] 68 recipients = [recipients]
76 77
77 if self.debug: 78 if self.debug:
78 smtp_serv.set_debuglevel(1) 79 smtp_serv.set_debuglevel(1)
79 80
80 smtp_serv.ehlo() 81 smtp_serv.ehlo()
82 if self.auth:
83 smtp_serv.esmtp_features["auth"] = self.auth
81 84
82 #if server requires authorization you must provide login and password 85 # if server requires authorization you must provide login and password
83 #but only if we have them 86 # but only if we have them
84 if self.user and self.passwd: 87 if self.user and self.passwd:
85 smtp_serv.login(self.user, self.passwd) 88 smtp_serv.login(self.user, self.passwd)
86 89
87 date_ = formatdate(localtime=True) 90 date_ = formatdate(localtime=True)
88 msg = MIMEMultipart() 91 msg = MIMEMultipart()
154 :param msg_file: 157 :param msg_file:
155 """ 158 """
156 if isinstance(msg_file, str): 159 if isinstance(msg_file, str):
157 return open(msg_file, "rb").read() 160 return open(msg_file, "rb").read()
158 else: 161 else:
159 #just for safe seek to 0 162 # just for safe seek to 0
160 msg_file.seek(0) 163 msg_file.seek(0)
161 return msg_file.read() 164 return msg_file.read()
165