Commit c8b3791b authored by Frank Bessou's avatar Frank Bessou 🍁
Browse files

feat: allow authentication on SMTP for outgoing emails

This feature was mistakenly droped during merge of 3.28.3 into 3.29.2.

Closes #404.

--HG--
branch : 3.29
parent 111ad4c37c96
......@@ -174,7 +174,7 @@ from os.path import (exists, join, expanduser, abspath,
basename, dirname, splitext, realpath)
import pkgutil
import pkg_resources
from smtplib import SMTP
from smtplib import SMTP, SMTPAuthenticationError
import stat
import sys
from threading import Lock
......@@ -843,6 +843,16 @@ class CubicWebConfiguration(CubicWebNoAppConfiguration):
'help': 'listening port of the SMTP mail server',
'group': 'email', 'level': 1,
}),
('smtp-username',
{'default': '',
'help': 'username for SMTP mail server if auth is required',
'group': 'email', 'level': 2,
}),
('smtp-password',
{'default': '',
'help': 'password for SMTP mail server if auth is required',
'group': 'email', 'level': 2,
}),
('sender-name',
{'type' : 'string',
'default': Method('default_instance_id'),
......@@ -1237,12 +1247,21 @@ the repository',
if connection to the smtp server failed, else True.
"""
server, port = self['smtp-host'], self['smtp-port']
smtp_user, smtp_password = self['smtp-username'], self['smtp-password']
if fromaddr is None:
fromaddr = '%s <%s>' % (self['sender-name'], self['sender-addr'])
SMTP_LOCK.acquire()
try:
try:
smtp = SMTP(server, port)
try:
if smtp_user and smtp_password:
smtp.login(smtp_user, smtp_password)
except SMTPAuthenticationError as exception:
self.exception(
"cannot log in to SMTP server %s:%s (%s)",
server, port, exception
)
except Exception as ex:
self.exception("can't connect to smtp server %s:%s (%s)",
server, port, ex)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment