JavaScript cannot attach file to email

YES! That worked. Thank you so much.

I thought I was doing something wrong because I was copying and pasting the examples.

Woah, when did that happen? I’ve been rummaging and can’t find a paper trail. Guess it’s been ever since Actions were rolled into binding V2.

Anyway,let’s not just pass over it …

1 Like

Yes. That was probably wrong from the beginning.


I wonder if this part is wrong as well as I cannot get it to work.

import java.util.List

val List<String> attachmentUrlList = newArrayList(

I am now able to send a single attachment but not multiple…yet.

I have made a bit of progress with trying to send multiple attachments but it fails.

var ArrayList = Java.type('java.util.ArrayList');

message = "<H2>Someone is at the front door at " + Date() + "</H2>";

  attachmentUrlList = new ArrayList();

actions.get("mail", "mail:smtp:1032da1f").sendHtmlMailWithAttachment("", "openHAB 3 front door bell", message , attachmentUrlList);"The array  list " + attachmentUrlList)

The log shows there are array items:

2021-04-20 10:15:52.836 [INFO ] [org.openhab.rule.8e1e6893d6         ] - The array  list [,]

But the email doesn’t send because in the log it says:

2021-04-20 10:15:52.835 [WARN ] [mail.internal.action.SendMailActions] - Could not send mail: no protocol: [,]

I can send a single attachment and that works fine.

I checked and it wa snot wrong from the beginning. Due to limitations in core the action have been renamed in [mail] Added translations for ThingActions by cweitkamp · Pull Request #9121 · openhab/openhab-addons · GitHub but the documentation has not been adjusted.

The only way I have found to send more than one attachment is to send multiple emails with a single attachment. That works.

I have tried different ways to make the multiple attachments send but just cannot get it to work. It only sends one attachment.

Has anyone managed to get it to send multiple attachments on a single email?


The method name for sending multiple attachments is ever so slightly different. It’s sendHtmlMailWithAttachments (plural)

This is tested and works with both local and remote files:

ArrayList = Java.type('java.util.ArrayList');
listOfFiles = new ArrayList();

actions.get("mail", "mail:smtp:testing").sendHtmlMailWithAttachments("", "Subject", "Message", listOfFiles)

you are a rock star :+1:

@naador That was exactly what I had but was missing the s for plural of attachments.
Thanks for the solution. I have spend weeks trying to get this to work.

I swear I stared at your code for at least 5 minutes trying to figure out why mine worked and yours didn’t. I usually only get to do that with my own :yum:

That’s why copy and paste was invented… :slight_smile:
I thought I was doing something wrong. My thoughts were surly this can’t be so hard and am I the only person trying to send multiple emails using JavaScript?

Anyway glad it is working.

1 Like

Good Lord, that’s just bonkers for users.


I have upgraded to OH 3.1.0.RC1 and now the email with attachments doesn’t work anymore.
It used to work on the stable release.

Here is the error I get.
2021-06-27 14:00:31.302 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘a118e0e70d’ failed: TypeError: null has no such function “sendHtmlMailWithAttachments” in at line number 329

I removed the old mail binding and installed the 3.1.0.RC1 binding but still doesn’t work.

EDIT: I have worked out what the issue is. The UUID had changed when upgraded.
It used to be this: mail:smtp:1032da1f but now it is this: mail:smtp:a514b96247
It works now.
I will have to go through my rules and find where else I use the send email.

Does this mean every time there is an upgrade I will have to go through and change all my rules? or it was because I removed and re-installed the binding?
I had about 10 rules (I din’t think I used it that much) I had to change.
All good now though.
I just cd to /var/lib/openhab/jsondb
grep 1032da1f *
Then less automation_rules.json
Then searched for 1032da1f in that file and found the rules to change.
Then I changed the rules via the UI.

If I have to re-install the binding again after removing I will have to remember to make the new binding the same as the old binding UUID. E.G mail:smtp:a514b96247 and if I do that then I won’t have to go and change all my rules.

1 Like

You could give your Thing a memorable name when you first create it, like
and use that in your rules. As you say, if you re-use the same UID when some circumstance forces you to create a new Thing, then it will “re-attach” to existing rules and Items.
You can’t edit the UID, only choose at moment of creation.

Yes I thought of this AFTER I went through all the rules and changed them. :sob:

Method names reference here: openhab-addons/ at main · openhab/openhab-addons · GitHub

In summary, as of 2021-07-22:

Command Arguments
sendMail; sendHtmlMail String recipient, String subject, String text
sendMailWithAttachment; sendHtmlMailWithAttachment String recipient, String subject, String text, String url
sendMailWithAttachments; sendHtmlMailWithAttachments String recipient, String subject, String text, List<String> urlList
1 Like

I was just doing some other work on the mail bonding. Shall I add the above table to the docs?