##// END OF EJS Templates
mailer: don't use a mutable default argument
marcink -
r1408:dc932537 default
parent child Browse files
Show More
@@ -1,118 +1,118 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2013-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 Simple smtp mailer used in RhodeCode
23 23 """
24 24
25 25 import time
26 26 import logging
27 27 from socket import sslerror
28 28 from email.utils import formatdate
29 29 from rhodecode.lib.rcmail.message import Message
30 30 from rhodecode.lib.rcmail.utils import DNS_NAME
31 31
32 32 log = logging.getLogger(__name__)
33 33
34 34
35 35 class SmtpMailer(object):
36 36 """SMTP mailer class
37 37
38 38 mailer = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth
39 39 mail_port, ssl, tls)
40 40 mailer.send(recipients, subject, body, attachment_files)
41 41
42 42 :param recipients might be a list of string or single string
43 43 :param attachment_files is a dict of {filename:location}
44 44 it tries to guess the mimetype and attach the file
45 45
46 46 """
47 47
48 48 def __init__(self, mail_from, user, passwd, mail_server, smtp_auth=None,
49 49 mail_port=None, ssl=False, tls=False, debug=False):
50 50
51 51 self.mail_from = mail_from
52 52 self.mail_server = mail_server
53 53 self.mail_port = mail_port
54 54 self.user = user
55 55 self.passwd = passwd
56 56 self.ssl = ssl
57 57 self.tls = tls
58 58 self.debug = debug
59 59 self.auth = smtp_auth
60 60
61 61 def _get_smptlib(self):
62 62 #patch the output
63 63 import smtplib
64 64
65 65 class StderrLogger(object):
66 66
67 67 def write(self, message):
68 68 log.debug(message)
69 69
70 70 org_stderr = smtplib.stderr
71 71 smtplib.stderr = StderrLogger()
72 72 return smtplib
73 73
74 def send(self, recipients=[], subject='', body='', html='',
74 def send(self, recipients=None, subject='', body='', html='',
75 75 attachment_files=None):
76
76 recipients = recipients or []
77 77 if isinstance(recipients, basestring):
78 78 recipients = [recipients]
79 79 headers = {
80 80 'Date': formatdate(time.time())
81 81 }
82 82 msg = Message(subject, recipients, body, html, self.mail_from,
83 83 recipients_separator=", ", extra_headers=headers)
84 84 raw_msg = msg.to_message()
85 85
86 86 #patched smtplib without stderr
87 87 smtplib = self._get_smptlib()
88 88 if self.ssl:
89 89 smtp_serv = smtplib.SMTP_SSL(self.mail_server, self.mail_port,
90 90 local_hostname=DNS_NAME.get_fqdn())
91 91 else:
92 92 smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port,
93 93 local_hostname=DNS_NAME.get_fqdn())
94 94
95 95 if self.tls:
96 96 smtp_serv.ehlo()
97 97 smtp_serv.starttls()
98 98
99 99 if self.debug:
100 100 smtp_serv.set_debuglevel(1)
101 101
102 102 smtp_serv.ehlo()
103 103 if self.auth:
104 104 smtp_serv.esmtp_features["auth"] = self.auth
105 105
106 106 # if server requires authorization you must provide login and password
107 107 # but only if we have them
108 108 if self.user and self.passwd:
109 109 smtp_serv.login(self.user, self.passwd)
110 110
111 111 smtp_serv.sendmail(msg.sender, msg.send_to, raw_msg.as_string())
112 112 logging.info('MAIL SEND TO: %s' % recipients)
113 113
114 114 try:
115 115 smtp_serv.quit()
116 116 except sslerror:
117 117 # sslerror is raised in tls connections on closing sometimes
118 118 smtp_serv.close()
General Comments 0
You need to be logged in to leave comments. Login now