How to configure the Mail Binding to use GMail for sending email from openHAB

Tags: #<Tag:0x00007efec20da698> #<Tag:0x00007efec20da5d0>

Many who want to use the Mail binding want to do so using their GMail account. This tutorial will walk you through how to configure the Mail binding to send mail using the GMail servers.

2-Factor Authentication

If you have 2FA configured on your Google account you will need to generate an app password. If you do not have 2FA configured, go do it now. I have an Android phone so I like to use Phone. It used to be the case that you couldn’t have App Passwords (see below) when using a Security Key but it may be supported now. Personally I use Phone and an Authenticator app as a backup.

App Password

Because you have 2FA enabled, you need to generate an App Password to give openHAB access to your GMail. See https://support.google.com/mail/answer/185833?hl=en for instructions.

Select “Mail” for the app:

Enter “openHAB” for the device and hit generate:


You will get your app password in the yellow box:

NOTE: I deleted this app password before posting this tutorial so it’s useless.

This will be the only time you see the actual password. If you don’t copy the password now and enter into OH you will have to delete this one and generate a new one. So leave this page open until you are ready to enter the password into openHAB.

Install the Binding and configure the Thing

In PaperUI browse to Add-ons -> Bindings -> Mail and install it.

Once installed you need to manually create an SMTP Thing.

Click on Inbox and the blue + icon.

Select “Mail Binding” from the list and then select “SMTP Server” from the next list.

Click on “Show more” and you should see the following:

Enter the following:

  • Name: GMail (or something else meaningful to you)
  • Thing ID: gmail (or something else meaningful to you)
  • Sender: your gmail address (it doesn’t seem to matter if you put something else in here)
  • Server Hostname: smtp.gmail.com
  • SMTP Server Security Protocol: STARTTLS
  • SMTP Server Username: your GMail address
  • SMTP Server Password: your App password generated above, don’t include the spaces
  • Server Port: 587

Click the blue check icon at the top to save the Thing.

Sending Mail from a Rule

Rule Actions in OH 2.x bindings need to be acquired and then called in a Rule. In Scripted Automation Python:

from core.rules import rule
from core.triggers import when

@rule("Send alert")
@when("Item Alert received command")
def send_alert(event):
    (actions.get("mail", "mail:smtp:gmail")
        .sendMail(admin_email,  "openHAB Alert", event.itemCommand.toString()))

NOTE: admin_email is defined in configuration.py per Python helper libraries instructions.

Rules DSL:

rule "Send alert"
when
    Item Alert received command
then
    getActions("mail", "mail:smtp:gmail").sendMail(email, "openHAB Alert", receivedCommand.toString)
end
10 Likes

I get the following warning .

How to solve this.

I have currently disabled 2 factor auth too, allowed access to low security apps and also switched off the firewall etc. also running java 8 version above neccessary requirement.

What openHAB version are you using, and how did you install it?
Searching the community here for PKIX and google for it it always comes down to certificate issues (besides firewall which you’ve already disabled)

openhab version 2.5.11 downloaded from official website for windows

OK, i’ve found this and this

Both suggest disabling antivirus for checking and if that helps you might need to add an exception into your antivirus configuration. Are you by any chance running avast since that was the root cause in the above mentioned articles.

If that doesn’t work, i’m afraid you need to search the web for other hints. It’s clearly not openHAB related and i’m not sure how many here are sending e-mails to gmail from their windows openHAB instance

1 Like

Okay thanks for help. Avast was running in background, so resolved the issue by disabling it.