17
17
"""A convenience class around smtplib."""
19
from email.utils import getaddresses, parseaddr
19
from __future__ import absolute_import
21
from email import Utils
30
from bzrlib.errors import (
33
DefaultSMTPConnectionRefused,
34
SMTPConnectionRefused,
35
38
smtp_password = config.Option('smtp_password', default=None,
37
40
Password to use for authentication to SMTP server.
39
42
smtp_server = config.Option('smtp_server', default=None,
41
44
Hostname of the SMTP server to use for sending email.
43
46
smtp_username = config.Option('smtp_username', default=None,
45
48
Username to use for authentication to SMTP server.
49
class SMTPError(BzrError):
51
_fmt = "SMTP error: %(error)s"
53
def __init__(self, error):
57
class SMTPConnectionRefused(SMTPError):
59
_fmt = "SMTP connection to %(host)s refused"
61
def __init__(self, error, host):
66
class DefaultSMTPConnectionRefused(SMTPConnectionRefused):
68
_fmt = "Please specify smtp_server. No server at default %(host)s."
71
class NoDestinationAddress(InternalBzrError):
73
_fmt = "Message does not have a destination address."
76
52
class SMTPConnection(object):
77
53
"""Connect to an SMTP server and send an email.
79
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
80
56
understands the basic bzr SMTP configuration information: smtp_server,
81
57
smtp_username, and smtp_password.
106
82
self._create_connection()
107
83
# FIXME: _authenticate() should only be called when the server has
108
# refused unauthenticated access, so it can safely try to authenticate
84
# refused unauthenticated access, so it can safely try to authenticate
109
85
# with the default username. JRV20090407
110
86
self._authenticate()
114
90
self._connection = self._smtp_factory()
116
92
self._connection.connect(self._smtp_server)
117
except socket.error as e:
93
except socket.error, e:
118
94
if e.args[0] == errno.ECONNREFUSED:
119
95
if self._config_smtp_server is None:
120
96
raise DefaultSMTPConnectionRefused(socket.error,
136
112
if self._connection.has_extn("starttls"):
137
113
code, resp = self._connection.starttls()
138
114
if not (200 <= code <= 299):
139
raise SMTPError("server refused STARTTLS: %d %s" %
115
raise SMTPError("server refused STARTTLS: %d %s" % (code, resp))
141
116
# Say EHLO again, to check for newly revealed features
142
117
code, resp = self._connection.ehlo()
143
118
if not (200 <= code <= 299):
148
123
auth = config.AuthenticationConfig()
149
124
if self._smtp_username is None:
150
125
# FIXME: Since _authenticate gets called even when no authentication
151
# is necessary, it's not possible to use the default username
126
# is necessary, it's not possible to use the default username
153
128
self._smtp_username = auth.get_user('smtp', self._smtp_server)
154
129
if self._smtp_username is None:
172
147
"""Get the origin and destination addresses of a message.
174
149
:param message: A message object supporting get() to access its
175
headers, like email.message.Message or
176
breezy.email_message.EmailMessage.
150
headers, like email.Message or bzrlib.email_message.EmailMessage.
177
151
:return: A pair (from_email, to_emails), where from_email is the email
178
152
address in the From header, and to_emails a list of all the
179
153
addresses in the To, Cc, and Bcc headers.
181
from_email = parseaddr(message.get('From', None))[1]
155
from_email = Utils.parseaddr(message.get('From', None))[1]
182
156
to_full_addresses = []
183
157
for header in ['To', 'Cc', 'Bcc']:
184
158
value = message.get(header, None)
186
160
to_full_addresses.append(value)
187
to_emails = [pair[1] for pair in
188
getaddresses(to_full_addresses)]
161
to_emails = [ pair[1] for pair in
162
Utils.getaddresses(to_full_addresses) ]
190
164
return from_email, to_emails
195
169
The message will be sent to all addresses in the To, Cc and Bcc
198
:param message: An email.message.Message or
199
email.mime.multipart.MIMEMultipart object.
172
:param message: An email.Message or email.MIMEMultipart object.
202
175
from_email, to_emails = self.get_message_addresses(message)
209
182
self._connection.sendmail(from_email, to_emails,
210
183
message.as_string())
211
except smtplib.SMTPRecipientsRefused as e:
184
except smtplib.SMTPRecipientsRefused, e:
212
185
raise SMTPError('server refused recipient: %d %s' %
213
next(iter(e.recipients.values())))
214
except smtplib.SMTPResponseException as e:
186
e.recipients.values()[0])
187
except smtplib.SMTPResponseException, e:
215
188
raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
216
except smtplib.SMTPException as e:
189
except smtplib.SMTPException, e:
217
190
raise SMTPError(str(e))