/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-11-06 02:25:29 UTC
  • mto: This revision was merged to the branch mainline in revision 7150.
  • Revision ID: jelmer@jelmer.uk-20181106022529-qlctdqketvoibpvz
Simplify brz-git, drop imports.

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 import Utils
 
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
 
20
26
import errno
21
27
import smtplib
22
28
import socket
23
29
 
24
 
from bzrlib import (
 
30
from . import (
25
31
    config,
26
32
    osutils,
27
33
    )
28
 
from bzrlib.errors import (
29
 
    NoDestinationAddress,
30
 
    SMTPError,
31
 
    DefaultSMTPConnectionRefused,
32
 
    SMTPConnectionRefused,
 
34
from .errors import (
 
35
    BzrError,
 
36
    InternalBzrError,
33
37
    )
34
38
 
35
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
 
36
83
class SMTPConnection(object):
37
84
    """Connect to an SMTP server and send an email.
38
85
 
39
 
    This is a gateway between bzrlib.config.Config and smtplib.SMTP. It
 
86
    This is a gateway between breezy.config.Config and smtplib.SMTP. It
40
87
    understands the basic bzr SMTP configuration information: smtp_server,
41
88
    smtp_username, and smtp_password.
42
89
    """
48
95
        if self._smtp_factory is None:
49
96
            self._smtp_factory = smtplib.SMTP
50
97
        self._config = config
51
 
        self._config_smtp_server = config.get_user_option('smtp_server')
 
98
        self._config_smtp_server = config.get('smtp_server')
52
99
        self._smtp_server = self._config_smtp_server
53
100
        if self._smtp_server is None:
54
101
            self._smtp_server = self._default_smtp_server
55
102
 
56
 
        self._smtp_username = config.get_user_option('smtp_username')
57
 
        self._smtp_password = config.get_user_option('smtp_password')
 
103
        self._smtp_username = config.get('smtp_username')
 
104
        self._smtp_password = config.get('smtp_password')
58
105
 
59
106
        self._connection = None
60
107
 
74
121
        self._connection = self._smtp_factory()
75
122
        try:
76
123
            self._connection.connect(self._smtp_server)
77
 
        except socket.error, e:
 
124
        except socket.error as e:
78
125
            if e.args[0] == errno.ECONNREFUSED:
79
126
                if self._config_smtp_server is None:
80
127
                    raise DefaultSMTPConnectionRefused(socket.error,
131
178
        """Get the origin and destination addresses of a message.
132
179
 
133
180
        :param message: A message object supporting get() to access its
134
 
            headers, like email.Message or bzrlib.email_message.EmailMessage.
 
181
            headers, like email.message.Message or
 
182
            breezy.email_message.EmailMessage.
135
183
        :return: A pair (from_email, to_emails), where from_email is the email
136
184
            address in the From header, and to_emails a list of all the
137
185
            addresses in the To, Cc, and Bcc headers.
138
186
        """
139
 
        from_email = Utils.parseaddr(message.get('From', None))[1]
 
187
        from_email = parseaddr(message.get('From', None))[1]
140
188
        to_full_addresses = []
141
189
        for header in ['To', 'Cc', 'Bcc']:
142
190
            value = message.get(header, None)
143
191
            if value:
144
192
                to_full_addresses.append(value)
145
193
        to_emails = [ pair[1] for pair in
146
 
                Utils.getaddresses(to_full_addresses) ]
 
194
                getaddresses(to_full_addresses) ]
147
195
 
148
196
        return from_email, to_emails
149
197
 
153
201
        The message will be sent to all addresses in the To, Cc and Bcc
154
202
        headers.
155
203
 
156
 
        :param message: An email.Message or email.MIMEMultipart object.
 
204
        :param message: An email.message.Message or
 
205
            email.mime.multipart.MIMEMultipart object.
157
206
        :return: None
158
207
        """
159
208
        from_email, to_emails = self.get_message_addresses(message)
165
214
            self._connect()
166
215
            self._connection.sendmail(from_email, to_emails,
167
216
                                      message.as_string())
168
 
        except smtplib.SMTPRecipientsRefused, e:
 
217
        except smtplib.SMTPRecipientsRefused as e:
169
218
            raise SMTPError('server refused recipient: %d %s' %
170
 
                    e.recipients.values()[0])
171
 
        except smtplib.SMTPResponseException, e:
 
219
                    next(iter(e.recipients.values())))
 
220
        except smtplib.SMTPResponseException as e:
172
221
            raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
173
 
        except smtplib.SMTPException, e:
 
222
        except smtplib.SMTPException as e:
174
223
            raise SMTPError(str(e))