Add support for overriding SMTP recipient(s) in a service call (#47611)
parent
583626a74f
commit
90e9216e9a
|
@ -12,6 +12,7 @@ import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
ATTR_DATA,
|
ATTR_DATA,
|
||||||
|
ATTR_TARGET,
|
||||||
ATTR_TITLE,
|
ATTR_TITLE,
|
||||||
ATTR_TITLE_DEFAULT,
|
ATTR_TITLE_DEFAULT,
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
|
@ -182,7 +183,11 @@ class MailNotificationService(BaseNotificationService):
|
||||||
msg = _build_text_msg(message)
|
msg = _build_text_msg(message)
|
||||||
|
|
||||||
msg["Subject"] = subject
|
msg["Subject"] = subject
|
||||||
msg["To"] = ",".join(self.recipients)
|
|
||||||
|
recipients = kwargs.get(ATTR_TARGET)
|
||||||
|
if not recipients:
|
||||||
|
recipients = self.recipients
|
||||||
|
msg["To"] = recipients if isinstance(recipients, str) else ",".join(recipients)
|
||||||
if self._sender_name:
|
if self._sender_name:
|
||||||
msg["From"] = f"{self._sender_name} <{self._sender}>"
|
msg["From"] = f"{self._sender_name} <{self._sender}>"
|
||||||
else:
|
else:
|
||||||
|
@ -191,14 +196,14 @@ class MailNotificationService(BaseNotificationService):
|
||||||
msg["Date"] = email.utils.format_datetime(dt_util.now())
|
msg["Date"] = email.utils.format_datetime(dt_util.now())
|
||||||
msg["Message-Id"] = email.utils.make_msgid()
|
msg["Message-Id"] = email.utils.make_msgid()
|
||||||
|
|
||||||
return self._send_email(msg)
|
return self._send_email(msg, recipients)
|
||||||
|
|
||||||
def _send_email(self, msg):
|
def _send_email(self, msg, recipients):
|
||||||
"""Send the message."""
|
"""Send the message."""
|
||||||
mail = self.connect()
|
mail = self.connect()
|
||||||
for _ in range(self.tries):
|
for _ in range(self.tries):
|
||||||
try:
|
try:
|
||||||
mail.sendmail(self._sender, self.recipients, msg.as_string())
|
mail.sendmail(self._sender, recipients, msg.as_string())
|
||||||
break
|
break
|
||||||
except smtplib.SMTPServerDisconnected:
|
except smtplib.SMTPServerDisconnected:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
|
|
|
@ -16,9 +16,9 @@ from homeassistant.setup import async_setup_component
|
||||||
class MockSMTP(MailNotificationService):
|
class MockSMTP(MailNotificationService):
|
||||||
"""Test SMTP object that doesn't need a working server."""
|
"""Test SMTP object that doesn't need a working server."""
|
||||||
|
|
||||||
def _send_email(self, msg):
|
def _send_email(self, msg, recipients):
|
||||||
"""Just return string for testing."""
|
"""Just return msg string and recipients for testing."""
|
||||||
return msg.as_string()
|
return msg.as_string(), recipients
|
||||||
|
|
||||||
|
|
||||||
async def test_reload_notify(hass):
|
async def test_reload_notify(hass):
|
||||||
|
@ -140,7 +140,7 @@ def test_send_message(message_data, data, content_type, hass, message):
|
||||||
"""Verify if we can send messages of all types correctly."""
|
"""Verify if we can send messages of all types correctly."""
|
||||||
sample_email = "<mock@mock>"
|
sample_email = "<mock@mock>"
|
||||||
with patch("email.utils.make_msgid", return_value=sample_email):
|
with patch("email.utils.make_msgid", return_value=sample_email):
|
||||||
result = message.send_message(message_data, data=data)
|
result, _ = message.send_message(message_data, data=data)
|
||||||
assert content_type in result
|
assert content_type in result
|
||||||
|
|
||||||
|
|
||||||
|
@ -162,5 +162,30 @@ def test_send_text_message(hass, message):
|
||||||
sample_email = "<mock@mock>"
|
sample_email = "<mock@mock>"
|
||||||
message_data = "Test msg"
|
message_data = "Test msg"
|
||||||
with patch("email.utils.make_msgid", return_value=sample_email):
|
with patch("email.utils.make_msgid", return_value=sample_email):
|
||||||
result = message.send_message(message_data)
|
result, _ = message.send_message(message_data)
|
||||||
assert re.search(expected, result)
|
assert re.search(expected, result)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"target",
|
||||||
|
[
|
||||||
|
None,
|
||||||
|
"target@example.com",
|
||||||
|
],
|
||||||
|
ids=[
|
||||||
|
"Verify we can send email to default recipient.",
|
||||||
|
"Verify email recipient can be overwritten by target arg.",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_send_target_message(target, hass, message):
|
||||||
|
"""Verify if we can send email to correct recipient."""
|
||||||
|
sample_email = "<mock@mock>"
|
||||||
|
message_data = "Test msg"
|
||||||
|
with patch("email.utils.make_msgid", return_value=sample_email):
|
||||||
|
if not target:
|
||||||
|
expected_recipient = ["recip1@example.com", "testrecip@test.com"]
|
||||||
|
else:
|
||||||
|
expected_recipient = target
|
||||||
|
|
||||||
|
_, recipient = message.send_message(message_data, target=target)
|
||||||
|
assert recipient == expected_recipient
|
||||||
|
|
Loading…
Reference in New Issue