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();
attachmentUrlList.add("http://192.168.0.164/zm/cgi-bin/zms?mode=single&monitor=6&scale=50");
attachmentUrlList.add("http://192.168.0.164/zm/cgi-bin/zms?mode=single&monitor=7&scale=50");
actions.get("mail", "mail:smtp:1032da1f").sendHtmlMailWithAttachment("myemail@gmail.com", "openHAB 3 front door bell", message , attachmentUrlList);
logger.info("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 [http://192.168.0.164/zm/cgi-bin/zms?mode=single&monitor=6&scale=50, http://192.168.0.164/zm/cgi-bin/zms?mode=single&monitor=7&scale=50]
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: [http://192.168.0.164/zm/cgi-bin/zms?mode=single&monitor=6&scale=50, http://192.168.0.164/zm/cgi-bin/zms?mode=single&monitor=7&scale=50]
I can send a single attachment and that works fine.
@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.
That’s why copy and paste was invented…
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?
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.
You could give your Thing a memorable name when you first create it, like mail:smtp:myMailProvider
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.