Add localhost_name and timeout as get_smtp_server uri parameters

This allows a better customization of the smtp connection

issue6540
review39411002
This commit is contained in:
Sergi Almacellas Abellana
2017-06-16 09:47:39 +02:00
parent b01181b76d
commit 5dfd3b0791
4 changed files with 28 additions and 2 deletions
+1
View File
@@ -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
+9
View File
@@ -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
+8 -2
View File
@@ -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()
+10
View File
@@ -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):