/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: 2017-09-01 07:15:43 UTC
  • mfrom: (6770.3.2 py3_test_cleanup)
  • Revision ID: jelmer@jelmer.uk-20170901071543-1t83321xkog9qrxh
Merge lp:~gz/brz/py3_test_cleanup

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 __future__ import absolute_import
 
20
 
19
21
from email import Utils
20
22
import errno
21
23
import smtplib
22
24
import socket
23
25
 
24
 
from bzrlib import (
 
26
from . import (
25
27
    config,
26
28
    osutils,
27
29
    )
28
 
from bzrlib.errors import (
29
 
    NoDestinationAddress,
30
 
    SMTPError,
31
 
    DefaultSMTPConnectionRefused,
32
 
    SMTPConnectionRefused,
 
30
from .errors import (
 
31
    BzrError,
 
32
    InternalBzrError,
33
33
    )
34
34
 
35
35
 
 
36
smtp_password = config.Option('smtp_password', default=None,
 
37
        help='''\
 
38
Password to use for authentication to SMTP server.
 
39
''')
 
40
smtp_server = config.Option('smtp_server', default=None,
 
41
        help='''\
 
42
Hostname of the SMTP server to use for sending email.
 
43
''')
 
44
smtp_username = config.Option('smtp_username', default=None,
 
45
        help='''\
 
46
Username to use for authentication to SMTP server.
 
47
''')
 
48
 
 
49
 
 
50
class SMTPError(BzrError):
 
51
 
 
52
    _fmt = "SMTP error: %(error)s"
 
53
 
 
54
    def __init__(self, error):
 
55
        self.error = error
 
56
 
 
57
 
 
58
class SMTPConnectionRefused(SMTPError):
 
59
 
 
60
    _fmt = "SMTP connection to %(host)s refused"
 
61
 
 
62
    def __init__(self, error, host):
 
63
        self.error = error
 
64
        self.host = host
 
65
 
 
66
 
 
67
class DefaultSMTPConnectionRefused(SMTPConnectionRefused):
 
68
 
 
69
    _fmt = "Please specify smtp_server.  No server at default %(host)s."
 
70
 
 
71
 
 
72
 
 
73
class NoDestinationAddress(InternalBzrError):
 
74
 
 
75
    _fmt = "Message does not have a destination address."
 
76
 
 
77
 
 
78
 
36
79
class SMTPConnection(object):
37
80
    """Connect to an SMTP server and send an email.
38
81
 
39
 
    This is a gateway between bzrlib.config.Config and smtplib.SMTP. It
 
82
    This is a gateway between breezy.config.Config and smtplib.SMTP. It
40
83
    understands the basic bzr SMTP configuration information: smtp_server,
41
84
    smtp_username, and smtp_password.
42
85
    """
48
91
        if self._smtp_factory is None:
49
92
            self._smtp_factory = smtplib.SMTP
50
93
        self._config = config
51
 
        self._config_smtp_server = config.get_user_option('smtp_server')
 
94
        self._config_smtp_server = config.get('smtp_server')
52
95
        self._smtp_server = self._config_smtp_server
53
96
        if self._smtp_server is None:
54
97
            self._smtp_server = self._default_smtp_server
55
98
 
56
 
        self._smtp_username = config.get_user_option('smtp_username')
57
 
        self._smtp_password = config.get_user_option('smtp_password')
 
99
        self._smtp_username = config.get('smtp_username')
 
100
        self._smtp_password = config.get('smtp_password')
58
101
 
59
102
        self._connection = None
60
103
 
74
117
        self._connection = self._smtp_factory()
75
118
        try:
76
119
            self._connection.connect(self._smtp_server)
77
 
        except socket.error, e:
 
120
        except socket.error as e:
78
121
            if e.args[0] == errno.ECONNREFUSED:
79
122
                if self._config_smtp_server is None:
80
123
                    raise DefaultSMTPConnectionRefused(socket.error,
131
174
        """Get the origin and destination addresses of a message.
132
175
 
133
176
        :param message: A message object supporting get() to access its
134
 
            headers, like email.Message or bzrlib.email_message.EmailMessage.
 
177
            headers, like email.Message or breezy.email_message.EmailMessage.
135
178
        :return: A pair (from_email, to_emails), where from_email is the email
136
179
            address in the From header, and to_emails a list of all the
137
180
            addresses in the To, Cc, and Bcc headers.
165
208
            self._connect()
166
209
            self._connection.sendmail(from_email, to_emails,
167
210
                                      message.as_string())
168
 
        except smtplib.SMTPRecipientsRefused, e:
 
211
        except smtplib.SMTPRecipientsRefused as e:
169
212
            raise SMTPError('server refused recipient: %d %s' %
170
 
                    e.recipients.values()[0])
171
 
        except smtplib.SMTPResponseException, e:
 
213
                    next(iter(e.recipients.values())))
 
214
        except smtplib.SMTPResponseException as e:
172
215
            raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
173
 
        except smtplib.SMTPException, e:
 
216
        except smtplib.SMTPException as e:
174
217
            raise SMTPError(str(e))