/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 bzrlib/smtp_connection.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from __future__ import absolute_import
20
20
 
21
 
try:
22
 
    from email.utils import getaddresses, parseaddr
23
 
except ImportError:  # python < 3
24
 
    from email.Utils import getaddresses, parseaddr
25
 
 
 
21
from email import Utils
26
22
import errno
27
23
import smtplib
28
24
import socket
29
25
 
30
 
from . import (
 
26
from bzrlib import (
31
27
    config,
32
28
    osutils,
33
29
    )
34
 
from .errors import (
35
 
    BzrError,
36
 
    InternalBzrError,
 
30
from bzrlib.errors import (
 
31
    NoDestinationAddress,
 
32
    SMTPError,
 
33
    DefaultSMTPConnectionRefused,
 
34
    SMTPConnectionRefused,
37
35
    )
38
36
 
39
37
 
51
49
''')
52
50
 
53
51
 
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
52
class SMTPConnection(object):
84
53
    """Connect to an SMTP server and send an email.
85
54
 
86
 
    This is a gateway between breezy.config.Config and smtplib.SMTP. It
 
55
    This is a gateway between bzrlib.config.Config and smtplib.SMTP. It
87
56
    understands the basic bzr SMTP configuration information: smtp_server,
88
57
    smtp_username, and smtp_password.
89
58
    """
121
90
        self._connection = self._smtp_factory()
122
91
        try:
123
92
            self._connection.connect(self._smtp_server)
124
 
        except socket.error as e:
 
93
        except socket.error, e:
125
94
            if e.args[0] == errno.ECONNREFUSED:
126
95
                if self._config_smtp_server is None:
127
96
                    raise DefaultSMTPConnectionRefused(socket.error,
178
147
        """Get the origin and destination addresses of a message.
179
148
 
180
149
        :param message: A message object supporting get() to access its
181
 
            headers, like email.message.Message or
182
 
            breezy.email_message.EmailMessage.
 
150
            headers, like email.Message or bzrlib.email_message.EmailMessage.
183
151
        :return: A pair (from_email, to_emails), where from_email is the email
184
152
            address in the From header, and to_emails a list of all the
185
153
            addresses in the To, Cc, and Bcc headers.
186
154
        """
187
 
        from_email = parseaddr(message.get('From', None))[1]
 
155
        from_email = Utils.parseaddr(message.get('From', None))[1]
188
156
        to_full_addresses = []
189
157
        for header in ['To', 'Cc', 'Bcc']:
190
158
            value = message.get(header, None)
191
159
            if value:
192
160
                to_full_addresses.append(value)
193
161
        to_emails = [ pair[1] for pair in
194
 
                getaddresses(to_full_addresses) ]
 
162
                Utils.getaddresses(to_full_addresses) ]
195
163
 
196
164
        return from_email, to_emails
197
165
 
201
169
        The message will be sent to all addresses in the To, Cc and Bcc
202
170
        headers.
203
171
 
204
 
        :param message: An email.message.Message or
205
 
            email.mime.multipart.MIMEMultipart object.
 
172
        :param message: An email.Message or email.MIMEMultipart object.
206
173
        :return: None
207
174
        """
208
175
        from_email, to_emails = self.get_message_addresses(message)
214
181
            self._connect()
215
182
            self._connection.sendmail(from_email, to_emails,
216
183
                                      message.as_string())
217
 
        except smtplib.SMTPRecipientsRefused as e:
 
184
        except smtplib.SMTPRecipientsRefused, e:
218
185
            raise SMTPError('server refused recipient: %d %s' %
219
 
                    next(iter(e.recipients.values())))
220
 
        except smtplib.SMTPResponseException as e:
 
186
                    e.recipients.values()[0])
 
187
        except smtplib.SMTPResponseException, e:
221
188
            raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
222
 
        except smtplib.SMTPException as e:
 
189
        except smtplib.SMTPException, e:
223
190
            raise SMTPError(str(e))