Search code examples
pythongmailimaplib

Python - Get datetime of mails - Gmail


I was trying to download the attachments from the Gmail using python for a specific keyword and the code is below,

def read_email_from_gmail():
mail = imaplib.IMAP4_SSL(imap_url)
mail.login(user, password)
mail.select('"[Gmail]/All Mail"')
print("Login into Mailbox")
result, data = mail.search(None, '(SUBJECT "Contract note")')
count = 0
for num in data[0].split():
    result, data = mail.fetch(num, "(RFC822)")
    raw_email_string = data[0][1].decode('utf-8')
    msg = email.message_from_string(raw_email_string)
    for part in msg.walk():
        if part.get_content_type() == "text/plain":
            print(part.get_payload(decode=True))

        if part.get_content_maintype() == 'multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue
        fileName = part.get_filename()
        print(part.get_)
        fileName = fileName+str(count)+str('.pdf')
        count = count + 1
        if bool(fileName):
            filePath = os.path.join(attachment_dir, fileName)
            with open(filePath, 'wb') as e:
                e.write(part.get_payload(decode=True))

The code works fine and it was downloading the attachment. The issue is all the attachment in the mails are off in the same name so in the above code I have added a count and appended it. But in future it will be so tough to find the right file.

Note:- I used to recieve the mail daily

Can someone please guide me how to get the date of the email so that I will append it to the filename rather than the count.


Solution

  • We can get the date of the email using

    msg['Date']