/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-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""A convenience class around smtplib."""
18
18
 
19
 
from email.utils import getaddresses, parseaddr
 
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
20
25
 
21
26
import errno
22
27
import smtplib
33
38
 
34
39
 
35
40
smtp_password = config.Option('smtp_password', default=None,
36
 
                              help='''\
 
41
        help='''\
37
42
Password to use for authentication to SMTP server.
38
43
''')
39
44
smtp_server = config.Option('smtp_server', default=None,
40
 
                            help='''\
 
45
        help='''\
41
46
Hostname of the SMTP server to use for sending email.
42
47
''')
43
48
smtp_username = config.Option('smtp_username', default=None,
44
 
                              help='''\
 
49
        help='''\
45
50
Username to use for authentication to SMTP server.
46
51
''')
47
52
 
68
73
    _fmt = "Please specify smtp_server.  No server at default %(host)s."
69
74
 
70
75
 
 
76
 
71
77
class NoDestinationAddress(InternalBzrError):
72
78
 
73
79
    _fmt = "Message does not have a destination address."
74
80
 
75
81
 
 
82
 
76
83
class SMTPConnection(object):
77
84
    """Connect to an SMTP server and send an email.
78
85
 
105
112
 
106
113
        self._create_connection()
107
114
        # FIXME: _authenticate() should only be called when the server has
108
 
        # refused unauthenticated access, so it can safely try to authenticate
 
115
        # refused unauthenticated access, so it can safely try to authenticate 
109
116
        # with the default username. JRV20090407
110
117
        self._authenticate()
111
118
 
136
143
        if self._connection.has_extn("starttls"):
137
144
            code, resp = self._connection.starttls()
138
145
            if not (200 <= code <= 299):
139
 
                raise SMTPError("server refused STARTTLS: %d %s" %
140
 
                                (code, resp))
 
146
                raise SMTPError("server refused STARTTLS: %d %s" % (code, resp))
141
147
            # Say EHLO again, to check for newly revealed features
142
148
            code, resp = self._connection.ehlo()
143
149
            if not (200 <= code <= 299):
148
154
        auth = config.AuthenticationConfig()
149
155
        if self._smtp_username is None:
150
156
            # FIXME: Since _authenticate gets called even when no authentication
151
 
            # is necessary, it's not possible to use the default username
 
157
            # is necessary, it's not possible to use the default username 
152
158
            # here yet.
153
159
            self._smtp_username = auth.get_user('smtp', self._smtp_server)
154
160
            if self._smtp_username is None:
184
190
            value = message.get(header, None)
185
191
            if value:
186
192
                to_full_addresses.append(value)
187
 
        to_emails = [pair[1] for pair in
188
 
                     getaddresses(to_full_addresses)]
 
193
        to_emails = [ pair[1] for pair in
 
194
                getaddresses(to_full_addresses) ]
189
195
 
190
196
        return from_email, to_emails
191
197
 
210
216
                                      message.as_string())
211
217
        except smtplib.SMTPRecipientsRefused as e:
212
218
            raise SMTPError('server refused recipient: %d %s' %
213
 
                            next(iter(e.recipients.values())))
 
219
                    next(iter(e.recipients.values())))
214
220
        except smtplib.SMTPResponseException as e:
215
221
            raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
216
222
        except smtplib.SMTPException as e: