Is there a way using Python 3 IMAPlib to retrieve both the sender address & the associated UID for a given message

女生的网名这么多〃 提交于 2019-12-11 06:20:21

问题


Using regular expressions I have managed to extract all sender addresses from the emails located in my Inbox, However I've tried and failed many times to also extract the associated UIDs for those individual emails.

Here's what I have so far:

    result, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
for i in id_list:
  typ, data = mail.fetch(i,'(RFC822)')
  for response_part in data:
    if isinstance(response_part, tuple):
      msg = email.message_from_bytes(response_part[1])
      sender = msg['from'].split()[-1]
      address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
  if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
    email_list.append(address)
    print address

The output is slow (I'm assuming because of regular expressions) but none the less it gets the job done.

Output:

    no-reply@mail.instagram.com
    no-reply@accounts.google.com
    rhodesi926@icloud.com
    wat@elevenyellow.com
    pinbot@notifications.pinterest.com
    support@autopin.co
    pinbot@account.pinterest.com
    info@shootbox.me
    pinbot@explore.pinterest.com
    bugra@boostfy.co
    mail-noreply@google.com
    pinbot@inspire.pinterest.com
    mua@mikasabeauty.com
    noreply@apple.com
    privacy-noreply@policies.google.com

Part of the problem is I don't understand how the UIDs are connected to the sender and where the UIDs get stored in the IMAP structure.

Im assuming I could right a regular expression that could pull any 4 digit combination of numbers from the "UID:" Field, I then fear it will slow my script down to a crawl....

If anyone understands Imaplib and can help I would be eternally grateful. Thank You.

来源:https://stackoverflow.com/questions/51052157/is-there-a-way-using-python-3-imaplib-to-retrieve-both-the-sender-address-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!