0

I'm currently trying to automate incoming emails with python. I'm using the IMAP_TOOLS library. I've already managed to get the necessary values, but I need the time when the email response arrived, but I can't get this information

from imap_tools import MailBox, AND
from datetime import date

data_atual = date.today()
data_hoje = '{}/{}/{}'.format(data_atual.day, data_atual.month,data_atual.year)
valores_lista = []
# pegar emails de um remetente para um destinatário
username = "[REDACTED]@gmail.com"
password = "[REDACTED]"

# lista de imaps: https://www.systoolsgroup.com/imap/
meu_email = MailBox('imap.gmail.com').login(username, password)
lista = []
#pegar emails enviados por remetente especifico
coluna = 0
lista_emails = meu_email.fetch(AND(from_="[REDACTED]@gmail.com", to="c[REDACTED]@gmail.com",subject="MOV:",answered=True))
for email in lista_emails:
    email_subject = email.subject
    print(email_subject)

    email_hora = email.date_str
    #print(email_hora)
    hora0, hora1, hora2, hora3, hora_enviado,hora4 = email_hora.split()

    variavel0,variavel1,variavel2,variavel3,mv,variavel4 = email_subject.split()
    mv_ = mv.replace("MOV:","").replace("/UP:","")
    print(mv_)
    #print("Envio: ",mv + hora_enviado)
    #print(email.reply_to)
    lista = [data_hoje,hora_enviado,"","","",mv_]
    valores_lista.append(lista)
    coluna += 1
print(valores_lista)

I tried to use the documentation functions from the imap_tools documentation msg.to_values msg.from_values but it returns empty.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    If that password is real, please change it immediately! – craigb Nov 01 '22 at 00:53
  • Your original title said something else, but you seem to actually be asking about the delivery time specifically. Please review my edit and make corrections if I got it wrong. If you mean something radically different, probably also explain in more detail what you actually mean. – tripleee Nov 01 '22 at 07:45
  • `msg.from_values` and `msg.to_values` will simply contain the sender and the recipients. I don't think they should be empty, but they also don't contain what you seem to be asking about (even if I misunderstood your question). – tripleee Nov 01 '22 at 07:46
  • Hi! Thanks for answering. The password is fake! And in my question, I wanted to get the RESPONSE time from the email. For example, the email was replied to at 19:47 <- wanted to get this value – Paulo Amaral Nov 02 '22 at 13:25

1 Answers1

1

The IMAP standard does not expose this information directly.

What you can examine is:

  • The Date: header gets injected by the sender (typically automatically by their email client) and indicates the system time on the sending computer (or, in some circumstances, if this header was not added by the sender, the MTA which initially received the submission), usually in their local time zone, sometimes with formatting which violates the RFC.

    • The RFC calls for a standard machine-readable format with abbreviated month names and weekdays in English, but some clients will use local conventions and/or the user's preferred display language
    • Example: Date: Tue, 01 Nov 2022 09:03:51 +0200
    • In a parsed email.message.EmailMessage, the value can be extracted with message["date"] and parsed into a datetime object with message["date"].datetime.
    • The default email.policy will sometimes balk if the Date: header is not in the standard RFC format, though it is resilient against minor variations; for example, if the weekday is translated, but the month name can be parsed, it will cope just fine, and actually expose the weekday in English even though the original had it in some other language. If the Date: header could not be parsed, the datetime attribute will be None.
    • imap_tools contains the roughly equivalent information in msg.date; per the documentation, it will be a datetime object which defaults to 1900-01-01 if the header could not be parsed. The original Date: header is in msg.date_str
  • The Received: headers may contain this information, but their format is not specified in any standard.

    • The header inserted by your IMAP server (or whatever system delivered the message there) will typically contain this information, but you have to guess how to parse it.
    • In practice, if you can identify this information in a recent message, you can probably rely on this remaining reasonably stable, though if the email infrastructure is updated at some point, whatever logic you came up with might break.
      • Keep in mind that Received: headers are added from bottom to top. Generally, the topmost Received: header will be the one to look at, though some systems will add multiple headers, some of them with other information than the actual delivery event.
    • Example: Received: from internal.relay.example.com (unknown [10.9.8.7])
      by imap.example.com (Postfix) with ESMTP id DEADBEEFABAD
      for <victim@example.com>; Wed, 7 Jul 2022 19:06:47 +0300 (EEST)
    • The above example is from Postfix; Sendmail's headers look roughly similar. Exim and Microsoft Exchange are weird in different ways, Microsoft predictably horribly.
    • Gmail example: Received: by 2002:a05:6502:61c:0b:232:4e44:489d with SMTP id g1csp123456abc;
      Thu, 20 Oct 2022 01:22:33 -0700 (PDT)
    • imaptools collects the headers in msg.headers in a slightly weird dict structure, but this seems to omit the details and extract only the parsed host name.
    • Here's a rough sketch for parsing the date out of the top Received: header out of a Gmail message from the email.message.EmailMessage object in msg.obj as provided by imap_tools:
      top_rcvd = msg.obj["received"]
      assert ";" in top_rcvd
      rcvd_date_str = top_rcvd.split(";")[-1].strip()
      rcvd_date = email.utils.parsedate_tz(rcvd_date_str)
      
      See also How to parse a RFC 2822 date/time into a Python datetime? for some variations and amplifications.

In a "happy scenario" the difference between the two will be fairly minor, but it is not unheard of for a message to spend several hours or even days in transit, so if you need the time the message was actually delivered, the latter is (unfortunately) the answer.

tripleee
  • 175,061
  • 34
  • 275
  • 318