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.