How to send mails in OH3?

Tags: #<Tag:0x00007f1751ba4680>

I have tried:

configuration:
config: mail:smtp:6095998bde
type: mail.sendHtmlMail(“c.hof......er@gmail.com”, “Es klingelt an der Haustür”, “Jemand steht vor der Hausür”, “http://10.0.0.51/jpg/image.jpg”)
label: Sendet eine Mail an Hofi

But that ends in error.
In OH2.5 this had sent a screensot from a camera.

You should use code fences as it can be the spacing that stops YAML from working and no one can see what your using based on your post.

Recommend you check out the Telegram binding. You can send photos, animations like a gif and video mp4 files. Plus you can also send questions with buttons that can turn lights or anything in openHAB on and off remotely. More powerful emails.

The V3 binding just got some updates in the latest snapshot builds and you can see the updated documentation here:

1 Like

I can’t be sure but that looks a whole lot like the old Mail Action. I don’t think those old 1.x version Actions were ported over to OH 3. Now Actions like that are installed with the bindings instead of separately. So have you installed the Mail binding and configured an SMPT Thing with your SMTP server’s information and credentials? Then you need to pull the Action associated with that Thing to call sendHtmlMail

val mailActions = getActions("mail","mail:smtp:sampleserver")
mailActions.sendHtmlMail("mail@example.com", "Test subject", "<h1>Header</h1>This is the mail content.", attachmentUrlList)

I’ve posted a tutorial for how to configure it with gmail at How to configure the Mail Binding to use GMail for sending email from openHAB

1 Like

Yes, I installed the binding and set up the SMTP server as “Thing”, it is also online.


Then I created the rule in OH 3 in the UI:
image
Then add the code. To do this, I took the code that worked under OH2
triggers:

  • id: “1”
    configuration:
    itemName: KlingeltasterMQTT_Klingeltaster
    state: ON
    type: core.ItemStateChangeTrigger
    conditions: []
    actions:
  • inputs: {}
    id: “2”
    label: Doorbell Büro
    configuration:
    sink: chromecast:chromecast:b2ac767c16ed70bc25bdf4e581062ad4
    sound: doorbell.mp3
    type: media.PlayAction
  • inputs: {}
    id: “3”
    label: Doorbell Bad
    configuration:
    sink: chromecast:chromecast:684a1f597a61ec0ab6befdcc93ecb59f
    sound: doorbell.mp3
    type: media.PlayAction
  • inputs: {}
    id: “4”
    label: Doorbell Couch
    configuration:
    sink: chromecast:chromecast:3ed70b5dca4fc33e8e31aef499e592b1
    sound: doorbell.mp3
    type: media.PlayAction
  • inputs: {}
    id: “5”
    label: Doorbell Ruheraum
    configuration:
    sink: chromecast:chromecast:275cfb4fba2f082c07046848f1e0e4b3
    sound: doorbell.mp3
    type: media.PlayAction
  • inputs: {}
    id: “6”
    label: Sendet eine Mail an Hofi
    configuration:
    config: mail:smtp:6095998bde
    type: mail.sendHtmlMail(“c.ho......er@gmail.com”, “Es klingelt an der Haustür”, “Jemand steht vor der Hausür”, “http://10.0.0.51/jpg/image.jpg”)

The rule then immediately shows an error message when I enter the last part (mail-#@address, subject, link).

INVALID_RULE
Validation of rule 0ca79312a0 has failed! Action Type "mail.sendHtmlMail(“c.ho......er@gmail.com”, “Es klingelt an der Haustür”, “Jemand steht vor der Hausür”, "http://10.0.0.51/jpg/image.jpg")" does not exist!

The link isn’t the reason, it doesn’t work like this either:
“mail.sendHtmlMail(“c.hof.....tter@gmail.com”, “Es klingelt an der Haustür”, “Jemand steht vor der Hausür”, “du nervst”)” does not exist!

OK, so you are using the mail action directly and not from a Script Action. The best I can offer at this point is to file an issue on the binding and see where that goes. I’ve not done that yet so I don’t have any experience to draw from.

I’m too stupid, now I’ve tried it that way:
Script:

val mailActions = getActions(“mail”,“mail:smtp:Google-SMTP-SRV”)
mailActions.sendMail(“c.hofstaetter@gmail.com”, “Es klingelt an der Haustür”, “Jemand steht vor der Hausür”, “http://10.0.0.51/jpg/image.jpg”)

but I get an error message again:

2020-11-13 18:11:12.553 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘0ca79312a0’ failed: :1:4 Expected ; but found mailActions
val mailActions = getActions(“mail”,“mail:smtp:Google-SMTP-SRV”)
^ in at line number 1 at column number 4

This is my SMTP-Server thing:

Click on the code tab of your rule and post the full yaml (in code fences please).

I hope I got the right code fences now, sorry:

triggers:
  - id: "1"
    configuration:
      itemName: KlingeltasterMQTT_Klingeltaster
      state: ON
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    label: Doorbell Büro
    configuration:
      sink: chromecast:chromecast:b2ac767c16ed70bc25bdf4e581062ad4
      sound: doorbell.mp3
    type: media.PlayAction
  - inputs: {}
    id: "3"
    label: Doorbell Bad
    configuration:
      sink: chromecast:chromecast:684a1f597a61ec0ab6befdcc93ecb59f
      sound: doorbell.mp3
    type: media.PlayAction
  - inputs: {}
    id: "4"
    label: Doorbell Couch
    configuration:
      sink: chromecast:chromecast:3ed70b5dca4fc33e8e31aef499e592b1
      sound: doorbell.mp3
    type: media.PlayAction
  - inputs: {}
    id: "5"
    label: Doorbell Ruheraum
    configuration:
      sink: chromecast:chromecast:275cfb4fba2f082c07046848f1e0e4b3
      sound: doorbell.mp3
    type: media.PlayAction
  - inputs: {}
    id: "7"
    configuration:
      type: application/javascript
      script: >-
        val mailActions = getActions("mail","mail:smtp:Google-SMTP-SRV")

        mailActions.sendMail("c.hof.....ter@gmail.com", "Es klingelt an der Haustür", "Jemand steht vor der Hausür", "http://10.0.0.51/jpg/image.jpg")
    type: script.ScriptAction

OK, I think the problem here is you chose ECMAScript (aka JavaScript) as the language for the Script Action but the code is Rules DSL. I don’t know if you can change the language after the fact or not. You might need to delete it and recreate it. this time select “Rules DSL (v1)” as the “script type”.

Then you can paste int the same code and it will at least be the right syntax.

1 Like

Thank you very much, the script is now functional better, only e-mails are still not being sent:
There is only a meaningless error message in the log and I am unable to set the mail binding to DEBUG logging:

2020-11-14 10:39:00.328 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '0ca79312a0' failed: null

I have now switched to “App Password”, but it doesn’t help either.
My rule now looks like this:

triggers:
  - id: "1"
    configuration:
      itemName: KlingeltasterMQTT_Klingeltaster
      state: ON
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    label: Doorbell Büro
    configuration:
      sink: chromecast:chromecast:b2ac767c16ed70bc25bdf4e581062ad4
      sound: doorbell.mp3
    type: media.PlayAction
  - inputs: {}
    id: "3"
    label: Doorbell Bad
    configuration:
      sink: chromecast:chromecast:684a1f597a61ec0ab6befdcc93ecb59f
      sound: doorbell.mp3
    type: media.PlayAction
  - inputs: {}
    id: "4"
    label: Doorbell Couch
    configuration:
      sink: chromecast:chromecast:3ed70b5dca4fc33e8e31aef499e592b1
      sound: doorbell.mp3
    type: media.PlayAction
  - inputs: {}
    id: "5"
    label: Doorbell Ruheraum
    configuration:
      sink: chromecast:chromecast:275cfb4fba2f082c07046848f1e0e4b3
      sound: doorbell.mp3
    type: media.PlayAction
  - id: "6"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-
        val mailActions = getActions("mail","mail:smtp:SMTP_Server") 

        mailActions.sendMail("c.hof.....ter@gmail.com", "Es klingelt an der Haustür", "Jemand steht vor der Hausür", "http://10.0.0.51/jpg/image.jpg")
    type: script.ScriptAction

I hope I don’t need 2 days for each of my rules to switch to OH 3 :slight_smile:

At this point all I can offer is to file an issue. There is nothing else obviously wrong. I suggested trying the script action to see if the original problem occurs there’s too, which seems to be the case.

1 Like

It works, thank you very much Rich, without your help I would search for another 2 days.
I would always have entered the label of the mail server:

val mailActions = getActions(“mail”,“mail:smtp:SMTPServer”)

But if I enter the identifier it works:

val mailActions = getActions(“mail”,“mail:smtp:27b83f35ab”)

I tried that yesterday, but the wrong script language was selected. Now that the script language fits, thanks to your help, it worked immediately after switching to the identifier.

1 Like

Thanks for pointing this out, I’ll definitely take a look at this. The function with the button in the message has charm. Since I run smart locks on OH, I could just use them to open the door when someone I like rings the bell.

Hello all, I’m really interesting with realizing simple rule only with UI (and thus without my buggy scripts) but I really don’t find how to send mail from the action part of the rule editor.

At the top of this topic (Nov 13, 2020), there is a print screen that show the user can select to send mail in the actions through a thing. I’ve tryed many thing but it’s impossible for me to choose sendMail… I just have following list :

By the way, I fully succeed to send mail with scripts which indicate that the mail binding is correctly configured.

That never worked. Actions that can’t work are no longer listed. It’s never been possible to send an email without using a Script Action and doing it in code.

1 Like

OK. Thank you for your help. It is good to know that is not my fault ! :stuck_out_tongue_winking_eye: