How to send email from rule in OH3?

I don’t know at all how it was done in OH2, and I didn’t find such an option in the UI of OH3. I did find some examples in the documentation like:

val mailActions = getActions(“mail”,“mail:smtp:samplesmtp”)
val success = mailActions.sendMail(“mail@example.com”, “Test subject”, “This is the mail content.”)
success = mailActions.sendMail(“mail1@example.com, mail2@example.com”, “Test subject”, “This is the mail content sent to multiple recipients.”)

Unfortunately, I don’t know where I should write this. It will probably be a script, but I don’t know the syntax and how to work with it. Is this described somewhere?

Install the binding. Configure the Thing based on the documentation for the binding. Then you can use those lines of code in a rule.

Binding is instaled and configured (At least I think so…)

I don’t know where I should write the code.

Could you please tell me what specifically and where to write? It’s not very clear in the UI items.
thank you in advance Jan

You’d probably want to write a rule.
First task, when do you want it all to happen? This allows you to choose a rule trigger.

So I studied the recommended chapters. Unfortunately, they are nebulous from my point of view.

I got the impression that I could write some of the elements as a script:

But it doesn’t work - see error message

Therefore, please once again, would you find someone here who could clearly and unambiguously tell me how and where to write to make it work.

for this kindness, I deeply bow down to the benefactor in advance.
best regards Jan

You are correct. That’s where you would call these actions. In a Rule. A Script is just a special type of rule.

As for what’s wrong? :person_shrugging: You’ve cut off the logs and it’s really hard to read screen shots. Please paste the text into your replies using code fences:

```
code/logs/configs go here
```

Where there is a “Code” tab in the upper right, click on that and paste the text you find there instead of screen shots.

Some of the obvious problems:

  • there’s a typo in the thing ID in the call to getActions
  • the Thing is not ONLINE

Code of thing SMTP server:

UID: mail:smtp:b5b5cdc2e4
label: SMTP Server
thingTypeUID: mail:smtp
configuration:
  hostname: smtp.lam.cz
  security: PLAIN
  port: "25"
  sender: openhab@lam.cz

Code of rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: PohybBarta_PohybBarta
      state: "1"
      previousState: "0"
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-
        val mailActions = getActions(“mail”,“mail:smtp:b5b5cdc2e4”)

        val success = mailActions.sendMail(“jbarta@lam.cz”, “Poplach schody !”, “Narušení chráněného prostoru”)
    type: script.ScriptAction

I think the problem is at a higher level. Openhab reports an error no matter how I formulate the command. When I type:

getActions("mail", "mail:smtp:lamsmtp").sendMail(“jbarta@lam.cz”, "Poplach schody !", “Narušení chráněného prostoru”)

Appears in the log:
2022-11-05 08:52:53.316 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘9bcbd5b01d’ failed: getActions(“mail”, “mail:smtp:lamsmtp”).sendMail(“jbarta@lam.cz”, “Poplach schody !”, “Narušení chráněného prostoru”)

When I write it differently, it appears in the log:

val mailActions = getActions(“mail”,“mail:smtp:b5b5cdc2e4”)
val success = mailActions.sendMail(“jbarta@lam.cz”, “Poplach schody !”, “Narušení chráněného prostoru”)
success = mailActions.sendMail(“jbarta@lam.cz”, “Poplach schody !”, “Narušení chráněného prostoru”)

2022-11-05 08:54:41.054 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘14b68b8223’ failed: val mailActions = getActions(“mail”,“mail:smtp:b5b5cdc2e4”)

It seems to me that Openhab does not accept any code. Could it be a missing component in some general setting?

I don’t know where you copy/pasted that from, and how well it represents what you actually have, but those are the “wrong” kind of quotemarks, and will break if used in a real rule.

“incorrect quotemarks”
"correct quotemarks"

Yes, sir, you hit the nail on the head!!!
I copied it from the Openhab documentation and replaced it with real data - so I wouldn’t have dreamed of it…

Thank you very, very, very much for the great observation!!!
As I promised, I bow to you…