/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/smtp_connection.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-19 13:16:11 UTC
  • mto: (6968.4.3 git-archive)
  • mto: This revision was merged to the branch mainline in revision 6972.
  • Revision ID: jelmer@jelmer.uk-20180519131611-l9h9ud41j7qg1m03
Move tar/zip to breezy.archive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""A convenience class around smtplib."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
try:
 
22
    from email.utils import getaddresses, parseaddr
 
23
except ImportError:  # python < 3
 
24
    from email.Utils import getaddresses, parseaddr
 
25
 
 
26
import errno
 
27
import smtplib
 
28
import socket
 
29
 
 
30
from . import (
 
31
    config,
 
32
    osutils,
 
33
    )
 
34
from .errors import (
 
35
    BzrError,
 
36
    InternalBzrError,
 
37
    )
 
38
 
 
39
 
 
40
smtp_password = config.Option('smtp_password', default=None,
 
41
        help='''\
 
42
Password to use for authentication to SMTP server.
 
43
''')
 
44
smtp_server = config.Option('smtp_server', default=None,
 
45
        help='''\
 
46
Hostname of the SMTP server to use for sending email.
 
47
''')
 
48
smtp_username = config.Option('smtp_username', default=None,
 
49
        help='''\
 
50
Username to use for authentication to SMTP server.
 
51
''')
 
52
 
 
53
 
 
54
class SMTPError(BzrError):
 
55
 
 
56
    _fmt = "SMTP error: %(error)s"
 
57
 
 
58
    def __init__(self, error):
 
59
        self.error = error
 
60
 
 
61
 
 
62
class SMTPConnectionRefused(SMTPError):
 
63
 
 
64
    _fmt = "SMTP connection to %(host)s refused"
 
65
 
 
66
    def __init__(self, error, host):
 
67
        self.error = error
 
68
        self.host = host
 
69
 
 
70
 
 
71
class DefaultSMTPConnectionRefused(SMTPConnectionRefused):
 
72
 
 
73
    _fmt = "Please specify smtp_server.  No server at default %(host)s."
 
74
 
 
75
 
 
76
 
 
77
class NoDestinationAddress(InternalBzrError):
 
78
 
 
79
    _fmt = "Message does not have a destination address."
 
80
 
 
81
 
 
82
 
 
83
class SMTPConnection(object):
 
84
    """Connect to an SMTP server and send an email.
 
85
 
 
86
    This is a gateway between breezy.config.Config and smtplib.SMTP. It
 
87
    understands the basic bzr SMTP configuration information: smtp_server,
 
88
    smtp_username, and smtp_password.
 
89
    """
 
90
 
 
91
    _default_smtp_server = 'localhost'
 
92
 
 
93
    def __init__(self, config, _smtp_factory=None):
 
94
        self._smtp_factory = _smtp_factory
 
95
        if self._smtp_factory is None:
 
96
            self._smtp_factory = smtplib.SMTP
 
97
        self._config = config
 
98
        self._config_smtp_server = config.get('smtp_server')
 
99
        self._smtp_server = self._config_smtp_server
 
100
        if self._smtp_server is None:
 
101
            self._smtp_server = self._default_smtp_server
 
102
 
 
103
        self._smtp_username = config.get('smtp_username')
 
104
        self._smtp_password = config.get('smtp_password')
 
105
 
 
106
        self._connection = None
 
107
 
 
108
    def _connect(self):
 
109
        """If we haven't connected, connect and authenticate."""
 
110
        if self._connection is not None:
 
111
            return
 
112
 
 
113
        self._create_connection()
 
114
        # FIXME: _authenticate() should only be called when the server has
 
115
        # refused unauthenticated access, so it can safely try to authenticate 
 
116
        # with the default username. JRV20090407
 
117
        self._authenticate()
 
118
 
 
119
    def _create_connection(self):
 
120
        """Create an SMTP connection."""
 
121
        self._connection = self._smtp_factory()
 
122
        try:
 
123
            self._connection.connect(self._smtp_server)
 
124
        except socket.error as e:
 
125
            if e.args[0] == errno.ECONNREFUSED:
 
126
                if self._config_smtp_server is None:
 
127
                    raise DefaultSMTPConnectionRefused(socket.error,
 
128
                                                       self._smtp_server)
 
129
                else:
 
130
                    raise SMTPConnectionRefused(socket.error,
 
131
                                                self._smtp_server)
 
132
            else:
 
133
                raise
 
134
 
 
135
        # Say EHLO (falling back to HELO) to query the server's features.
 
136
        code, resp = self._connection.ehlo()
 
137
        if not (200 <= code <= 299):
 
138
            code, resp = self._connection.helo()
 
139
            if not (200 <= code <= 299):
 
140
                raise SMTPError("server refused HELO: %d %s" % (code, resp))
 
141
 
 
142
        # Use TLS if the server advertised it:
 
143
        if self._connection.has_extn("starttls"):
 
144
            code, resp = self._connection.starttls()
 
145
            if not (200 <= code <= 299):
 
146
                raise SMTPError("server refused STARTTLS: %d %s" % (code, resp))
 
147
            # Say EHLO again, to check for newly revealed features
 
148
            code, resp = self._connection.ehlo()
 
149
            if not (200 <= code <= 299):
 
150
                raise SMTPError("server refused EHLO: %d %s" % (code, resp))
 
151
 
 
152
    def _authenticate(self):
 
153
        """If necessary authenticate yourself to the server."""
 
154
        auth = config.AuthenticationConfig()
 
155
        if self._smtp_username is None:
 
156
            # FIXME: Since _authenticate gets called even when no authentication
 
157
            # is necessary, it's not possible to use the default username 
 
158
            # here yet.
 
159
            self._smtp_username = auth.get_user('smtp', self._smtp_server)
 
160
            if self._smtp_username is None:
 
161
                return
 
162
 
 
163
        if self._smtp_password is None:
 
164
            self._smtp_password = auth.get_password(
 
165
                'smtp', self._smtp_server, self._smtp_username)
 
166
 
 
167
        # smtplib requires that the username and password be byte
 
168
        # strings.  The CRAM-MD5 spec doesn't give any guidance on
 
169
        # encodings, but the SASL PLAIN spec says UTF-8, so that's
 
170
        # what we'll use.
 
171
        username = osutils.safe_utf8(self._smtp_username)
 
172
        password = osutils.safe_utf8(self._smtp_password)
 
173
 
 
174
        self._connection.login(username, password)
 
175
 
 
176
    @staticmethod
 
177
    def get_message_addresses(message):
 
178
        """Get the origin and destination addresses of a message.
 
179
 
 
180
        :param message: A message object supporting get() to access its
 
181
            headers, like email.message.Message or
 
182
            breezy.email_message.EmailMessage.
 
183
        :return: A pair (from_email, to_emails), where from_email is the email
 
184
            address in the From header, and to_emails a list of all the
 
185
            addresses in the To, Cc, and Bcc headers.
 
186
        """
 
187
        from_email = parseaddr(message.get('From', None))[1]
 
188
        to_full_addresses = []
 
189
        for header in ['To', 'Cc', 'Bcc']:
 
190
            value = message.get(header, None)
 
191
            if value:
 
192
                to_full_addresses.append(value)
 
193
        to_emails = [ pair[1] for pair in
 
194
                getaddresses(to_full_addresses) ]
 
195
 
 
196
        return from_email, to_emails
 
197
 
 
198
    def send_email(self, message):
 
199
        """Send an email message.
 
200
 
 
201
        The message will be sent to all addresses in the To, Cc and Bcc
 
202
        headers.
 
203
 
 
204
        :param message: An email.message.Message or
 
205
            email.mime.multipart.MIMEMultipart object.
 
206
        :return: None
 
207
        """
 
208
        from_email, to_emails = self.get_message_addresses(message)
 
209
 
 
210
        if not to_emails:
 
211
            raise NoDestinationAddress
 
212
 
 
213
        try:
 
214
            self._connect()
 
215
            self._connection.sendmail(from_email, to_emails,
 
216
                                      message.as_string())
 
217
        except smtplib.SMTPRecipientsRefused as e:
 
218
            raise SMTPError('server refused recipient: %d %s' %
 
219
                    next(iter(e.recipients.values())))
 
220
        except smtplib.SMTPResponseException as e:
 
221
            raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
 
222
        except smtplib.SMTPException as e:
 
223
            raise SMTPError(str(e))