The main problem with converting Outlook email to text files is that Outlook does not always preserve formatting information in the email. This can make it difficult to understand the content of the email.
-3.x email outlook text-files share|improve this question edited Jan 14 '15 at 8:13 asked Jan 14 '15 at 7:51 user3775557 1,8271133 add a comment| 2 Answers 2 active oldest votes up vote 0 down vote accepted You can use the email module to read the message and then save it to a file. The following code will read the message from your inbox and save it as a text file in your current directory. import os import imaplib import email def save_attachment(msg, download_folder="/tmp"): """Given a message, save its attachments to the specified download folder (default is /tmp)""" att_path = "No attachment found." for part in msg.walk(): if part.get_content_maintype() == 'multipart': continue if part.get('Content-Disposition') is None: continue filename = part.get_filename() if filename is not None: sv_path = os.path.join(download_folder, filename) content = part.get_payload(decode=True) try: with open(sv_path, 'wb') as fp: fp.write(content) except IOError: print("Error saving attachment to", sv_path) return att_path return sv-path userName = "yourmail@gmailcom" password = "yourpassword" mail = imaplib.IMAP4('imap-mailhost') mail .login(userName , password ) mail .select("inbox") result , data
= mail .search(None, “ALL”) ids = data [ 0 ] id_list = ids.split() latest_email_id = id_list[- 1 ] result , data = mail .fetch(latest_email_id , “(RFC822)”) raw_email = data [ 0 ][ 1 ] # converts byte literal to string removing b” msg = email.message_from_string(raw_email) savepath = os.getcwd() + ‘/’ + str(msg[‘Subject’]) + ‘.txt’ # or savepath = ‘C:\Users\Downloads\’ strMsg=str(“Subject: “+msg[‘Subject’]+”
Work with text files
There are a few ways to work with text files in Python. The simplest way is to open the file in a text editor like Notepad or TextEdit, and type your code.
Another way is to use the file object in Python. You can open the file using the open() function, and then use the read() and write() functions to read and write data to the file, respectively.
Finally, you can use the os.path module to access the path of a text file. You can then use the gettext() function to get information about the file, such as its name and size.
Outlook is a popular email client used by millions of people around the world. It is available on many different platforms, including Windows, MacOS, and Linux. Outlook has a rich feature set that allows you to manage your email, calendar, and contacts easily. In this tutorial, we will discuss how to work with Outlook email in Python.
First, you will need to create an instance of Outlook object. You can do this by using the built-in Outlook() function:
Outlook = Outlook()
Next, you will need to create an instance of the Email object. This object represents an individual email message in your mailbox:
email = Email( ‘YourEmailAddress’ )
You can then access various properties and methods of the Email object:
email.subject() print (email.subject)