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.
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.
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.
rule "Send alert" when Item Alert received command then getActions("mail", "mail:smtp:gmail").sendMail(email, "openHAB Alert", receivedCommand.toString) end