React to incoming mail

Hello,

I have following use case:
One day before the refuse collection comes I get a email like this:

Ihr nächster Abfuhrtermin
Sehr geehrte Kund,
wunschgemäß möchten wir Sie an den folgenden Abfuhrtermin erinnern:

Restmülltonne:
Freitag, 13.01.2017
Falls Sie diesen Service…

Now I want to react to this Mail and send a notification to a Central Operation Unit (Link). I already send somethink to the operation unit, so this is not my problem… The problem is, how can I react to a incomming mail and scour it for a word like “Restmülltonne” or “Gelber Sack”?

I already try it with IFTTT but somehow it is not working… with the connection to gmail…
Question: Is there a other posibility/binding to react to a gmail? (Instead of IFTTT)

regards

There is a 1.9 binding that apparently has not yet been tested with OH 2 yet.

See the following for how to install and test this with OH 2.

http://docs.openhab.org/addons/bindings.html#further-1x-bindings

If it works, please create an issue on the openhab2_addons GitHub repo or post a new thread on the forum saying you got it to work.

I recall that binding has some usability issues and could use a review in that regard.

Could you meanwhile solve this topic? I want to achieve the same thing.

There seams no (simple - I am an openhab beginner) way to get the content of an incoming mail and react to this.

Did you have a look to the mail binding ?

1 Like

If you run your own mailserver, you could use procmail to react immediately on incoming mails and trigger REST calls to openHAB or push MQTT messages. I think we need to know a bit more about your setup. :slight_smile:

I think this would be the correct approach. The content channel gives access to the mail content and can be processed in rules or by transformations.

I have the mail binding installed and use it to send mails.

But, I don’t see any channels in the thing configuration. Especially no content channel. According to the binding documentation it has to be configured manually? I don’t know how to do this.

With access to the email subject and body (content) it should be possible to trigger a rule.

My configuration is OH4 on an Ubuntu running NUC.

I am not running an own mail server.

That’s correct, as you probably have configured a smtp thing for sending mail.
The binding has imap and pop3 things for incoming mail as well.

1 Like

I have these things:

The code-tab of the IMAP thing shows the following (edited):

UID: mail:imap:b01553b004
label: IMAP-Server
thingTypeUID: mail:imap
configuration:
  refresh: 60
  hostname: imap.example.org
  password: geheim
  security: SSL
  port: "993"
  username: geheim@example.org

Under the channel tab I find:

There seems to nothing to configure?

Click on “Add Channel” (immediately below the message), select “Content” and add the configuration.

1 Like

This looks promising.

As I wrote: I am a beginner to OH

1 Like

I was able to create the channels:

Code for the thing:

UID: mail:imap:b01553b004
label: IMAP-Server
thingTypeUID: mail:imap
configuration:
  refresh: 60
  hostname: imap.example.org
  password: geheim
  security: SSL
  port: "993"
  username: example@example.org
channels:
  - id: mailAbfuhrtermineContent
    channelTypeUID: mail:content
    label: mailAbfuhrtermineContent
    description: ""
    configuration:
      subject: .*Abfuhrtermine.*
      folder: INBOX
      markAsRead: true
  - id: CountSmarthomeMails
    channelTypeUID: mail:mailcount
    label: CountSmarthomeMails
    description: ""
    configuration:
      type: UNREAD
      folder: INBOX

I then linked the two channels to items using the sematic model:

grafik

The “count item” gives values as expected:

with the item code:

label: CountSmarthomeMails
type: Number
category: ""
groupNames:
  - IMAPServer
tags:
  - Point

But the “content item” is NULL:

Code:

label: mailAbfuhrtermineContent
type: String
category: ""
groupNames:
  - IMAPServer
tags:
  - Point

My hope was that the content would be a string with the mail body/bodies. What am I missing?

That looks quite good. Is the mail already marked as “read”? In that case it will not be processed. You could try removing the “read” marker on the mail. If that does not solve the problem:

  • disable the thing
  • select “TRACE” logging (Settings/Add-on Settings (right column), click “show all”, select the mail binding)
  • mark the message as “unread”
  • enable the thing
  • check the log the the inbox folder is processed, copy the log
  • select “DEFAULT” logging (Settings/Add-on Settings (right column), click “show all”, select the mail binding)

You should see log lines like

[DEBUG] Processing `subject` from `sender-address`

then one of

[TRACE]  Detected plain text message
[TRACE]  Detected MIME message
[TRACE]  Detected MIME multipart text message

and finally

[TRACE]  Found content 'mail content goes here'

Can you confirm that?

No. Nevertheless, I tried to remove the “read” marker without success.

The “openhab.log” file shows the following line after Trace enabled:

2024-03-25 17:56:00.143 [DEBUG] [inding.mail.internal.POP3IMAPHandler] - Processing `Ihre Abfuhrtermine` from `"Dr. Harald Pott" <example@example.org>`
2024-03-25 17:56:00.162 [WARN ] [inding.mail.internal.POP3IMAPHandler] - Failed to convert mail content from '"Dr. Harald Pott" <example@example.org>' with subject 'Ihre Abfuhrtermine', to String: class com.sun.mail.imap.IMAPInputStream
2024-03-25 17:56:03.812 [DEBUG] [inding.mail.internal.POP3IMAPHandler] - Processing `Ihre Abfuhrtermine` from `"Dr. Harald Pott" <example@example.org>`
2024-03-25 17:56:03.831 [WARN ] [inding.mail.internal.POP3IMAPHandler] - Failed to convert mail content from '"Dr. Harald Pott" <example@example.org>' with subject 'Ihre Abfuhrtermine', to String: class com.sun.mail.imap.IMAPInputStream

Edit: Delete mail-Address

Did you install „regex transformation services“ from the Addon-Store?

So one of the messages in the second block is not in the log? Please forward the mail, including all headers to github@klug.nrw. I‘ll check why it is not properly processed. Thanks.

1 Like

I forwarded the email in question to the given address. Thank you very much for the outstanding support!

Yes, the add-on ist installed:
grafik