17
17
"""A convenience class around smtplib."""
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
19
from email import Utils
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."
77
class NoDestinationAddress(InternalBzrError):
79
_fmt = "Message does not have a destination address."
83
36
class SMTPConnection(object):
84
37
"""Connect to an SMTP server and send an email.
86
This is a gateway between breezy.config.Config and smtplib.SMTP. It
39
This is a gateway between bzrlib.config.Config and smtplib.SMTP. It
87
40
understands the basic bzr SMTP configuration information: smtp_server,
88
41
smtp_username, and smtp_password.
95
48
if self._smtp_factory is None:
96
49
self._smtp_factory = smtplib.SMTP
97
50
self._config = config
98
self._config_smtp_server = config.get('smtp_server')
51
self._config_smtp_server = config.get_user_option('smtp_server')
99
52
self._smtp_server = self._config_smtp_server
100
53
if self._smtp_server is None:
101
54
self._smtp_server = self._default_smtp_server
103
self._smtp_username = config.get('smtp_username')
104
self._smtp_password = config.get('smtp_password')
56
self._smtp_username = config.get_user_option('smtp_username')
57
self._smtp_password = config.get_user_option('smtp_password')
106
59
self._connection = None
121
74
self._connection = self._smtp_factory()
123
76
self._connection.connect(self._smtp_server)
124
except socket.error as e:
77
except socket.error, e:
125
78
if e.args[0] == errno.ECONNREFUSED:
126
79
if self._config_smtp_server is None:
127
80
raise DefaultSMTPConnectionRefused(socket.error,
178
131
"""Get the origin and destination addresses of a message.
180
133
:param message: A message object supporting get() to access its
181
headers, like email.message.Message or
182
breezy.email_message.EmailMessage.
134
headers, like email.Message or bzrlib.email_message.EmailMessage.
183
135
:return: A pair (from_email, to_emails), where from_email is the email
184
136
address in the From header, and to_emails a list of all the
185
137
addresses in the To, Cc, and Bcc headers.
187
from_email = parseaddr(message.get('From', None))[1]
139
from_email = Utils.parseaddr(message.get('From', None))[1]
188
140
to_full_addresses = []
189
141
for header in ['To', 'Cc', 'Bcc']:
190
142
value = message.get(header, None)
192
144
to_full_addresses.append(value)
193
145
to_emails = [ pair[1] for pair in
194
getaddresses(to_full_addresses) ]
146
Utils.getaddresses(to_full_addresses) ]
196
148
return from_email, to_emails
201
153
The message will be sent to all addresses in the To, Cc and Bcc
204
:param message: An email.message.Message or
205
email.mime.multipart.MIMEMultipart object.
156
:param message: An email.Message or email.MIMEMultipart object.
208
159
from_email, to_emails = self.get_message_addresses(message)
215
166
self._connection.sendmail(from_email, to_emails,
216
167
message.as_string())
217
except smtplib.SMTPRecipientsRefused as e:
168
except smtplib.SMTPRecipientsRefused, e:
218
169
raise SMTPError('server refused recipient: %d %s' %
219
next(iter(e.recipients.values())))
220
except smtplib.SMTPResponseException as e:
170
e.recipients.values()[0])
171
except smtplib.SMTPResponseException, e:
221
172
raise SMTPError('%d %s' % (e.smtp_code, e.smtp_error))
222
except smtplib.SMTPException as e:
173
except smtplib.SMTPException, e:
223
174
raise SMTPError(str(e))