push the emails into a json

This commit is contained in:
Paul Trowbridge 2023-05-16 21:44:11 -04:00
parent 7819d8f79f
commit c10f156584
1 changed files with 17 additions and 3 deletions

20
get.py
View File

@ -1,6 +1,8 @@
import imaplib
import email
from email.header import decode_header
from getpass import getpass
import json
# IMAP server settings for Hotmail
imap_server = 'imap-mail.outlook.com'
@ -30,6 +32,10 @@ for email_id in email_ids:
raw_email = response[0][1]
email_message = email.message_from_bytes(raw_email)
# Extract the sender and receipt date of the email
email_from = str(decode_header(email_message['From'])[0][0])
email_date = email_message['Date']
# Extract the body of the email
if email_message.is_multipart():
for part in email_message.get_payload():
@ -39,10 +45,18 @@ for email_id in email_ids:
else:
email_body = email_message.get_payload(decode=True).decode()
print("Email body:")
print(email_body)
print("------------------------")
# Create a dictionary with the email details
email_details = {
'id': email_id.decode(),
'from': email_from,
'date': email_date,
'body': email_body
}
# Print the JSON representation of the email details
print(json.dumps(email_details, indent=4))
# Logout and close the connection
imap_conn.logout()
imap_conn.close()