I have used exchangelib library to download emails from my Inbox. The messages are eventually instance of exchangelib.items.Message. I want to save this entire email as .msg file so that I can later attach it to some the application. Can someone please let me know how I can do this in python ? In Below code I want to save each element of the msgs list. Currently I am working on just one email.
'''
from exchangelib import Account, Configuration, Credentials, DELEGATE
def connect(server, email, username, password):
"""
Get Exchange account cconnection with server
"""
creds = Credentials(username=username, password=password)
config = Configuration(server=server, credentials=creds)
return Account(primary_smtp_address=email, autodiscover=False, config = config, access_type=DELEGATE)
def get_recent_emails(account, folder_name, count):
"""
Retrieve most emails for a given folder
"""
# Get the folder object
folder = account.inbox / folder_name
# Get emails
return folder.all().order_by('-datetime_received')[:count]
account = connect(server, email, username, password)
emails = get_recent_emails(account, 'BSS_IT', 1)
msgs = []
for msg in emails:
msgs.append(msg)
'''
I'm not sure there's a recognized standard for the format of .eml
files, but at least some email clients dump the raw MIME content, which is available in exchangelib as Message.mime_content
.