/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-06-10 01:35:53 UTC
  • mto: (6670.4.8 move-bzr)
  • mto: This revision was merged to the branch mainline in revision 6681.
  • Revision ID: jelmer@jelmer.uk-20170610013553-560y7mn3su4pp763
Fix remaining tests.

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 (
 
30
from .errors import (
29
31
    NoDestinationAddress,
30
32
    SMTPError,
31
33
    DefaultSMTPConnectionRefused,
33
35
    )
34
36
 
35
37
 
 
38
smtp_password = config.Option('smtp_password', default=None,
 
39
        help='''\
 
40
Password to use for authentication to SMTP server.
 
41
''')
 
42
smtp_server = config.Option('smtp_server', default=None,
 
43
        help='''\
 
44
Hostname of the SMTP server to use for sending email.
 
45
''')
 
46
smtp_username = config.Option('smtp_username', default=None,
 
47
        help='''\
 
48
Username to use for authentication to SMTP server.
 
49
''')
 
50
 
 
51
 
36
52
class SMTPConnection(object):
37
53
    """Connect to an SMTP server and send an email.
38
54
 
39
 
    This is a gateway between bzrlib.config.Config and smtplib.SMTP. It
 
55
    This is a gateway between breezy.config.Config and smtplib.SMTP. It
40
56
    understands the basic bzr SMTP configuration information: smtp_server,
41
57
    smtp_username, and smtp_password.
42
58
    """
48
64
        if self._smtp_factory is None:
49
65
            self._smtp_factory = smtplib.SMTP
50
66
        self._config = config
51
 
        self._config_smtp_server = config.get_user_option('smtp_server')
 
67
        self._config_smtp_server = config.get('smtp_server')
52
68
        self._smtp_server = self._config_smtp_server
53
69
        if self._smtp_server is None:
54
70
            self._smtp_server = self._default_smtp_server
55
71
 
56
 
        self._smtp_username = config.get_user_option('smtp_username')
57
 
        self._smtp_password = config.get_user_option('smtp_password')
 
72
        self._smtp_username = config.get('smtp_username')
 
73
        self._smtp_password = config.get('smtp_password')
58
74
 
59
75
        self._connection = None
60
76
 
74
90
        self._connection = self._smtp_factory()
75
91
        try:
76
92
            self._connection.connect(self._smtp_server)
77
 
        except socket.error, e:
 
93
        except socket.error as e:
78
94
            if e.args[0] == errno.ECONNREFUSED:
79
95
                if self._config_smtp_server is None:
80
96
                    raise DefaultSMTPConnectionRefused(socket.error,
131
147
        """Get the origin and destination addresses of a message.
132
148
 
133
149
        :param message: A message object supporting get() to access its
134
 
            headers, like email.Message or bzrlib.email_message.EmailMessage.
 
150
            headers, like email.Message or breezy.email_message.EmailMessage.
135
151
        :return: A pair (from_email, to_emails), where from_email is the email
136
152
            address in the From header, and to_emails a list of all the
137
153
            addresses in the To, Cc, and Bcc headers.
165
181
            self._connect()
166
182
            self._connection.sendmail(from_email, to_emails,
167
183
                                      message.as_string())
168
 
        except smtplib.SMTPRecipientsRefused, e:
 
184
        except smtplib.SMTPRecipientsRefused as e:
169
185
            raise SMTPError('server refused recipient: %d %s' %
170
 
                    e.recipients.values()[0])
171
 
        except smtplib.SMTPResponseException, e:
 
186
                    next(iter(e.recipients.values())))
 
187
        except smtplib.SMTPResponseException as e:
172
188
            raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
173
 
        except smtplib.SMTPException, e:
 
189
        except smtplib.SMTPException as e:
174
190
            raise SMTPError(str(e))