JavaScript cannot attach file to email

I am using Openhab 3 and have installed the mail binding.
I can send emails successfully without attachments but cannot send with an attachment.

Here is what I have done that doesn’t work.

message = "Someone is at the front door at " + Date();

var attachmentUrlList = "file:///tmp/image.jpg" ;

actions.get("mail", "mail:smtp:1032da1f").sendHtmlMail("", "openHAB 3 front door bell", message , attachmentUrlList);

This is the error I am seeing:
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘8e1e6893d6’ failed: TypeError: Can not invoke method [jdk.dynalink.beans.SimpleDynamicMethod Boolean org.openhab.binding.mail.internal.action.SendMailActions.sendHtmlMail(String,String,String)] with the passed arguments; they do not match any of its method signatures. in at line number 96

I must be missing something obvious as sending email attachments should be something that is used everyday.

I have looked at the mail examples and they didn’t work.


Same problem as here, I think

The challenge is to create the correct type of Java list object for the mail action.

Reading the link you referred to sounds like it can’t be done?
That is probably why I can’t find any information about attachments.
I would have thought this would have been an “out of the box” option.

Looks like I will have to do an exec to the server email command line.


It is “out of the box”, for the old style DSL rules as shown in the example.
You’ve chosen to write rules in javascript, which is at the moment a little less popular and well supported, so you get more of an “adventure” factor.

I would have expected the List creating method shown in the linked thread to have worked. Have you tried it?
That’s the extent of my javascript knowledge. You need someone else who’s figured out the knack here.

If you are desperate to make it work, you can used multiple rule languages at once. You can write a DSL rule just for this job.

Yes I tried it and no I can’t get it to work.

Can you get it to work?
Does it work?

I just wanted to use JavaScript only if I can. I know I can mix DSL.
I can run an exec eternal email if I have to but it would be nice if the JavaScript could do it.


It looks like the function name(s) to send messages with attachments may have changed but the documentation wasn’t updated.


actions.get("mail", "mail:smtp:xxxx").sendHtmlMailWithAttachment("", "Subject", "Message", "file:///some/path/to/a/file.jpg");


actions.get("mail", "mail:smtp:xxxx").sendHtmlMailWithAttachments("", "Subject", "Message", arrayListOfStrings);

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.