push the emails into a json
This commit is contained in:
parent
7819d8f79f
commit
c10f156584
20
get.py
20
get.py
@ -1,6 +1,8 @@
|
|||||||
import imaplib
|
import imaplib
|
||||||
import email
|
import email
|
||||||
|
from email.header import decode_header
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
|
import json
|
||||||
|
|
||||||
# IMAP server settings for Hotmail
|
# IMAP server settings for Hotmail
|
||||||
imap_server = 'imap-mail.outlook.com'
|
imap_server = 'imap-mail.outlook.com'
|
||||||
@ -30,6 +32,10 @@ for email_id in email_ids:
|
|||||||
raw_email = response[0][1]
|
raw_email = response[0][1]
|
||||||
email_message = email.message_from_bytes(raw_email)
|
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
|
# Extract the body of the email
|
||||||
if email_message.is_multipart():
|
if email_message.is_multipart():
|
||||||
for part in email_message.get_payload():
|
for part in email_message.get_payload():
|
||||||
@ -39,10 +45,18 @@ for email_id in email_ids:
|
|||||||
else:
|
else:
|
||||||
email_body = email_message.get_payload(decode=True).decode()
|
email_body = email_message.get_payload(decode=True).decode()
|
||||||
|
|
||||||
print("Email body:")
|
# Create a dictionary with the email details
|
||||||
print(email_body)
|
email_details = {
|
||||||
print("------------------------")
|
'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
|
# Logout and close the connection
|
||||||
imap_conn.logout()
|
imap_conn.logout()
|
||||||
imap_conn.close()
|
imap_conn.close()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user