Help with IMAP working with long email

Hi everyone,

I have a little problem with loading data from an email via the IMAP integration. The problem is that the email I’m trying to analyze has really bad formatting. It contains a tracking picture with an extremely long URL (like 14,000 characters… amazing). This picture is actually in the middle of the email. The first value I’m extracting is before the picture, and the second is after the picture. The first value loads without any issue, but there’s a problem with loading the second value. Even if I set a bigger max message size, it takes a long time, and even then, the message doesn’t fully load.

Is it somehow possible to load only the text and not the HTML code? This would be the best solution, I think or better I hope… :smiley:

If not, is there any workaround? Here is the code I’m using for IMAP and sensor loading values from the email. I would like to have a second sensor working as well, which is after the problematic picture in the code. Any help or guidance would be greatly appreciated!

- trigger:
    - platform: event
      event_type: "imap_content"
      id: "email_imap"
      event_data:
        sender: "[email protected]"

  sensor:
    - name: imap_content
      state: "{{ trigger.event.data['subject'] }}"
      attributes:
        Message: "{{ trigger.event.data['text'] }}"
    - name: "cost"
      unit_of_measurement: "CZK"
      # regex will find every non-digit character and commas ',' and replace it with ''
      state: >
        {{ trigger.event.data["text"]
          | regex_findall_index("Částka:\s*([\d\s.,]+)\s*\sCZK<\/strong>")
          | regex_replace(find='[^0-9,]', replace='')
          | replace(",",".")
          | float }}
#    - name: "var_symb"
#      state: >
#        {{ trigger.event.data["text"]
#          | regex_findall_index("Variabilní symbol:\s*([\d\s.,]+)\s*\sCZK<\/strong>") }}

I would advice not to use the text in trigger data, but instead use the new imap.fetch service. It needs the entry_id and the uid. The entry id can be selected in the UI, the uid you can use from the event data.

See: IMAP - Home Assistant

This method has no message size limitation.

The new imap.fetch service read only the text/plan part, how could I read the text/html body?

That is not possible at the moment.

Perhaps by ensuring that the part of the text to be used is chosen by the user during the configuration phase and does not set up, regardless of how it is currently.

@property
    def text(self) -> str:
        """Get the message text from the email.

        Will look for text/plain or use/ text/html if not found.
        """
        message_text: str | None = None
        message_html: str | None = None
        message_untyped_text: str | None = None

        part: Message
        for part in self.email_message.walk():
            if part.get_content_type() == CONTENT_TYPE_TEXT_PLAIN:
                if message_text is None:
                    message_text = self._decode_payload(part)
            elif part.get_content_type() == "text/html":
                if message_html is None:
                    message_html = self._decode_payload(part)
            elif (
                part.get_content_type().startswith("text")
                and message_untyped_text is None
            ):
                message_untyped_text = str(part.get_payload())

        if message_text is not None and message_text.strip():
            return message_text

        if message_html:
            return message_html

        if message_untyped_text:
            return message_untyped_text

        return str(self.email_message.get_payload())

May be. But as for now the data is not available. It might be added as a new feature.

1 Like