【问题标题】:Getting the right 'Date' of the Last Email Sent to the Contact获取发送给联系人的最后一封电子邮件的正确“日期”
【发布时间】:2020-06-22 19:20:06
【问题描述】:

所以我成功地从下面的代码中的联系人那里获得了最后一封电子邮件的日期。

现在我正在尝试获取发送给联系人的最后一封电子邮件的日期。作为编码/python 的初学者,我无法正确理解它。我试图将“从”更改为“至”,但它没有给我正确的日期。

知道我应该如何进行吗?我试过环顾四周,但没有找到解决方案。

import email
from imapclient import IMAPClient
from datetime import timedelta, date, datetime


HOST = 'imap.gmail.com'
USERNAME = 'username'
PASSWORD = 'password'
ssl = True

## Connect, login and select the INBOX
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX') 

since_date = date(2016, 1, 1)    

##Search Inbox
messages = server.search(['FROM', 'email_i_want_to_search@gmail.com', 'Since', since_date])
response = server.fetch(messages, ['RFC822'])
last_msg_id = list(response.keys())[-1] 
data = response[last_msg_id]
msg_string = data[b'RFC822']
msg = email.message_from_string(msg_string.decode())
print('ID %d: From: %s Date: %s' % (last_msg_id , msg['From'], msg['date']))

【问题讨论】:

    标签: python-3.x gmail-api imapclient


    【解决方案1】:

    您没有得到正确日期的原因是列表无序。 为了保证你得到的是正确的,你需要先对它进行排序。

    试试这个以友好风格为新手编写的快速分步解决方案:

    # Getting your response keys
    response_keys='cabd' 
    
    # Your response keys converted into list
    msg_ids = list(response_keys) 
    print(msg_ids) # result: ['c', 'a', 'b', 'd']
    
    # Here your are sorting the list in place using built-in sort function
    # The syntax is: list.sort(reverse=True|False) and the default is True
    msg_ids.sort() 
    print(msg_ids) # result: ['a', 'b', 'c', 'd']
    
    # Finally, grab the last item in the list
    last_msg_id = msg_ids[-1] 
    print(last_msg_id ) # result: d
    

    【讨论】:

    • 嗨,杰弗里,非常感谢您的回答。我尝试按照您描述的方式对列表进行排序,但是,它仍然向我显示 1-2 个月前发送给联系人的最后一封电子邮件的日期(类似于倒数第二封电子邮件或倒数第三封电子邮件)。而我昨晚已经将邮件发送给了一个人。我觉得这与我们可以获得适当日期的语法有关。请让我知道你的想法。
    • 您好 Sourav,排序可能会受到您使用的日期格式的影响。您可能首先需要将 msg_ids 中的日期字符串数据类型转换为日期数据类型,然后进行相应的排序。为了能够确定最佳解决方案,您能否在此处发布 list(response.keys()) 的结果?
    • 所以这是我将其设置为“To”ID 16390: To: "souravchatterjee00" <souravchatterjee00@gmail.com> Date: 20 Nov 2019 12:49:29 -0000 时得到的输出,这是我将其设置为“From”ID 17155: From: Sourav Chatterjee <souravchatterjee00@gmail.com> Date: Fri, 7 Feb 2020 20:14:38 +0530 时的输出
    【解决方案2】:

    这个方法怎么样?不是很简单,但它可以完成工作。

    • 将发送给特定用户的消息设为List
    • 从中获取项目(第一个是最近发送的消息)
    • 使用第一条消息的id做一个Get,使用metadatadate作为formatmetadataHeaders的参数

    • 获取headers返回的第一项的值

        service = build('gmail', 'v1', credentials=creds)
    
    
        messages = service.users().messages().list(userId='me', q='to:user@email.com').execute()
    
        items = messages.items()[1][1]
    
        lastmess = service.users().messages().get(userId='me', id=items[0]['id'], format='metadata', metadataHeaders='Date').execute()
        print (lastmess['payload']['headers'][0]['value'])
    

    结果:

    Wed, 11 Mar 2020 16:10:32 +0100

    【讨论】:

    • 嗨,我收到一个错误NameError: name 'build' is not defined。我需要导入一些模块才能继续吗?
    • 是的,你需要从Quickstart添加导入和授权码(从第一行到service = build('gmail', 'v1', credentials=creds)
    • 好的,代码现在可以工作了。但它没有打印出任何东西。
    • 你能用新代码编辑你的问题吗?此外,请确保在 Gmail 界面的搜索栏中搜索 to:user@email.com 时获得结果。
    • 好吧,代码正在执行中。但它没有给出任何结果。
    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多