OH4 sending email as html with attachent javascript not DSL

I have migrated to OH4 from OH 3.4 and everything is working well but I cannot get the email to send as html and with attachments. The only documentation I could find is for DSL.

this code works but I just get the <H2> as plain text. In OH3 it was a header:

//below is how to sent html email with multiple attachemments (NOTE the plural of attachments)
var ArrayList = Java.type('java.util.ArrayList');
message = "<H2>Someone is at the front gate at " + Date() + "</H2>";
attachmentUrlList = new ArrayList();
attachmentUrlList.add("http://192.168.1.164/zm/cgi-bin/zms?mode=single&monitor=1&scale=50");
//attachmentUrlList.add("http://192.168.1.164:8080/static/graphviz.png");
//var attachmentUrlList=("http://192.168.1.164/zm/cgi-bin/zms?mode=single&monitor=7&scale=50");

actions.get("mail", "mail:smtp:a514b96247").sendMailWithAttachments("xxxxxxx@gmail.com", "openHAB 4 front gate IR", message, attachmentUrlList);

But if I change sendMailWithAttachments to sendHtmlMailWithAttachments I get a warning:

[2023-08-22 19:00:14.706 [WARN ] [ab.binding.mail.internal.SMTPHandler] - javax.mail.MessagingException: IOException while sending message;
  nested exception is:
	javax.activation.UnsupportedDataTypeException: text/plain; charset=UTF-8

and the email is not sent.

it used to work in Openhab 3.

I can use it as is and just noe use html in the emails but it should work.

I am looking at this documentation:

Thanks

I’ve never used this.

There shouldn’t be anything different in how this works from Rules DSL and JS. Do you see anything different if you code it as Rules DSL?

If it’s HTML formatted it should use text/html, not text/plain. But you are not setting that so it’s probably a bug in the Mail binding. New OH likely means new libraries which may mean the new library is more strict.

You should be able to simplify the code though.

//below is how to sent html email with multiple attachemments (NOTE the plural of attachments)
var message = "<H2>Someone is at the front gate at " + Date() + "</H2>";
var attachmentUrlList = ["http://192.168.1.164/zm/cgi-bin/zms?mode=single&monitor=1&scale=50",
                         "http://192.168.1.164:8080/static/graphviz.png"];

actions.get("mail", "mail:smtp:a514b96247").sendMailWithAttachments("xxxxxxx@gmail.com", "openHAB 4 front gate IR", message, attachmentUrlList);

JS Scripting should auto translate from the JS Array to Java List.

I have used your simplified code. Thanks

Strange the sendHtmlMail works and sends html email but the one with attachments doesn’t: sendHtmlMailWithAttachments

Anyway, something is better than nothing.

I think it’s worth filing an issue. It should work and I suspect it will be an easy fix over all.

Ok. How and where do I create an issue? I have never done one of those.

I have searched for how to report issue.

Thanks

Searched for raise issue. Found it.

Issue created:
[Mail] email not being sent it using sendHtmlMailWithAttachments option #15482

@ubeaut could you please paste your final code here?

I tried this using jruby and it worked fine for me:

things["mail:smtp:local"].send_html_mail_with_attachments(
  "myemailaddress@gmail.com", "test1",
  "<h2>Hello at #{Time.now}</h2>",
  ["https://m.media-amazon.com/images/I/81LWtC-NwyL._AC_SL1500_.jpg",
   "https://www.google.com/"]
)

adjust mail:smtp:local to your thing id which is mail:smtp:a514b96247

Can you verify that the machine that openhab is running can indeed download from those URLs listed? You can use the HTTP action to test it. Sorry my examples are in JRuby and not JS, but this proves that the mail binding works fine with the URL attachments.

Like I said it does work but not with the HTML option.
Here is the code that works but no HTML:

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

var attachmentUrlList = ["http://192.168.1.164/zm/cgi-bin/zms?mode=single&monitor=1&scale=50"];

//var attachmentUrlList = ["http://192.168.1.164/zm/cgi-bin/zms?mode=single&monitor=1&scale=50",
//                         "http://192.168.1.164:8080/static/graphviz.png"];

actions.get("mail", "mail:smtp:a514b96247").sendMailWithAttachments("xxxxx@gmail.com", "openHAB 4 front gate IR", message, attachmentUrlList);

If you change the above code and replace sendMailWithAttachments with sendHtmlMailWithAttachments it doesn’t work.

Also the sendHtmlMail works as well (obviously without attachments) which is strange.

Yes I can verify the problem occurs on my system too. It doesn’t work on JS but it works from jruby! @rlkoshak could this be a JS bug, somehow?

Anything’s possible but I don’t see how. Everything after actions. is implemented by the original Java Class, not in JS.

Maybe there’s something already fixed?

What versions of OH are the two of you running?

I’m using the latest snapshot.

I think @ubeaut is running 4.0.2.

I can’t imagine how it would work differently between jRuby and JS Scripting unless jRuby is overriding the Java stuff somehow.

In JS Scripting actions.get() is literally just a passthrough to the Java actions Object that gets injected into the rule through the Rule presets.

  get: (bindingId, thingUid) => actions.get(bindingId, thingUid),

The only place where I could see a difference is that we’re passing a JS String instead of a Java String as arguments. But it’s not misunderstanding the Strings. It’s using the wrong mime type in the message. Very strange indeed.

Yes I am also running OH 4.0.2

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.