openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.88k stars 3.59k forks source link

[Mail] sendMail and sendHtmlFail errors for UI rules #11838

Closed rkoshak closed 2 years ago

rkoshak commented 2 years ago

Expected Behavior

When calling sendMail or sendHtmlMail the email is delivered.

Current Behavior

When calling sendMail the following error occurs:

2021-12-20 14:42:15.581 [WARN ] [ab.binding.mail.internal.SMTPHandler] - Sending the email to the following server failed : smtp.gmail.com:587
2021-12-20 14:42:15.581 [WARN ] [ab.binding.mail.internal.SMTPHandler] - javax.mail.MessagingException: IOException while sending message;
  nested exception is:
        javax.activation.UnsupportedDataTypeException: text/plain; charset=UTF-8

When calling sendHtmlMail the following error occurs:

2021-12-20 14:40:32.423 [WARN ] [ab.binding.mail.internal.SMTPHandler] - Sending the email to the following server failed : smtp.gmail.com:587
2021-12-20 14:40:32.424 [WARN ] [ab.binding.mail.internal.SMTPHandler] - javax.mail.MessagingException: IOException while sending message;
  nested exception is:
        javax.activation.UnsupportedDataTypeException: multipart/alternative; 
        boundary="----=_Part_21_1035614427.1640036431905"

In both cases the code is written in JSScripting through the UI. Based on reports (see https://community.openhab.org/t/mail-binding-errors-sending-mail-unsupporteddatatypeexception-text-plaijn-charset-utf-8/130365) the same error occurs in Nashorn and Rules DSL. But there is one report of it working in a .rules file so perhaps the difference is UI verses text file.

Possible Solution

Steps to Reproduce (for Bugs)

  1. Create a Mail thing

  2. Create a UI rule with a script action. Use JavaScript or Rules DSL as the action language and the following line (obviously change the email as appropriate):

    actions.get("mail", "mail:smtp:redacted").sendMail("redacted@redacted.com", "Test", "Test")

  3. Watch the logs.

Context

I can no longer get alerts from openHAB via email.

Your Environment

runtimeInfo: version: 3.3.0 buildString: "Build #2651" locale: en-US systemInfo: configFolder: /openhab/conf userdataFolder: /openhab/userdata logFolder: /openhab/userdata/logs javaVersion: 11.0.13 javaVendor: Azul Systems, Inc. javaVendorVersion: Zulu11.52+13-CA osName: Linux osVersion: 5.4.0-91-generic osArchitecture: amd64 availableProcessors: 4 freeMemory: 47455416 totalMemory: 855638016 bindings:

Running in Docker.

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/mail-binding-errors-sending-mail-unsupporteddatatypeexception-text-plaijn-charset-utf-8/130365/7

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/openhab-3-2-release-discussion/130327/30

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/mail-binding-unsupporteddatatypeexception-error/130530/3

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/javascript-email-ioexemption/130910/2

csowada commented 2 years ago

Hello @jpg0 and @digitaldan,

this issue is marked as mail binding issue. But I was able to fix the by just removing the line from the jsscripting binding. After that I was able to send mails again.

https://github.com/openhab/openhab-addons/blob/71b4fcbac7af055c4ee11107f73d0c5d98098b16/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java#L111

I think it hase something to do with the change for another class loading issues https://github.com/openhab/openhab-addons/issues/11222

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/mail-binding-errors-sending-mail-unsupporteddatatypeexception-text-plaijn-charset-utf-8/130365/15

OlegAndreych commented 2 years ago

I'm having this issue with the Groovy scripting. So it seems like it's not specific for the JS scripting.

edwolfe3 commented 2 years ago

OH3.2: I installed the Mail binding because i need to get a reliable way to be alerted to an issue. So I was going to send email-text to my phone. However, I get this problem. Two questions: 1) as this seems like the cleanest solution, is there an ETA on a fix? 2) If no fix coming soon, is there a work around, another option, etc. that can be utilized either permanently or temporarily?

kstuken commented 2 years ago

is there a work around, another option, etc. that can be utilized either permanently or temporarily?

I am sure there is better or more elegant solutions. But since I did not want to wait with migrating my rules to JS, I have just built myself a quick and dirty workaround by:

That way, I can send emails from any JS rule by using that string item. Once this bug is fixed, I can then just quickly replace the sendCommand in the JS rule with the respective mailAction command

martychristensen commented 2 years ago

Hi Kstuken, can you post your creation?

is there a work around, another option, etc. that can be utilized either permanently or temporarily?

I am sure there is better or more elegant solutions. But since I did not want to wait with migrating my rules to JS, I have just built myself a quick and dirty workaround by:

  • creating a string item
  • in the JS rule, not being able to send the email from there, sendCommand() a JSON containing the relevant email info (recipient, subject, message) to the string item
  • creating a DSL rule that is triggered by the command to string item, parses the JSON, and then send the email (and then clears the string item via postUpdate())

That way, I can send emails from any JS rule by using that string item. Once this bug is fixed, I can then just quickly replace the sendCommand in the JS rule with the respective mailAction command

Hi Kstuken, can you post your creation?

edwolfe3 commented 2 years ago

@kstuken I'll give it a try.

I agree with @martychristensen, it would be nice to see your "creation" for reference.

edwolfe3 commented 2 years ago

@kstuken did I do something wrong or stumble across another issue with javascript in 3.2? In my JS rule, I issue the following command (per documentation)

items.getItem("WolfeDen_Mail").sendCommand("redacted")

However, I get:

TypeError: items.getItem is not a function

??

edwolfe3 commented 2 years ago

It seems like javascript has to many issues. I created a dsl rule to replace my js rule and all is fine.

UPDATE: it worked ONCE and then stopped. Now I'm getting:

javax.activation.UnsupportedDataTypeException: text/plain; charset=UTF-8

so I'm back to square one with the Mail binding.

@kstaniek can you post how your DSL script is working as I can't get anything related to mail work.

kstuken commented 2 years ago

Sure, if that helps:

Here's the item: String Mailtransporter "JSON String with values for mailer"

Here's the command sent in JS script

            const mailObject = {
                to: "some@email.com",
                subject: "subject line",
                message: "Message body goes here...",
            };
            items.getItem("Mailtransporter").postUpdate(JSON.stringify(mailObject));

And here's the DSL rule to pick it up and send the email:

rule "Mailer"
when
    Item Mailtransporter received command
then

    if (receivedCommand != NULL && receivedCommand != UNDEF && receivedCommand.toString != "") {
        val String json = receivedCommand.toString
        val String recipient  = transform("JSONPATH", "$.to", json)
        val String subject  = transform("JSONPATH", "$.subject", json)
        val String message  = transform("JSONPATH", "$.message", json)

        val mailActions = getActions("mail","mail:smtp:yourmail")
        mailActions.sendMail(recipient, subject, message)

        Mailtransporter.postUpdate("")
    }

end
markormesher commented 2 years ago

@kstuken What version are you running? I'm on openhab:3.2.0-debian and get the same error UnsupportedDataTypeException even via rules DSL, so it's interesting you're only having problems with JS rules. My rule for reference is:

rule "Debug: Send Email"
when
  // manual trigger only
  Item debug_neverChangingValue changed to "this-wont-happen"
then
  getActions("mail", "mail:smtp:migadu").sendMail("<email>", "OpenHAB Test Email", "Sent from OpenHAB")
end
edwolfe3 commented 2 years ago

I'm on openhab3.2.0 openhabian and it fails under DSL and JS rules too.

Jan 24, 2022 2:29:08 PM Mark Ormesher @.***>:

@kstuken[https://github.com/kstuken] What version are you running? I'm on openhab:3.2.0-debian and get the same error UnsupportedDataTypeException even via rules DSL, so it's interesting you're only having problems with JS rules. My rule for reference is:

rule "Debug: Send Email" when // manual trigger only Item debug_neverChangingValue changed to "this-wont-happen" then getActions("mail", "mail:smtp:migadu").sendMail("", "OpenHAB Test Email", "Sent from OpenHAB") end — Reply to this email directly, view it on GitHub[https://github.com/openhab/openhab-addons/issues/11838#issuecomment-1020464022], or unsubscribe[https://github.com/notifications/unsubscribe-auth/AJRFDT2YSN6AMB5ZE67WOLLUXWR6ZANCNFSM5KQXWJRA]. Triage notifications on the go with GitHub Mobile for iOS[https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675] or Android[https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub]. You are receiving this because you commented. [data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD8AAAA/CAYAAABXXxDfAAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAAAmSURBVGiB7cEBDQAAAMKg909tDwcUAAAAAAAAAAAAAAAAAAAAJwY+QwABivJx1AAAAABJRU5ErkJggg==###24x24:true###][Tracking image][https://github.com/notifications/beacon/AJRFDT4OMHKCUM2QXQ6O2PTUXWR6ZA5CNFSM5KQXWJRKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOHTJQXFQ.gif]

kstuken commented 2 years ago

I am on OH3.2 on openhabian, and using the JSScripting Add-on (ie ECMAScript 2021+ with the underlying openhab-js library). All my rules, as well as the mail thing definition are files based. I have no idea where your case might be failing, but looking at the OH forum it appears others have run into that issue before.

jpg0 commented 2 years ago

Notes regarding a quick investigation:

edwolfe3 commented 2 years ago

@kstuken I'm UI based.

Is there some workaround (that doesn't require rewriting the world) to get mail working until a fix can be made?

AndReinhar commented 2 years ago

Just FYI - the command seems to work fine when running it at startup - I have a rule to send me an email every startup (level 100) and this works. However running the rule manually does not work.

edwolfe3 commented 2 years ago

FYI: My rule is set to run everyday at 5;30. Today it ran and the email was sent. Just when I click "run now" it gets the error.

UPDATE: I have the DSL rule triggered by the update of a string item. Every time the item is updated the rule fires and sends the email. However, it is only if I click on "run now" in the UI script editor that it returns:

javax.activation.UnsupportedDataTypeException: text/plain; charset=UTF-8

opape commented 2 years ago

I can confirm this behavior. If the rule is triggered by the trigger (for me it is at a fixed time) the email is sent. If you click on "run now" in the UI you get the error message:

javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    javax.activation.UnsupportedDataTypeException: text/plain; charset=UTF-8

I use a Jython script within the rule..

J-N-K commented 2 years ago

That's because of a different ClassLoader being used in both cases.

fullmoonguru commented 2 years ago

I was hoping that triggering a rule with time would be a good work-around but that's not the case for me.

I THOUGHT this had been an intermittent issue for me before and I have just verified that. I set up a test rule to send an email every 5 minutes and I got 5 in a row.

So then I tried modifying a temp alert rule to trigger every 5 minutes and it did not work.

Then I un-paused the email test rule and it no longer worked.

I created a new email test rule and it did not work.

This is javascript.

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/mail-binding-errors-sending-mail-unsupporteddatatypeexception-text-plaijn-charset-utf-8/130365/21

fullmoonguru commented 2 years ago

Emails started working last night. I have 3 systems running openHABian OH3. I had a test email rule set to send an email on a time trigger (which wasn’t sending any) on one of these systems. I rebooted that system last night and woke up to a bunch of emails. I paused & unpaused the rule & it continues working. So I copied the rule to the other two systems. One started working, the other did not. I rebooted the one that wasn’t sending emails & it’s now working.

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/openhab-3-3-milestone-discussion/132715/37

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/mail-will-not-more-be-send/134230/5

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/mail-binding-errors-sending-mail-unsupporteddatatypeexception-text-plaijn-charset-utf-8/130365/25

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/cannot-send-mail-no-protocol/135334/10