appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

Send a HTML email through HA notify service

Open GigaTech opened this issue 5 years ago • 3 comments

Hi, I am trying to send a HTML email through HA notify service

This works self.call_service('notify/email_info', title = notification_title, message = notification_body)

This also works self.notify(notification_body, title = notification_title, name = 'email_info')

but both sends out a plain text email message. is there any way to get to the built in html message body format

This does not work self.call_service('notify/email_info', title = notification_title, html = notification_body)

Here are the HA docs https://www.home-assistant.io/integrations/smtp/

automations.yaml to send html email:

- alias: x_email
  trigger:
    - platform: time_pattern
      seconds: /10

  condition:
    - condition: state
      entity_id: input_boolean.testing
      state: "on"

  action:
    - service: notify.email_info
      data:
        title: 'Email From Home Assistant - title'      # Email subject line
        message: "Email From Home Assistant - message"  # This is required, NOT used for HTML emails
        data:
          html: >
            <!doctype html>
            <html lang="en" xmlns="http://www.w3.org/19...

this works fine. NOTE: The double 'data:' just thinking this look different to other services?

Thanks

GigaTech avatar Nov 06 '20 20:11 GigaTech

To clarify this further, below are all the files laying out what is needed to test.

with all this config it will send three emails:

  • one by HA action service (HTML WORKS JUST FINE)
  • the second email by HA Python scrypt (plain text only)
  • and the third bt AppDaemon Python script (plain text only)

My issues are that I CANNOT send a HTML email via AppDaemon nor HA Python script (this method will not fit my needs)

The docs for AppDaemon are not very helpful neither is the rest of the internet https://appdaemon.readthedocs.io/en/latest/HASS_API_REFERENCE.html#appdaemon.plugins.hass.hassapi.Hass.notify https://appdaemon.readthedocs.io/en/3.0.4/HASS_API_REFERENCE.html

Any help would be appreciated

configuration.yaml

python_script:

automation:
  - alias: x_email_send
    trigger:
      - platform: time_pattern
        seconds: /10

    condition:
      # Run if switch is on
      - condition: state
        entity_id: input_boolean.mail_testing
        state: "on"

    action:
      # Send email from HA python script
      - service: python_script.send_py_mail

      # Send email from here (HA)
      - service: notify.email_info
        data:
          # Email subject line
          title: 'Email From Home Assistant - title'
          # This is REQUIRED, but NOT used in HTML emails
          message: "Email From Home Assistant - message"
          data:
            # HTML email body
            html: >
              <!doctype html>
              <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
                <head>
                  <meta charset="UTF-8">
                  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                  <meta name="viewport" content="width=device-width, initial-scale=1.0">
                </head>
                <body>
                  <h1><b>Email From Home Assistant - HTML Body</b></h1>
                </body>
              </html>

input_boolean:
  mail_testing:
  name: mail_testing
  icon: mdi:sync-alert
  initial: false

notify:
  - name: email_info
    server: !secret email_server1
    sender: !secret email_sender1
    sender_name: !secret email_sender1_name
    platform: smtp
    port: 25
    timeout: 15
    encryption: none
    recipient:
      - !secret email_address1

Then add button entity 'input_boolean.mail_testing' to the UI/dashboard so you can start/stop the emails

/config/python_scripts/send_py_mail.py

notification_title = 'Email From Python Script - title'
notification_body = 'Email From Python Script - body plain'
# Plain text working
hass.services.call('notify', 'email_info', {'title': notification_title, 'message': notification_body})

/config/appdaemon/apps.yaml

send_ad_mail:
  module: send_ad_mail
  class: send_ad_mail

/config/appdaemon/apps/send_ad_mail/send_ad_mail.py

import appdaemon.plugins.hass.hassapi as hass

class send_ad_mail(hass.Hass):
  def initialize(self):
    # Run every 10 seconds
		self.run_every(self.run_callback, self.datetime(),10)

  def run_callback(self, kwargs):
    # Run if switch is on
    if self.get_state('input_boolean.mail_testing') == 'on':
      # Plain text working
      self.notify(notification_body, title = notification_title, name = 'email_info')

      # Alternative plain text working
      #self.call_service('notify/email_info', title = notification_title, message = notification_body)

GigaTech avatar Nov 07 '20 02:11 GigaTech

https://community.home-assistant.io/t/appdaemon-smtp-app/22961/8

ReneTode sorted the question

/config/appdaemon/apps/send_ad_mail/send_ad_mail.py

self.notify('aaa', title = notification_title, name = 'email_info', data = {"html": notification_body})
self.call_service('notify/email_info', message = 'aaa', data = {'html': notification_body})

Both of the above work for HTML emails from AppDaemon

from what I see in the email and internet headers of the received email, is that the ‘message’ field is required but it is not actually used for a HTML message.

This really does need some better documentation... well I guess this is it!

GigaTech avatar Nov 07 '20 02:11 GigaTech

like i stated on the forum, this is about how to translate yaml to python dicts. i dont think its up to AD to learn people that (allthough i am always happy to help people who struggle with it on our discordserver)

ReneTode avatar Nov 08 '20 21:11 ReneTode

For what it's worth, I wrote an App to do this, jump into discord if you are still interested

acockburn avatar Mar 04 '23 01:03 acockburn