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

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
16 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.

My problem was that I had forgotten the port. I hope it works now then. This is easy to overlook because you have to click Show More before.

How do I know if STARTTLS would be the correct setting? Can I set something accordingly there at gmail or is that “default”?

If it works it’s the right setting. If you are using port 587 you must use STARTTLS. I don’t think anything else works on that port. If using the other port (400 something) GMail offers I don’t know what the correct setting is. I do not recommend using the 400 port though as it’s only SSL. TLS is more secure and I think Google will probably eventually drop support for the 400 port anyway.

You are using GMail’s SMTP server and these settings are dictated by the server. You don’t have any control over them.

I add two more things, as they are probably errors that may occur more often. In the Karaf Console, the following error may occur:

My log is getting permanent spammed with failure : [ERROR] [xbase.resource.BatchLinkableResource] - resolution of uriFragment ‘|::0.2.0.2.0.3.6.1.7::0::/1’ failed

The solution to this can be found here:

[SOLVED] I having trouble new mail binding

I have solved this as follows for me:

val mailActions = getActions("mail","mail:smtp:<YOUR_THING_ID>")

rule "<YOUR_RULE_NAME>"
when
    <YOUR_TRIGGER>
then
    ...
    var receiver="<MAIL_TO@YOUR_RECEIVER>"
    var title="<YOUR_TITLE>"
    var text="<YOUR_TEXT>"
    mailActions.sendMail(receiver, title, text)
    ...
end

I have seen some users have problems trying to save mailActions outside the rule as a global like that. I recommend moving that line into the rule(s) where it’s used. There can be some timing issues where the Mail Thing is not yet initialized when the .rules file is loaded. As a result the action may not exist at that time and the call will not work until the .rules file is reloaded.

1 Like

This worked first time for me, which is somewhat astonishing.

For the record, in Javascript I used this code:

where the second parameter of getActions is the Identifier in the SMTP thing.

@LordLiverpool, I’m glad it worked for you. My script has changed slightly.

For anyone else trying to figure this out, you can use RulesDSL or Javascript (ECMAScript) but I use Javascript because I need to use that anyway, for more complex rules.

This is what I use now. Note that “SMTP_Server” is what I name my SMTP server Thing when installing it, and of course email@myemail.com would be replaced with your info:

var Actions = Java.type("org.openhab.core.model.script.actions.Things");
var mailActions = Actions.getActions("mail","mail:smtp:SMTP_Server");
mailActions.sendHtmlMail("email@myemail.com", "Subject", "Text");
1 Like

Looks like my gmail started failing:

2022-06-08 16:52:14.348 [WARN ] [ab.binding.mail.internal.SMTPHandler] - Sending the email to the following server failed : smtp.gmail.com:587
2022-06-08 16:52:14.349 [WARN ] [ab.binding.mail.internal.SMTPHandler] - javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted.

Do i need to uninstall/reinstall mail binding? OH 2.5.11

Figured it out; your instructions worked; seems i mispasted it?

With OpenHAB 4, the mail setup is slightly different, but the configuration options are still available. I have installed the Mail Binding and setup an SMTP Thing with it. I have defined the parameters as listed above and I am using a GMail application password. However, login is not possible, the log shows the following error:

2023-10-15 22:19:12.050 [WARN ] [ab.binding.mail.internal.SMTPHandler] - javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at

535 5.7.8 Can't sign in to your Google Account - Gmail Help o3-20020a05600c510300b004065d67c3c9sm5399840wms.8 - gsmtp

(The link in the error message does not work, though.)
I have set all parameters as defined above .Is this method known to still work with OH4 and the mail binding?

Thanks, Torstn

If something has changed, it’s on the Google side of things, not the OH dude if things. Nothing has changed in OH 4.

But I did just connect to Gmail the other day, not from OH but using the same application password config in Gmail and it worked.

Did you use your FULL email address for the smtp user?
The example above doesn’t show that.
I am using OH4 and gmail and it works well.

It is working now, I had set the application password for the wrong account. I am sorry and thank you for your help!