SMTP to MQTT Add-on

Home Assistant SMTP to MQTT Add-on

About

I had a need recently to parse the contents of emails from one of my devices and get them into Home Assistant to work with them but couldn’t find anything that could offer this functionality, so I made an add-on.

The gist is if you have an IoT device that is limited, like maybe a camera that that can only send motion alert emails or something similar, this add-on will let you configure that device’s SMTP settings such that your Home Assistant IP is the SMTP server and it will send its emails to your Home Assistant and this add-on will publish them to an MQTT broker (presumably the well-known Mosquitto add-on, or any other broker) where you can then create mqtt sensor(s) that parse the contents with a value template & do something with it.

It’s fairly simple, does one job, the complexities are left to templating in Home Assistant.

Installation

Installation is straight forward:

  1. I had to make my own add-on repository since I guess the Community Add-ons project isn’t accepting any more at the moment. Just add this URL as an add-on repository in your Home Assistant (Settings → Add-ons → Add-On Store → three dots upper right): https://github.com/bcastellucci/addons
  2. Click on this add-on & click the Install button.
  3. All the configuration is optional, if you are already using the Mosquitto add-on then the only thing you may need is to set a MQTT user and MQTT password. The documentation explains all the available configuration options as well as the add-on behavior in pretty decent detail.
  4. Click the Save button.
  5. Start the add-on.
  6. Check the logs to make sure it started OK.

As I mentioned above, this is dead-simple and I wasn’t really planning to actively entertain features, etc.
If there is some egregious issue then post here (or file an issue on GitHub) and I’ll take a look when I have time.

This is something I needed, I find it useful and I wanted to share, if it looks useful to anyone feel free!

3 Likes

Tagging:

@perozonp

I came across their post in my initial search:

Tiny Email server incorporated into Home Assistant

Where it seems this is exactly what they are after but it is archived and I can’t reply to it to see if they still need it.

I seem to be having trouble getting this to work. I have it installed but my reolink cameras are giving me an error 400 when I try to send a test message, likely my camera is doing something weird.

If anyone is getting this to work with a Reolink Argust PT or other Reolink battery cameras, can you offer some tips? (besides throwing them in the trash…)

Try turning on debug logging in the addon, then if you have a windows pc handy on the same network as your home assistant, open a powershell prompt and type:

Send-MailMessage -SmtpServer [IP address of your Home Assistant without these square brackets] -from [email protected] -subject "Test Message" -body "hello, this is a test" -to [email protected]

and hit enter, then go to the log tab of the addon and refresh (if necessary), you should see the mail come in and get published, this will at least tell you if the addon is working as expected.

Then go back to the Reolink config and verify your home assistant IP address is the smtp server and the smtp port is 25 and there is NO username/password or TLS info set (this is bare bones smtp).

I have a bunch of Reolink IP/PoE cameras myself and I use a different, but very similar python based smtp server I wrote to parse their motion alert emails, this should work identical, but I’ll admit I do not have the battery powered Reolink cameras, I have read they behave differently in a lot of cases from the IP/PoE ones, however SMTP is SMTP, this should work.

Good luck!

I was able to get the PowerShell test to work. Publishes MQTT perfectly and generates a clear log.

Doing the exact same settings via the Reolink app and the Reolink client for Windows and still getting an error 400 with no logs generated in the add-on.

I’ll look at my WiFi to see if that’s the culprit.

I have the same issue. in my case I have Hiseeu NVR, but it’s failing with “incorrect user or password” even though there is no such config in SMTP2MQTT. From powershell all fine.

Did some more testing. I tried it with my Reolink RLC-511W cameras and I do get a successful test message, but unfortunately, I still can’t get the Argus PT to send the message.

I’ve tried resetting the camera, using a different WiFi network, putting the camera next to the router…nothing. No logs generate and always an error 400. I also tried to do a test using Gmail as the SMTP and that also produced the same error.

Given my experience, I’m under the strong suspicion that Reolink is lying about SMTP capabilities on the Argus PT cameras or that their current firmware has broken SMTP. Anyway, I hope this info is helpful to anyone with a Reolink Argust PT camera – don’t waste your time.

@carlosCastro99

The only thing I can think of is if maybe you previously had your cameras sending emails and when you switched them to sending to this addon you may have left a password in there or maybe a TLS setting.

Basically in your camera config you would want to set the SMTP server to the IP address of your home assistant, the SMTP port to 25 and maybe a sender address (whatever you had in there before should be fine) and NOTHING ELSE (clear out anything else).

If that still isn’t working (but obviously the powershell test does) then it must be some funny business in the camera.

Good luck!

This is a pretty great idea. But my reolink cameras require a email and password, I can’t just put an SMTP server. Did I miss a step?

I also have Reolink cameras with their newer web interface and a Reolink NVR with their older web interface, neither one requires a password.

Here is how I have the fields set

Newer web interface:

Device Settings (gear icon) -> Surveillance -> Email -> Email Settings (expand):

    SMTP Server: Custom (this reveals additional fields)
    SMTP Server: IP address of home assistant
    Enable SSL or TLS: <off>
    SMTP Port: 25
    Sender Name: a suitable display name for the FROM (this can be anything you want, like Front_Door_Cam)
    Sender Address: a suitable email address for the FROM (this can also be anything you want, like [email protected])
    Password: <empty>
    Recipient Address 1: my own email address
    ...
    Email Content: Text with Picture (or just Text if all you want is the alert itself)
    Interval: 30 seconds (this is the smallest interval they offer, whichever interval you prefer)

Older web interface:

Device Settings (gear icon) -> Network -> Advanced -> Email:

    Enable SSL or TLS: <unchecked>
    SMTP Server: IP address of home assistant
    SMTP Port: 25
    Sender Name: a suitable display name for the FROM (this can be anything you want, like Front_Door_Cam)
    Sender Address: a suitable email address for the FROM (this can also be anything you want, like [email protected])
    Password: <empty>
    Recipient Address 1: my own email address
    ...
    Attachment: Attach Picture (or No Attachment if all you want is the alert itself)
    Interval: 30 seconds (this is the smallest interval they offer, whichever interval you prefer)

Hopefully this helps!

Thank you for sharing this add-on!
My reolink Argus 3 Pro is not accesting an empty password too.
is this a way to make the addon using a username/password?

Ok, to try & help folks with devices that insist on setting a password I added a new config option, SMTP Auth Required. It’s a toggle, just turn it on in the config and then set whatever password you like in your device config, it is just a dummy auth that always returns success so it doesn’t matter what you put as the password.

The updated version is 1.0.1, home assistant should alert you that it’s available & offer to update, if not then check for updates in the addon store.

Let me know how it goes.

Works like a charm!
Thank you very much

No problem, glad to hear it is working for you. :slight_smile:

sorry for my question but I’m not very expert … I configured everything and from the log I see the test email that my Reolink camera sends … but then how can I see the notification via Homeassistant? I would like to see the image of the motion that is sent to me. i don’t understand what i have to do after configuring the addon

It’s not a complete, turn-key solution, rather it is a way to get the information into home assistant then you have to figure out how you want to use it.

I assume you have the Mosquitto MQTT addon going and the email is published to it?

If so then home assistant can define sensors directly from MQTT topics, home assistant can also define automations that use MQTT topics as their triggers. There’s endless things that can be done once its published to MQTT.

I’ll give you an example of how I currently use it. I read the docs on notifications with attachments & decided to try it myself…

First thing I made sure to turn on ‘Save Attachments’ in this addon. This will save any attachments to the file system, by default under /share/smtp2mqtt/.

Next thing I made sure to configure the media_source integration and add a second folder, like so:

media_dirs:
    local: /media
    smtp2mqtt: /share/smtp2mqtt

The first one is the default, it’s just there for verbosity, the second is the one I need. This goes into the main configuration.yaml, under the main homeassistant: section (be sure to restart after adding it).

The next thing I did was define an automation to send a notification to my android phone and attach the image from the email to it, but I had to do it in yaml because it is a little too complex for the GUI:

alias: Reolink Motion Notification
description: Notify's my phone when there is motion detected from the cameras
trigger:
  - platform: mqtt
    topic: smtp2mqtt/[email protected]
condition:
  - condition: template
    value_template: "{{ trigger.payload_json.mime_parts | length > 1 }}"
  - condition: template
    value_template: "{{ trigger.payload_json.mime_parts[1].best_guess == 'attachment' }}"
action:
  - service: notify.mobile_app_[my_device]
    data:
      title: Reolink Camera
      message: "{{ trigger.payload_json.headers.subject }}"
      data:
        image: >-
          {{trigger.payload_json.mime_parts|selectattr('saved_file_name','defined')|map(attribute='saved_file_name')|first|replace('/share','/media')}}
mode: single

This isn’t perfect and the image will be cropped by the phone to a 2:1 but it works, pretty well, and should give you ideas about other ways you can use this.

Good luck!

Thanks for your huge help. I will try to configure everything…just one last thing about the Log. I think there is some error even though I seem to receive the email.

 warn("Requiring AUTH while not requiring TLS "
auth_required == True but auth_require_tls == False
2023-02-23 16:29:58,452 - smtp2mqtt - INFO - main thread - dummy-authenticating whatever credentials are offered...
Session.login_data is deprecated and will be removed in version 2.0
2023-02-23 16:29:58,546 - smtp2mqtt - INFO - ca2db341 - Message from [email protected]
2023-02-23 16:29:58,548 - smtp2mqtt - DEBUG - ca2db341 - Message data (truncated): From: ""<[email protected]>
To: <[email protected]>
Subject: =?UTF-8?B?VGVzdCBlLW1haWw=?=
Mime-Version: 1.0
Content-Type: multipart/mixed;
    boundary=PartBoundary12345678

--PartBoundary12345678
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

SWYgeW91IHJlY2VpdmUgdGhpcyBlLW1haWwgeW91IGhhdmUgc3VjY2Vzc2Z1bGx5IHNldCB1cCBhbmQgdGVzdGVkIHRoZSBlLW1haWwgYWxlcnQgZnJvbSB5b3VyIElQQw==

--PartBoundary12345678--

2023-02-23 16:29:58,600 - smtp2mqtt - DEBUG - ca2db341 - got message header From: [email protected]
2023-02-23 16:29:58,600 - smtp2mqtt - DEBUG - ca2db341 - got message header To: [email protected]
2023-02-23 16:29:58,601 - smtp2mqtt - DEBUG - ca2db341 - got message header Subject: Test e-mail
2023-02-23 16:29:58,601 - smtp2mqtt - DEBUG - ca2db341 - got message header Mime-Version: 1.0
2023-02-23 16:29:58,602 - smtp2mqtt - DEBUG - ca2db341 - got message header Content-Type: multipart/mixed; boundary="PartBoundary12345678"
2023-02-23 16:29:58,607 - smtp2mqtt - DEBUG - ca2db341 - got body header Content-Type: text/plain; charset="utf-8"
2023-02-23 16:29:58,607 - smtp2mqtt - DEBUG - ca2db341 - got body header Content-Transfer-Encoding: base64
2023-02-23 16:29:58,612 - smtp2mqtt - DEBUG - ca2db341 - stored [If you receive this e-mail you have successfully set up and tested the e-mail alert from your IPC] as the content
2023-02-23 16:29:58,615 - smtp2mqtt - DEBUG - ca2db341 - Publishing [{"uuid": "ca2db341", "headers": {"from": "[email protected]", "to": "[email protected]", "subject": "Test e-mail", "mime-version": "1.0", "content-type": "multipart/mixed; boundary=\"PartBoundary12345678\""}, "mime_parts": [{"best_guess": "message body", "headers": {"content-type": "text/plain; charset=\"utf-8\"", "content-transfer-encoding": "base64"}, "content": "If you receive this e-mail you have successfully set up and tested the e-mail alert from your IPC"}, {"best_guess": "message body", "headers": {"content-type": "text/plain; charset=\"utf-8\"", "content-transfer-encoding": "base64"}, "content": "If you receive this e-mail you have successfully set up and tested the e-mail alert from your IPC"}]}] to smtp2mqtt/[email protected]
2023-02-23 16:29:59,551 - smtp2mqtt - ERROR - ca2db341 - Failed publishing
Traceback (most recent call last):
  File "/app/smtp2mqtt.py", line 152, in mqtt_publish
    publish.single(
  File "/usr/lib/python3.10/site-packages/paho/mqtt/publish.py", line 240, in single
    multiple([msg], hostname, port, client_id, keepalive, will, auth, tls,
  File "/usr/lib/python3.10/site-packages/paho/mqtt/publish.py", line 176, in multiple
    client.connect(hostname, port, keepalive)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 914, in connect
    return self.reconnect()
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1044, in reconnect
    sock = self._create_socket_connection()
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
    return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
  File "/usr/lib/python3.10/socket.py", line 824, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.10/socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name does not resolve
2023-02-23 16:29:59,571 - smtp2mqtt - DEBUG - ca2db341 - SKIP relaying the original email

OK, you’re getting there… :slight_smile:

Looks like your camera is configured correctly and this addon is working, however it is having trouble publishing to MQTT.

Do you have an MQTT broker running & available? It’s common to just install the Mosquitto addon. If you have that then you’ll need to look at its config and make note of the username and password you configured for it, then come back to the config for this addon and enter those in the appropriate fields, then restart it & try again.

If you don’t have the Mosquitto addon then you’ll need some kind of MQTT broker on your network and you’ll need to enter its host/IP in the config of this addon so it knows where to publish, along with any username & password, etc.

Regardless of which MQTT broker you use, you’ll also need the MQTT integration (so home assistant can incorporate MQTT events & utilize them).

It’s been a while since I have installed the MQTT integration but if I remember right, when you use the Mosquitto addon, installing and configuring the MQTT integration is pretty painless (you may need to make note of that username & password again).

Now I think I have no more errors. I have mqtt installed on docker in debian. I had to enter the home assistant’s ip address. the username and password were already correct

2023-02-23 17:01:53,382 - smtp2mqtt - INFO - main thread - dummy-authenticating whatever credentials are offered...
Session.login_data is deprecated and will be removed in version 2.0
2023-02-23 17:01:53,472 - smtp2mqtt - INFO - f1ef10d7 - Message from [email protected]
2023-02-23 17:01:53,473 - smtp2mqtt - DEBUG - f1ef10d7 - Message data (truncated): From: ""<[email protected]>
To: <[email protected]>
Subject: =?UTF-8?B?VGVzdCBlLW1haWw=?=
Mime-Version: 1.0
Content-Type: multipart/mixed;
    boundary=PartBoundary12345678

--PartBoundary12345678
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

SWYgeW91IHJlY2VpdmUgdGhpcyBlLW1haWwgeW91IGhhdmUgc3VjY2Vzc2Z1bGx5IHNldCB1cCBhbmQgdGVzdGVkIHRoZSBlLW1haWwgYWxlcnQgZnJvbSB5b3VyIElQQw==

--PartBoundary12345678--

2023-02-23 17:01:53,508 - smtp2mqtt - DEBUG - f1ef10d7 - got message header From: [email protected]
2023-02-23 17:01:53,509 - smtp2mqtt - DEBUG - f1ef10d7 - got message header To: [email protected]
2023-02-23 17:01:53,510 - smtp2mqtt - DEBUG - f1ef10d7 - got message header Subject: Test e-mail
2023-02-23 17:01:53,511 - smtp2mqtt - DEBUG - f1ef10d7 - got message header Mime-Version: 1.0
2023-02-23 17:01:53,512 - smtp2mqtt - DEBUG - f1ef10d7 - got message header Content-Type: multipart/mixed; boundary="PartBoundary12345678"
2023-02-23 17:01:53,520 - smtp2mqtt - DEBUG - f1ef10d7 - got body header Content-Type: text/plain; charset="utf-8"
2023-02-23 17:01:53,521 - smtp2mqtt - DEBUG - f1ef10d7 - got body header Content-Transfer-Encoding: base64
2023-02-23 17:01:53,530 - smtp2mqtt - DEBUG - f1ef10d7 - stored [If you receive this e-mail you have successfully set up and tested the e-mail alert from your IPC] as the content
2023-02-23 17:01:53,536 - smtp2mqtt - DEBUG - f1ef10d7 - Publishing [{"uuid": "f1ef10d7", "headers": {"from": "[email protected]", "to": "[email protected]", "subject": "Test e-mail", "mime-version": "1.0", "content-type": "multipart/mixed; boundary=\"PartBoundary12345678\""}, "mime_parts": [{"best_guess": "message body", "headers": {"content-type": "text/plain; charset=\"utf-8\"", "content-transfer-encoding": "base64"}, "content": "If you receive this e-mail you have successfully set up and tested the e-mail alert from your IPC"}, {"best_guess": "message body", "headers": {"content-type": "text/plain; charset=\"utf-8\"", "content-transfer-encoding": "base64"}, "content": "If you receive this e-mail you have successfully set up and tested the e-mail alert from your IPC"}]}] to smtp2mqtt/[email protected]
2023-02-23 17:01:53,546 - smtp2mqtt - DEBUG - f1ef10d7 - SKIP relaying the original email

That looks good to me.

Your emails should be publishing to that MQTT broker now.

All you need is to make sure home assistant is aware of the broker (via the MQTT integration) and then you should be able to start defining sensors, automations, etc. that utilize the topic being published to.