17
17
"""A convenience class around smtplib."""
19
from email import Utils
19
from __future__ import absolute_import
22
from email.utils import getaddresses, parseaddr
23
except ImportError: # python < 3
24
from email.Utils import getaddresses, parseaddr
28
from bzrlib.errors import (
31
DefaultSMTPConnectionRefused,
32
SMTPConnectionRefused,
40
smtp_password = config.Option('smtp_password', default=None,
42
Password to use for authentication to SMTP server.
44
smtp_server = config.Option('smtp_server', default=None,
46
Hostname of the SMTP server to use for sending email.
48
smtp_username = config.Option('smtp_username', default=None,
50
Username to use for authentication to SMTP server.
54
class SMTPError(BzrError):
56
_fmt = "SMTP error: %(error)s"
58
def __init__(self, error):
62
class SMTPConnectionRefused(SMTPError):
64
_fmt = "SMTP connection to %(host)s refused"
66
def __init__(self, error, host):
71
class DefaultSMTPConnectionRefused(SMTPConnectionRefused):
73
_fmt = "Please specify smtp_server. No server at default %(host)s."
76
class NoDestinationAddress(InternalBzrError):
78
_fmt = "Message does not have a destination address."
36
81
class SMTPConnection(object):
37
82
"""Connect to an SMTP server and send an email.
39
This is a gateway between bzrlib.config.Config and smtplib.SMTP. It
84
This is a gateway between breezy.config.Config and smtplib.SMTP. It
40
85
understands the basic bzr SMTP configuration information: smtp_server,
41
86
smtp_username, and smtp_password.
48
93
if self._smtp_factory is None:
49
94
self._smtp_factory = smtplib.SMTP
50
95
self._config = config
51
self._config_smtp_server = config.get_user_option('smtp_server')
96
self._config_smtp_server = config.get('smtp_server')
52
97
self._smtp_server = self._config_smtp_server
53
98
if self._smtp_server is None:
54
99
self._smtp_server = self._default_smtp_server
56
self._smtp_username = config.get_user_option('smtp_username')
57
self._smtp_password = config.get_user_option('smtp_password')
101
self._smtp_username = config.get('smtp_username')
102
self._smtp_password = config.get('smtp_password')
59
104
self._connection = None
66
111
self._create_connection()
67
112
# FIXME: _authenticate() should only be called when the server has
68
# refused unauthenticated access, so it can safely try to authenticate
113
# refused unauthenticated access, so it can safely try to authenticate
69
114
# with the default username. JRV20090407
70
115
self._authenticate()
74
119
self._connection = self._smtp_factory()
76
121
self._connection.connect(self._smtp_server)
77
except socket.error, e:
122
except socket.error as e:
78
123
if e.args[0] == errno.ECONNREFUSED:
79
124
if self._config_smtp_server is None:
80
125
raise DefaultSMTPConnectionRefused(socket.error,
96
141
if self._connection.has_extn("starttls"):
97
142
code, resp = self._connection.starttls()
98
143
if not (200 <= code <= 299):
99
raise SMTPError("server refused STARTTLS: %d %s" % (code, resp))
144
raise SMTPError("server refused STARTTLS: %d %s" %
100
146
# Say EHLO again, to check for newly revealed features
101
147
code, resp = self._connection.ehlo()
102
148
if not (200 <= code <= 299):
107
153
auth = config.AuthenticationConfig()
108
154
if self._smtp_username is None:
109
155
# FIXME: Since _authenticate gets called even when no authentication
110
# is necessary, it's not possible to use the default username
156
# is necessary, it's not possible to use the default username
112
158
self._smtp_username = auth.get_user('smtp', self._smtp_server)
113
159
if self._smtp_username is None:
131
177
"""Get the origin and destination addresses of a message.
133
179
:param message: A message object supporting get() to access its
134
headers, like email.Message or bzrlib.email_message.EmailMessage.
180
headers, like email.message.Message or
181
breezy.email_message.EmailMessage.
135
182
:return: A pair (from_email, to_emails), where from_email is the email
136
183
address in the From header, and to_emails a list of all the
137
184
addresses in the To, Cc, and Bcc headers.
139
from_email = Utils.parseaddr(message.get('From', None))[1]
186
from_email = parseaddr(message.get('From', None))[1]
140
187
to_full_addresses = []
141
188
for header in ['To', 'Cc', 'Bcc']:
142
189
value = message.get(header, None)
144
191
to_full_addresses.append(value)
145
to_emails = [ pair[1] for pair in
146
Utils.getaddresses(to_full_addresses) ]
192
to_emails = [pair[1] for pair in
193
getaddresses(to_full_addresses)]
148
195
return from_email, to_emails
153
200
The message will be sent to all addresses in the To, Cc and Bcc
156
:param message: An email.Message or email.MIMEMultipart object.
203
:param message: An email.message.Message or
204
email.mime.multipart.MIMEMultipart object.
159
207
from_email, to_emails = self.get_message_addresses(message)
166
214
self._connection.sendmail(from_email, to_emails,
167
215
message.as_string())
168
except smtplib.SMTPRecipientsRefused, e:
216
except smtplib.SMTPRecipientsRefused as e:
169
217
raise SMTPError('server refused recipient: %d %s' %
170
e.recipients.values()[0])
171
except smtplib.SMTPResponseException, e:
218
next(iter(e.recipients.values())))
219
except smtplib.SMTPResponseException as e:
172
220
raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
173
except smtplib.SMTPException, e:
221
except smtplib.SMTPException as e:
174
222
raise SMTPError(str(e))