# OH4 sending email as html with attachent javascript not DSL

**URL:** <https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951>\
**Category:** Off-Topic\
**Created:** [August 22, 2023, 9:14am UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951 "2023-08-22T09:14:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [August 22, 2023, 9:14am UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/1 "2023-08-22T09:14:07Z")

</div>

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:

```auto
//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:

```auto
[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:

> **[Mail - Bindings](https://www.openhab.org/addons/bindings/mail/)**
>
> The Mail binding provides support for sending mails from rules.

Thanks

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 22, 2023, 3:25pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/2 "2023-08-22T15:25:51Z")

</div>

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?

> [@ubeaut](#):
>
> `javax.activation.UnsupportedDataTypeException: text/plain; charset=UTF-8`

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.

```auto
//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.

---

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [August 22, 2023, 10:32pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/3 "2023-08-22T22:32:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 22, 2023, 10:34pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/4 "2023-08-22T22:34:53Z")

</div>

> [@ubeaut](#):
>
> Strange the sendHtmlMail works and sends html email but the one with attachments doesn’t: sendHtmlMailWithAttachments

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

---

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [August 22, 2023, 10:52pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/5 "2023-08-22T22:52:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [August 22, 2023, 11:51pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/6 "2023-08-22T23:51:45Z")

</div>

Searched for raise issue. Found it.

> [@How to file an Issue](https://community.openhab.org/t/how-to-file-an-issue/68464):
>
> There will be times when you discover a bug in OH. This tutorial explains how to file an issue so the developers can know about the bug and hopefully fix it. Do I Have a Bug? openHAB is a complex system with lots of options and ways to configure various capabilities. As the old saying goes, openHAB gives you a lot of rope with which to hang yourself. So how do I as an average user know whether I’ve done something wrong or if there is a bug? The good news is we don’t expect you. Step 1: Ask fo…

---

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [August 23, 2023, 12:11am UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/7 "2023-08-23T00:11:34Z")

</div>

> [@rlkoshak](#):
>
> I think it’s worth filing an issue. It should work and I suspect it will be an easy fix over all.

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

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 23, 2023, 12:40am UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/8 "2023-08-23T00:40:42Z")

</div>

@ubeaut could you please paste your final code here?

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

```ruby
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.

---

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [August 23, 2023, 1:45am UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/9 "2023-08-23T01:45:41Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 23, 2023, 1:52am UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/10 "2023-08-23T01:52:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 23, 2023, 1:12pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/11 "2023-08-23T13:12:23Z")

</div>

> [@jimtng](#):
>
> @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?

---

<div class="post-metadata">

**Author:** ![jimtng](https://community.openhab.org/user_avatar/community.openhab.org/jimtng/32/98543_2.png) [@jimtng](https://community.openhab.org/u/jimtng)\
**Post date:** [August 23, 2023, 1:16pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/12 "2023-08-23T13:16:43Z")

</div>

> [@rlkoshak](#):
>
> What versions of OH are the two of you running?

I’m using the latest snapshot.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 23, 2023, 1:59pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/13 "2023-08-23T13:59:07Z")

</div>

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.

```auto
  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.

---

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [August 23, 2023, 10:30pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/14 "2023-08-23T22:30:18Z")

</div>

Yes I am also running OH 4.0.2

---

<div class="post-metadata">

**Author:** ![system](https://community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/6/6a52f2ac5d00e4c7b50471a354b3a9221188be90.png) [@system](https://community.openhab.org/u/system)\
**Post date:** [October 4, 2023, 2:31pm UTC](https://community.openhab.org/t/oh4-sending-email-as-html-with-attachent-javascript-not-dsl/148951/15 "2023-10-04T14:31:05Z")

</div>

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