Email (SMTP) multiple recipients and message info

Hi all, I’m a newbie when it comes to HA, but I’ve managed to get most things working so far. I have a trigger that is able to send an email to one recipient. I’d like to send the same email to several people at the same time
notify:
name: NOTIFIER_NAME
platform: smtp
server: MAIL_SERVER
port: YOUR_SMTP_PORT
sender: SENDER_EMAIL_ADDRESS
starttls: true or false
username: YOUR_SMTP_USERNAME
password: YOUR_SMTP_PASSWORD
recipient:
- [email protected]
- [email protected]
I’ve tried the above and various iterations that are similar to conditional nesting (used in automation) but to no avail.
I’ve also tried recipient: '[email protected]; [email protected]

My second question is around the “message:” field. I am able to setup the message with simple text, but when I try to use a template it fails. As an example this works…
message: 'Door open’
this doesn’t
message: 'Door is open @ {{states.sensor.garagefeedback.state}}%.'
although the second works fine in the template tester showing this- > Door is open @ 10%.

Thanks in advance!

With regard to my second question…both my examples above appear to work now :blush:

But I’d still like help with the first problem, thanks

When a message is triggered, what error do you get?

Hi,I’ve tried a lot of variations here are some samples
like this
recipient: '[email protected] [email protected]
produces…
16-10-03 01:29:31 homeassistant.bootstrap: Invalid config for [notify.smtp]: expected an Email for dictionary value @ data[‘recipient’]. Got ‘[email protected] [email protected]

like this
recipient: ‘[email protected]’, '[email protected]
produces…
16-10-03 02:08:36 homeassistant.util.yaml: while parsing a block mapping
in “/home/hass/.homeassistant/devices/notify.yaml”, line 1, column 3
expected , but found ‘,’
in “/home/hass/.homeassistant/devices/notify.yaml”, line 9, column 32

like this
recipient: '[email protected], [email protected]
produces
16-10-03 01:38:48 homeassistant.bootstrap: Invalid config for [notify.smtp]: expected an Email for dictionary value @ data[‘recipient’]. Got ‘[email protected], [email protected]

Like this:
recipient:
- '[email protected]
- '[email protected]
produces:
16-10-03 01:33:32 homeassistant.bootstrap: Invalid config for [notify.smtp]: expected an Email for dictionary value @ data[‘recipient’]. Got [‘[email protected]’, ‘[email protected]’]

I’ve checked my gmail account and a comma is used to separate email addresses in the ‘To:’ field when constructing a email. I have a work around that sends two separate emails from the same trigger using two services from the same action, calling two different “notify” constructs which are identical except for the recipient addresses and the name of course. Using the work around generates emails that are a few seconds apart with slightly differing values…not a big deal but it would be nice if they were identical and sent at the same time.

Any ideas?

I modified the SMTP component as a custom component for the exact same reason. It allows you to assign the target/recipient when you call the service.

You can find my modified version here

Save that as my_smtp.py, (or whatever you want to call it), to your “…/.homeassistant/custom_components/notify” folder.

Configuration Example ( notice the lack of recipient: )

notify:
  platform: my_smtp
  name: gmail
  server: smtp.gmail.com
  port: 587
  sender: !secret smtp_sender
  starttls: 1
  username: !secret smtp_username
  password: !secret smtp_password

Example Usage: ( Separate targets with commas )

service: notify.gmail
  data:
    target: [email protected],[email protected]
    title: 'Your Subject'
    message: 'Your Message'
1 Like

Hi, thanks VladTepz. That’s perfect! I gave it a test and it works no problems…thanks again.

I tried your my_smtp.py today. Could not send anything. It was failing with this error:
AttributeError: ‘list’ object has no attribute ‘split’

When I hardcoded recipient, it started failing on msg:
AttributeError: ‘list’ object has no attribute ‘encode’

I am actually having the same error as well.

Below are my error log entries in case they help.

17-01-15 22:43:10 homeassistant.core: Error doing job: Future exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
   result = self.fn(*self.args, **self.kwargs)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/core.py", line 1054, in execute_service
    service_handler.func(service_call)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/notify/__init__.py", line 110,         in notify_message
    notify_service.send_message(**kwargs)
  File "/home/hass/.homeassistant/custom_components/notify/my_smtp.py", line 138, in send_message
    return self._send_email(msg, recipient)
  File "/home/hass/.homeassistant/custom_components/notify/my_smtp.py", line 145, in _send_email
    mail.sendmail(self._sender, recipient.split(','),
AttributeError: 'list' object has no attribute 'split'

If you get the error AttributeError: ‘list’ object has no attribute ‘split’ using VladTepz’s SMTP modification I found a fix.

To fix it modify the SMTP.py file you created (like my_smtp.py) and add recipientstring = str(recipient[0])

to look like this

subject = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
recipient = kwargs.get(ATTR_TARGET)
recipientstring = str(recipient[0])
data = kwargs.get(ATTR_DATA)

Then you need to replace a few other instances of recpient with recipientstring so that the SMTP script uses your new string instead of the list.

msg[‘Subject’] = subject
msg[‘To’] = recipientstring
msg[‘From’] = self._sender
msg[‘X-Mailer’] = ‘HomeAssistant’

    return self._send_email(msg, recipientstring)
def _send_email(self, msg, recipientstring):
    """Send the message."""
    mail = self.connect()
    for _ in range(self.tries):
        try:
            mail.sendmail(self._sender, recipientstring.split(','),
                          msg.as_string())

The reason for all this is the original recipient value comes out like [‘[email protected],[email protected]’] when you need it to be just [email protected],[email protected] This string conversion does that so the Split function can handle the multiple email addreses.

If you get the error TypeError: get_service() takes 2 positional arguments but 3 were given using VladTepz’s SMTP modification I found a fix.

To fix it modify the SMTP.py file you created (like my_smtp.py) and add discovery_info=None to the code below

def get_service(hass, config, discovery_info=None):
“”“Get the mail notification service.”“”
mail_service = MailNotificationService(
config.get(CONF_SERVER),
config.get(CONF_PORT),
config.get(CONF_SENDER),
config.get(CONF_STARTTLS),
config.get(CONF_USERNAME),
config.get(CONF_PASSWORD),
config.get(CONF_DEBUG))

has this been pushed to be considered for a release as the standard component?

This functionality was added in #7319 and merged to be available in version 0.44.

I setup my notify.smtp in configuration because I wanted to get SMS messages for notify automation, and it works great. ATT allows you to send an email that generates an SMS:

[email protected] (text message)
[email protected] (picture or video message)

The only not great part is to get it to work with Gmail I had to set:

Allow less secure apps: ON

What I would really prefer to do is add an app specific password, so that it is a little more secure. Is that possible with notify.smtp?

That is possible, I’m on the road at the moment. Otherwise I’d share a link. You should be able to google it