Imap email content sensor rewrite

Hello,

I am looking into the ‘imap_email_content’ integration and I believe it needs some update. I am working on adding config flow and making it async using aioimaplib. During the re-write I went through the reported issues to better understand what are the issues users are facing with the current email message retrieval process. Here is what I would like this integration to provide and would like your comments to make it more powerful.

The user will have the following options to specify which email message should be considered when updating the sensor:

  • Folder name (default is INBOX)
  • List of allowed senders (default is blank which means any sender): This is already used in the current integration however, it is a required field. I plan to make it optional and blank by default.
  • Select unread message only (default is True): The current integration doesn’t provide the option to specify if only unread messages should be checked. This option allows to look at unread messages only.

As for the search process and updating the sensor here is what I though would be best:

  • Upon initial setup the integration will fetch the newest email message from the INBOX folder.
  • The user can keep this setting or change the options mentioned above to select the allowed senders and whether to check for unread only or any email.
  • In case allowed senders are selected the integration will return the latest email message which would be sent from any allowed sender.
  • In case multiple email messages are returned from the search only the newest message is fetched and used to update the sensor state. Unlike the current implementation which iterates through each new message and updates the sensor state I believe it is best to get the latest message only.

There are some considerations which I am still not sure how to deal with so I will list them below for your comments:

  • In case the search criteria doesn’t return any messages what should the sensor state update to?
  • In case the last fetched message is deleted from the imap server how should the sensor update?

Here are the possible answers:

  • List item
  • Change state to ‘unknown’
  • Make the sensor ‘unavailable’

If there is anything else that I have missed feel free to highlight.

Hi @engrbm87,

A feature that has been discussed a few times in this forum (and that I also would like as well) is the feature to delete the e-mail from the IMAP server once it has been used.

One use-case of imap_email_content is to dedicate an IMAP account to Home Assistant and to send sensor e-mails there. As e-mails never get deleted, the mailbox is growing indefinitely except if we implement a script to regularly clean it up.
See this thread on the topic: Imap_email_content sensor go through all emails in inbox after restart - #21 by browetd

Are you already working on the rewrite of the email content sensor?

Frédéric.

I was looking at this too.

Instead of rewriting entirely to use asyncio, why not just subclass the imap component? It handles push too.

Thanks for your feedback. There is currently a discussion about using the imap integration to fire events for new email messages. This would be a replacement for the email_content sensor.
This would remove the complexity of handling the sensor state updates and allow the user to configure their automations (and template sensors) based on the fired events.
As for deleting emails, this would a little hard as we need to look into when the email should be deleted and should that be done automatically or as a service that the user can call to delete certain emails. Will definitely look into this with the new approach mentioned above.

1 Like

I would just like it to either make it READ after i read it, as it keeps reading the same mail over and over and the sensor stays active.