IMAP subject line and date

Under 2023.4, IMAP now says:

IMAP

The previously deprecated YAML configuration of the IMAP integration has been removed.

IMAP is now configured via the UI, any existing YAML configuration has been imported in previous releases and can now be safely removed from your YAML configuration files.

With this new change, I deleted this from my configuration.yaml file:

# IMAP
sensor:
  - platform: imap_email_content
    server: imap.gmail.com
    name: aaronautomation_mail
    port: 993
    username: _username_
    password: _password_
    folder: Inbox
    senders:
     - [email protected]
     - [email protected]

And then, I added IMAP through Settings > Devices & Services > Add Integration.

The issue, is that I no longer know how to extract the subject line and date of emails that come in. Previously, I was using this to get the subject:

{{ state_attr('sensor.aaronautomation_mail','subject') }}
{{ state_attr('sensor.aaronautomation_mail','date' }}

which no longer works. I am confused by the documentation on how to get these values now.

The IMAP integration only sends events. If you have a sensor, you created yourself :wink:

You need to add a template sensor in your configuration.yaml. I just created a link in my configuration.yaml to template.yaml and then added it in a new file called template.yaml I created.

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "aaronautomation_mail_eventid"
        event_data:
          sender: [email protected]
    sensor:
      - name: aaronautomation_mail_sensor
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Message: "{{ trigger.event.data['text'] }}"
          Server: "{{ trigger.event.data['server'] }}"
          Username: "{{ trigger.event.data['username'] }}"
          Search: "{{ trigger.event.data['search'] }}"
          Folder: "{{ trigger.event.data['folder'] }}"
          Sender: "{{ trigger.event.data['sender'] }}"
          Date: "{{ trigger.event.data['date'] }}"
          Subject: "{{ trigger.event.data['subject'] }}"
          To: "{{ trigger.event.data['headers'].get('Delivered-To', ['n/a'])[0] }}"
          Return-Path: "{{ trigger.event.data['headers'].get('Return-Path',['n/a'])[0] }}"
          Received-first: "{{ trigger.event.data['headers'].get('Received',['n/a'])[0] }}"
          Received-last: "{{ trigger.event.data['headers'].get('Received',['n/a'])[-1] }}"

You can remove any of the lines under attributes that you don’t want. So in your case since the subject in the example below is already in the “state”, your code would be:

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "aaronautomation_mail_eventid"
    sensor:
      - name: aaronautomation_mail_sensor
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Date: "{{ trigger.event.data['date'] }}"

Then you can call it in your templates with:

{{ states('sensor.aaronautomation_mail_sensor')  }}
{{ state_attr('sensor.aaronautomation_mail_sensor','Date' }}

If you wanted different event_data triggers… you do it like this:

template:
  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "aaronautomation_mail_eventid"
        event_data:
          sender: [email protected]
    sensor:
      - name: aaronautomation_mail_sensor
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Date: "{{ trigger.event.data['date'] }}"

  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "emailsensor2_mail_eventid"
        event_data:
          sender: [email protected]
    sensor:
      - name: emailsensor2_mail_sensor
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Date: "{{ trigger.event.data['date'] }}"

  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "emailsensor3_mail_eventid"
        event_data:
          sender: [email protected]
    sensor:
      - name: emailsensor3_mail_eventid
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          Date: "{{ trigger.event.data['date'] }}"

Check the documentation there are examples that show how to make multiple sensors from the same trigger. I do not know how to do multiple senders though, it’s documented to use custom template and I dont know how to do that yet.

Hi, I have set message size to

however if message size is 4000 bytes sensor returns unknown

if I send smaller email it works:

How to get it working - using prevous version I was able to extract link from Garmin livetrack email … now I’m strugling much…

Set the max size to something closer to what you need, not the absolute max. By setting it as high as it goes, you run the risk of the event exceeding its allowed limit.

1 Like