From 5dfd3b07919348b60d8d3250e95dc0e6d840b69f Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Fri, 16 Jun 2017 09:47:39 +0200 Subject: [PATCH] Add localhost_name and timeout as get_smtp_server uri parameters This allows a better customization of the smtp connection issue6540 review39411002 --- CHANGELOG | 1 + doc/topics/configuration.rst | 9 +++++++++ trytond/sendmail.py | 10 ++++++++-- trytond/tests/test_sendmail.py | 10 ++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7945823fda..8f7cc3b79c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add localhost_name and timeout as get_smtp_server uri parameters * Delete missing modules not activated when updating module list * Remove empty pages from notebook * Allow to store Dict as JSON on the database diff --git a/doc/topics/configuration.rst b/doc/topics/configuration.rst index 1ed05e6183..fe2ccf6b6b 100644 --- a/doc/topics/configuration.rst +++ b/doc/topics/configuration.rst @@ -191,6 +191,15 @@ The available protocols are: - `smtp+tls`: SMTP with STARTTLS - `smtps`: SMTP with SSL +The uri accepts the following additional parameters: + +* `local_hostname`: used as FQDN of the local host in the HELO/EHLO commands, + if omited it will use the value of `socket.getfqdn()`. +* `timeout`: A number of seconds used as timeout for blocking operations. A + `socket.timeout` will be raised when exceeded. If omited the default timeout + will be used. + + Default: `smtp://localhost:25` from diff --git a/trytond/sendmail.py b/trytond/sendmail.py index 4a2a2dbc0d..1e9c4c1904 100644 --- a/trytond/sendmail.py +++ b/trytond/sendmail.py @@ -4,6 +4,7 @@ import logging import smtplib import urllib from email.message import Message +from urlparse import parse_qs from .config import config, parse_uri from .transaction import Transaction @@ -44,10 +45,15 @@ def get_smtp_server(uri=None): if uri is None: uri = config.get('email', 'uri') uri = parse_uri(uri) + extra = {} + if uri.query: + cast = {'timeout': int} + for key, value in parse_qs(uri.query, strict_parsing=True).iteritems(): + extra[key] = cast.get(key, lambda a: a)(value[0]) if uri.scheme.startswith('smtps'): - server = smtplib.SMTP_SSL(uri.hostname, uri.port) + server = smtplib.SMTP_SSL(uri.hostname, uri.port, **extra) else: - server = smtplib.SMTP(uri.hostname, uri.port) + server = smtplib.SMTP(uri.hostname, uri.port, **extra) if 'tls' in uri.scheme: server.starttls() diff --git a/trytond/tests/test_sendmail.py b/trytond/tests/test_sendmail.py index 017740862a..6c6047fcc9 100644 --- a/trytond/tests/test_sendmail.py +++ b/trytond/tests/test_sendmail.py @@ -67,6 +67,16 @@ class SendmailTestCase(unittest.TestCase): SMTP.assert_called_once_with('localhost', 25) server.starttls.assert_called_once_with() + def test_get_smtp_server_extra_parameters(self): + 'Test get_smtp_server uri extra parameters' + with patch.object(smtplib, 'SMTP') as SMTP: + SMTP.return_value = server = Mock() + params = 'timeout=30&local_hostname=smtp.example.com' + self.assertEqual( + get_smtp_server('smtp://localhost:25?%s' % params), server) + SMTP.assert_called_once_with( + 'localhost', 25, timeout=30, local_hostname='smtp.example.com') + @patch('trytond.sendmail.get_smtp_server') @with_transaction() def test_SMTPDataManager(self, get_smtp_server):