changeset 8d3d023fc121 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=8d3d023fc121
description:
Add option to ensure emails are sent
issue9456
review298071002
diffstat:
CHANGELOG | 1 +
doc/ref/sendmail.rst | 8 ++++++--
trytond/sendmail.py | 20 +++++++++++++-------
3 files changed, 20 insertions(+), 9 deletions(-)
diffs (110 lines):
diff -r 37201325d6a4 -r 8d3d023fc121 CHANGELOG
--- a/CHANGELOG Tue Jul 21 14:32:09 2020 +0200
+++ b/CHANGELOG Tue Jul 21 14:33:49 2020 +0200
@@ -1,3 +1,4 @@
+* Add option to ensure emails are sent
* Allow keyword action for all models
* Add sortable_values in tools
* Remove default colors on graph and calendar
diff -r 37201325d6a4 -r 8d3d023fc121 doc/ref/sendmail.rst
--- a/doc/ref/sendmail.rst Tue Jul 21 14:32:09 2020 +0200
+++ b/doc/ref/sendmail.rst Tue Jul 21 14:33:49 2020 +0200
@@ -25,16 +25,20 @@
caring about the transaction.
The caller may pass a server instance from `smtplib`_.
-.. method:: get_smtp_server([uri])
+.. method:: get_smtp_server([uri[, strict]])
Return a SMTP instance from `smtplib`_ using the `uri` or the one defined in
the `email` section of the :ref:`configuration <topics-configuration>`.
+If strict is `True`, an exception is raised if it is not possible to connect to
+the server.
-.. class:: SMTPDataManager([uri])
+.. class:: SMTPDataManager([uri[, strict]])
A :class:`SMTPDataManager` implements a data manager which send queued email at
commit. An option optional `uri` can be passed to configure the SMTP
connection.
+If strict is `True`, the data manager prevents the transaction if it fails to
+send the emails.
.. method:: SMTPDataManager.put(from_addr, to_addrs, msg)
diff -r 37201325d6a4 -r 8d3d023fc121 trytond/sendmail.py
--- a/trytond/sendmail.py Tue Jul 21 14:32:09 2020 +0200
+++ b/trytond/sendmail.py Tue Jul 21 14:33:49 2020 +0200
@@ -25,9 +25,9 @@
datamanager.put(from_addr, to_addrs, msg)
-def sendmail(from_addr, to_addrs, msg, server=None):
+def sendmail(from_addr, to_addrs, msg, server=None, strict=False):
if server is None:
- server = get_smtp_server()
+ server = get_smtp_server(strict=strict)
if not server:
return
quit = True
@@ -38,6 +38,8 @@
try:
senderrs = server.sendmail(from_addr, to_addrs, msg.as_string())
except Exception:
+ if strict:
+ raise
logger.error('fail to send email', exc_info=True)
else:
if senderrs:
@@ -52,10 +54,11 @@
msg['From'] = from_
msg['To'] = to_addrs
msg['Subject'] = 'Tryton test email'
- sendmail(config.get('email', 'from'), to_addrs, msg, server=server)
+ sendmail(
+ config.get('email', 'from'), to_addrs, msg, server=server, strict=True)
-def get_smtp_server(uri=None):
+def get_smtp_server(uri=None, strict=False):
if uri is None:
uri = config.get('email', 'uri')
uri = parse_uri(uri)
@@ -71,6 +74,8 @@
try:
server = connector(uri.hostname, uri.port, **extra)
except Exception:
+ if strict:
+ raise
logger.error('fail to connect to %s', uri, exc_info=True)
return
@@ -86,8 +91,9 @@
class SMTPDataManager(object):
- def __init__(self, uri=None):
+ def __init__(self, uri=None, strict=False):
self.uri = uri
+ self.strict = strict
self.queue = []
self._server = None
@@ -98,7 +104,7 @@
def __eq__(self, other):
if not isinstance(other, SMTPDataManager):
return NotImplemented
- return self.uri == other.uri
+ return (self.uri == other.uri) and (self.strict == other.strict)
def abort(self, trans):
self._finish()
@@ -111,7 +117,7 @@
def tpc_vote(self, trans):
if self._server is None:
- self._server = get_smtp_server(self.uri)
+ self._server = get_smtp_server(self.uri, strict=self.strict)
def tpc_finish(self, trans):
if self._server is not None: