How to send mail from Home-Assistant docker?

I am trying to make a rule to send out emails from my home-assistant docker image… sadly I can’t get it to work.

What I did:

Added this to docker config:
-v /usr/bin/mailx:/usr/bin/mailx:ro

This is an example which I tried using as a test:

shell_command:
   send_test_mail: echo test | /usr/bin/mailx -s "test" [email protected]

automation:
  - alias: Send mail test
    trigger:
      platform: time
      after: '17:30:00'
    action:
      shell_command.send_test_mail

Does anybody have an idea how to get this working?

PS: I can send out emails from outside the docker image.

Have you tried using the SMTP notification platform instead? Seems like it would be a lot easier and you wouldn’t have to drop to a shell to do it.

Thanks for the advice. Seems like a better solution to use. However I can’t get it to work. :tired_face:

I am running the docker with --net=host option so port 25 should be available to localhost right?

Also tried it with docker IP address like this:

notify:
  - name: send_email_to_myself
    platform: smtp
    server: 172.17.42.1
    recipient: [email protected]

automation:
  - alias: Send test mail
    trigger:
      platform: time
      after: 'xx:xx:xx'
    action:
      service: notify.send_email_to_myself
      data:
        title: 'Test Email'
        message: 'Test'

Open a shell in the running container (e.g. docker exec -ti hass_container /bin/bash) and try to ping/telnet (port 25) your mailhost (172.17.42.1).
If that works, look at the mail logs on the mailhost when you try to send an email from home-assistant.

Sebastian

I can ping:

PING 172.17.42.1 (172.17.42.1): 56 data bytes
64 bytes from 172.17.42.1: icmp_seq=0 ttl=64 time=0.153 ms
64 bytes from 172.17.42.1: icmp_seq=1 ttl=64 time=0.299 ms

Added a -v /usr/bin/telnet:/usr/bin/telnet to docker config and now can use telnet…

telnet 172.17.42.1 25

Trying 172.17.42.1...
Connected to 172.17.42.1.
Escape character is '^]'.
220 mail-server ESMTP Postfix (Ubuntu)
^]
telnet> quit
Connection closed.

So I do get a connection now…

Wondering if the problem lies in my home-assistant config… as connection seems ok this way…

Ok, so the networking part works.
What do the mail logs say? Maybe the mail server does not accept connections from the 172.x.x.x network?
Or you need to add authentication information (username/password)?

You should see an error logged why it’s not accepting the mail from hass.

Sebastian

I see nothing in the /var/log/mail.log on the host. I’ve added 172.x.x.x network in my postfix config just in case.

By the way trying to run mailx in the docker image shows this:

mailx: /lib/x86_64-linux-gnu/libtinfo.so.5: no version information available (required by mailx)
mailx: /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0: version `OPENSSL_1.0.2' not found (required by mailx)

Yeah, I don’t think you can just map the mailx binary into the container filespace and expect that to work.
When you telnet to the mailhost, you should at least see something like:

postfix/smtpd[45474]: connect from unknown[172.x.x.x]

If there’s no mentioning of a rejected email, you could try to send an email via telnet from the container:

# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 my_mailserver ESMTP Postfix
MAIL FROM: me@here
250 2.1.0 Ok
RCPT TO: me@there
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
foo bar baz
.
250 2.0.0 Ok: queued as 51B1520670D9
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

In your case, you should either get a status code of 250 which means the mail was accepted, or an error message with a hint of why it’s not working.

Sebastian

Using SMTP commands I am able to send an email out… sadly no notifications from HASS itself…

Wondering if there is something wrong with the configuration or the SMTP component itself is not working.

Looking at your config, I’d add a sender: just for safety.
Also, there’s a debug: option that might make the component to be more verbose.

Sebastian

Found some errors with the docker logs command:

16-11-13 11:48:00 ERROR (ThreadPool Worker 5) [homeassistant.core] BusHandler:Exception doing job
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/core.py", line 1210, in job_handler
    func(*args)
  File "/usr/src/app/homeassistant/core.py", line 1065, in execute_service
    service_handler.func(service_call)
  File "/usr/src/app/homeassistant/components/notify/__init__.py", line 110, in notify_message
    notify_service.send_message(**kwargs)
  File "/usr/src/app/homeassistant/components/notify/smtp.py", line 138, in send_message
    return self._send_email(msg)
  File "/usr/src/app/homeassistant/components/notify/smtp.py", line 146, in _send_email
    msg.as_string())
  File "/usr/local/lib/python3.5/smtplib.py", line 856, in sendmail
    (code, resp) = self.mail(from_addr, esmtp_opts)
  File "/usr/local/lib/python3.5/smtplib.py", line 533, in mail
    self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
  File "/usr/local/lib/python3.5/smtplib.py", line 153, in quoteaddr
    if addrstring.strip().startswith('<'):
AttributeError: 'NoneType' object has no attribute 'strip'

Looks like it’s indeed missing a sender address.

Sebastian

Finally got it working!!!

Mails were not accepted by postfix at first. They were first rejected (code 550 I believe), so changed the sender to a correct name. After that they appeared to be stuck in the mail queue.

Final solution in my case was to also include username next to the sender address, eg:

notify:
  - name: send_mail_to_myself
    platform: smtp
    sender: root@fqdn-hostname
    username: root@fqdn-hostname
    recipient: [email protected]

Thank you Sebastian for all the help! :+1:

Great news! Glad I could help :slight_smile:

Sebastian