Show More
@@ -1,119 +1,120 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2013-2018 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 | from socket import sslerror | |
|
27 | import socket | |
|
28 | 28 | from email.utils import formatdate |
|
29 | ||
|
29 | 30 | from rhodecode.lib.rcmail.message import Message |
|
30 | 31 | from rhodecode.lib.rcmail.utils import DNS_NAME |
|
31 | 32 | |
|
32 | 33 | log = logging.getLogger(__name__) |
|
33 | 34 | |
|
34 | 35 | |
|
35 | 36 | class SmtpMailer(object): |
|
36 | 37 | """ |
|
37 | 38 | SMTP mailer class |
|
38 | 39 | |
|
39 | 40 | mailer = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth |
|
40 | 41 | mail_port, ssl, tls) |
|
41 | 42 | mailer.send(recipients, subject, body, attachment_files) |
|
42 | 43 | |
|
43 | 44 | :param recipients might be a list of string or single string |
|
44 | 45 | :param attachment_files is a dict of {filename:location} |
|
45 | 46 | it tries to guess the mimetype and attach the file |
|
46 | 47 | |
|
47 | 48 | """ |
|
48 | 49 | |
|
49 | 50 | def __init__(self, mail_from, user, passwd, mail_server, smtp_auth=None, |
|
50 | 51 | mail_port=None, ssl=False, tls=False, debug=False): |
|
51 | 52 | |
|
52 | 53 | self.mail_from = mail_from |
|
53 | 54 | self.mail_server = mail_server |
|
54 | 55 | self.mail_port = mail_port |
|
55 | 56 | self.user = user |
|
56 | 57 | self.passwd = passwd |
|
57 | 58 | self.ssl = ssl |
|
58 | 59 | self.tls = tls |
|
59 | 60 | self.debug = debug |
|
60 | 61 | self.auth = smtp_auth |
|
61 | 62 | |
|
62 | 63 | def _get_smptlib(self): |
|
63 | #patch the output | |
|
64 | # patch the output | |
|
64 | 65 | import smtplib |
|
65 | 66 | |
|
66 | 67 | class StderrLogger(object): |
|
67 | 68 | |
|
68 | 69 | def write(self, message): |
|
69 | 70 | log.debug(message) |
|
70 | 71 | |
|
71 | 72 | org_stderr = smtplib.stderr |
|
72 | 73 | smtplib.stderr = StderrLogger() |
|
73 | 74 | return smtplib |
|
74 | 75 | |
|
75 | 76 | def send(self, recipients=None, subject='', body='', html='', |
|
76 | 77 | attachment_files=None): |
|
77 | 78 | recipients = recipients or [] |
|
78 | 79 | if isinstance(recipients, basestring): |
|
79 | 80 | recipients = [recipients] |
|
80 | 81 | headers = { |
|
81 | 82 | 'Date': formatdate(time.time()) |
|
82 | 83 | } |
|
83 | 84 | msg = Message(subject, recipients, body, html, self.mail_from, |
|
84 | 85 | recipients_separator=", ", extra_headers=headers) |
|
85 | 86 | raw_msg = msg.to_message() |
|
86 | 87 | |
|
87 | 88 | # patched smtplib without stderr |
|
88 | 89 | smtplib = self._get_smptlib() |
|
89 | 90 | if self.ssl: |
|
90 | 91 | smtp_serv = smtplib.SMTP_SSL(self.mail_server, self.mail_port, |
|
91 | 92 | local_hostname=DNS_NAME.get_fqdn()) |
|
92 | 93 | else: |
|
93 | 94 | smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port, |
|
94 | 95 | local_hostname=DNS_NAME.get_fqdn()) |
|
95 | 96 | |
|
96 | 97 | if self.tls: |
|
97 | 98 | smtp_serv.ehlo() |
|
98 | 99 | smtp_serv.starttls() |
|
99 | 100 | |
|
100 | 101 | if self.debug: |
|
101 | 102 | smtp_serv.set_debuglevel(1) |
|
102 | 103 | |
|
103 | 104 | smtp_serv.ehlo() |
|
104 | 105 | if self.auth: |
|
105 | 106 | smtp_serv.esmtp_features["auth"] = self.auth |
|
106 | 107 | |
|
107 | 108 | # if server requires authorization you must provide login and password |
|
108 | 109 | # but only if we have them |
|
109 | 110 | if self.user and self.passwd: |
|
110 | 111 | smtp_serv.login(self.user, self.passwd) |
|
111 | 112 | |
|
112 | 113 | smtp_serv.sendmail(msg.sender, msg.send_to, raw_msg.as_string()) |
|
113 | 114 | log.info('email sent to: %s' % recipients) |
|
114 | 115 | |
|
115 | 116 | try: |
|
116 | 117 | smtp_serv.quit() |
|
117 | except sslerror: | |
|
118 | except socket.sslerror: | |
|
118 | 119 | # sslerror is raised in tls connections on closing sometimes |
|
119 | 120 | smtp_serv.close() |
General Comments 0
You need to be logged in to leave comments.
Login now