Support mailto://localhost (default user is root) (#1360)

1243-add-lark-support
Chris Caron 2025-07-06 11:14:09 -04:00 committed by GitHub
parent b72163aee2
commit 3e975f0c0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 122 additions and 14 deletions

View File

@ -393,7 +393,7 @@ class NotifyEmail(NotifyBase):
it was provided.
"""
if self.smtp_host or not self.user:
if self.smtp_host:
# SMTP Server was explicitly specified, therefore it is assumed
# the caller knows what he's doing and is intentionally
# over-riding any smarts to be applied. We also can not apply
@ -404,7 +404,7 @@ class NotifyEmail(NotifyBase):
from_addr = '{}@{}'.format(
re.split(r'[\s@]+', self.user)[0],
self.host,
)
) if self.user else self.host
for i in range(len(templates.EMAIL_TEMPLATES)): # pragma: no branch
self.logger.trace('Scanning %s against %s' % (
@ -458,6 +458,14 @@ class NotifyEmail(NotifyBase):
# not supported; switch it to email
self.user = '{}@{}'.format(self.user, self.host)
if 'from_user' in templates.EMAIL_TEMPLATES[i][2] \
and not self.from_addr[1]:
# Update our from address if defined
self.from_addr[1] = '{}@{}'.format(
templates.EMAIL_TEMPLATES[i][2]['from_user'],
self.host)
break
def send(self, body, title='', notify_type=NotifyType.INFO, attach=None,

View File

@ -255,6 +255,19 @@ EMAIL_TEMPLATES = (
},
),
# Localhost handling
(
'Local Mail Server',
re.compile(
r'^(((?P<label>[^+]+)\+)?(?P<id>[^@]+)@)?'
r'(?P<domain>localhost(\.localdomain)?)$', re.I),
{
# Provide a default user if one isn't provided
'from_user': 'root',
'smtp_host': None,
},
),
# Catch All
(
'Custom',

View File

@ -1798,12 +1798,12 @@ def test_plugin_email_variables_1087():
assert isinstance(result, list)
assert len(result) == 1
email = result[0]
assert email.from_addr == ['Apprise', 'testuser@alt.lan']
assert email.user == 'testuser@alt.lan'
assert email.smtp_host == 'smtp.alt.lan'
assert email.targets == [(False, 'alteriks@alt.lan')]
assert email.password == 'xxxxXXXxxx'
_email = result[0]
assert _email.from_addr == ['Apprise', 'testuser@alt.lan']
assert _email.user == 'testuser@alt.lan'
assert _email.smtp_host == 'smtp.alt.lan'
assert _email.targets == [(False, 'alteriks@alt.lan')]
assert _email.password == 'xxxxXXXxxx'
# Valid Configuration
result, _ = ConfigBase.config_parse(cleandoc("""
@ -1821,12 +1821,12 @@ def test_plugin_email_variables_1087():
assert isinstance(result, list)
assert len(result) == 1
email = result[0]
assert email.from_addr == ['Apprise', 'joe@alt.lan']
assert email.user == 'joe@alt.lan'
assert email.smtp_host == 'smtp.alt.lan'
assert email.targets == [(False, 'alteriks@alt.lan')]
assert email.password == 'abcd'
_email = result[0]
assert _email.from_addr == ['Apprise', 'joe@alt.lan']
assert _email.user == 'joe@alt.lan'
assert _email.smtp_host == 'smtp.alt.lan'
assert _email.targets == [(False, 'alteriks@alt.lan')]
assert _email.password == 'abcd'
@mock.patch('smtplib.SMTP_SSL')
@ -1890,6 +1890,93 @@ def test_plugin_email_to_handling_1356(mock_smtp, mock_smtp_ssl):
assert _msg.split('\n')[-3] == 'body'
@mock.patch('smtplib.SMTP_SSL')
@mock.patch('smtplib.SMTP')
def test_plugin_email_variables_1334(mock_smtp, mock_smtp_ssl):
"""
NotifyEmail() GitHub Issue 1334
https://github.com/caronc/apprise/issues/1334
Localhost & Local Domain default user
"""
response = mock.Mock()
mock_smtp_ssl.return_value = response
mock_smtp.return_value = response
results = email.NotifyEmail.parse_url('mailto://localhost')
assert isinstance(results, dict)
assert results['user'] is None
assert results['password'] is None
assert results['host'] == 'localhost'
assert results['port'] is None
assert results['from_addr'] == ''
assert results['smtp_host'] == ''
assert results['targets'] == []
obj = Apprise.instantiate(results, suppress_exceptions=False)
assert isinstance(obj, email.NotifyEmail)
assert len(obj.targets) == 1
assert (False, 'root@localhost') in obj.targets
assert obj.smtp_host == 'localhost'
assert obj.secure is False
assert obj.from_addr == [False, 'root@localhost']
assert mock_smtp.call_count == 0
assert mock_smtp_ssl.call_count == 0
assert obj.notify('body', 'title') is True
assert mock_smtp.call_count == 1
assert mock_smtp_ssl.call_count == 0
assert response.starttls.call_count == 0
# No login occurs as no user/pass was provided
assert response.login.call_count == 0
assert response.sendmail.call_count == 1
#
# Again, but a different variation of the localhost domain
#
mock_smtp.reset_mock()
mock_smtp_ssl.reset_mock()
response.reset_mock()
results = email.NotifyEmail.parse_url(
'mailtos://user@localhost.localdomain')
assert isinstance(results, dict)
assert results['user'] == 'user'
assert results['password'] is None
assert results['host'] == 'localhost.localdomain'
assert results['port'] is None
assert results['from_addr'] == ''
assert results['smtp_host'] == ''
assert results['targets'] == []
obj = Apprise.instantiate(results, suppress_exceptions=False)
assert isinstance(obj, email.NotifyEmail)
assert len(obj.targets) == 1
assert (False, 'user@localhost.localdomain') in obj.targets
assert obj.smtp_host == 'localhost.localdomain'
assert obj.secure is True
assert obj.from_addr == ["Apprise", 'user@localhost.localdomain']
assert mock_smtp.call_count == 0
assert mock_smtp_ssl.call_count == 0
assert obj.notify('body', 'title') is True
assert mock_smtp.call_count == 1
assert mock_smtp_ssl.call_count == 0
assert response.starttls.call_count == 1
# No login occurs as no user/pass was provided
assert response.login.call_count == 0
assert response.sendmail.call_count == 1
@mock.patch('smtplib.SMTP_SSL')
@mock.patch('smtplib.SMTP')
def test_plugin_host_detection_from_source_email(mock_smtp, mock_smtp_ssl):