0

I was looking for a solution to get all my text messages/content (not attachments) in my gmail's email-account to a text file and I found a piece of code which promises to do so. I have python 3.4 and 2.7 both installed on win 7. I know php a bit but python I am zero. Right now, I have the code copied into a notepad text and saved it as test.py. Here is the complete code.

import imaplib
import email
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myemailid@gmail.com', 'mypassword')
mail.list() 
mail.select('inbox')

result, data = mail.uid('search', None, "ALL")
i = len(data[0].split())
for x in range(i):
    latest_email_uid = data[0].split()[x]
    result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
    raw_email = email_data[0][1]

    raw_email_string = raw_email.decode('utf-8')
    email_message = email.message_from_string(raw_email_string)
for part in email_message.walk():
    if part.get_content_type() == "text/plain":
        body = part.get_payload(decode=True)
        save_string = r"F:\result\email_" + str(x) + ".txt"
        myfile = open(save_string, 'a')
        myfile.write(body.decode('utf-8'))
        myfile.close()
    else:
        continue

ISSUE : The code when run gives nothing in return.

UPDATE : Actually I have been going through a lot of threads here. Closest to what I am asking are here and here but no one has a unified answer and the solutions are scattered in bits and pieces, or it is too specific so it is very difficult for me to make sense out of it. Trust me, I am testing and trying. Even a Similar question remains unanswered here.

Community
  • 1
  • 1
gurung
  • 628
  • 2
  • 11
  • 33
  • Please try to spend some time on other posts related to this topic. Eg.:https://stackoverflow.com/questions/10991533/imaplib-gmail-how-to-download-full-message-all-parts-while-not-marking-read?rq=1 https://stackoverflow.com/questions/14029768/python-imaplib-fetch-body-emails-gmail?rq=1 – ρss Mar 23 '14 at 13:42
  • take a look at my update pl. – gurung Mar 23 '14 at 13:53
  • Couple of observations: Your tabbed-in lines don't look tabbed-in enough (4 spaces). You will need to change the save_string line if you want to specify a different extension and folder or drive. You can run this script by opening "Idle" and opening this .py text file. Then there will be a menu command "Run Module." – J Kelly Mar 23 '14 at 14:07
  • @JKelly I have been able to run the script since the question but I think I am getting the save_string line wrong. Right now I have `save_string = str("F:\result\email_" + str(x) + ".txt")` but that does not seem to work. Maybe this is the only thing which is wrong in the equation – gurung Mar 23 '14 at 14:30
  • Retain the double \\ from the original or put a lower-case r in front of the string. Actually, you shouldn't even need that str(), unless that is a new quirk of python 3.x? – J Kelly Mar 23 '14 at 15:11
  • save_string = r"F:\result\email_" + str(x) + ".txt" – J Kelly Mar 23 '14 at 15:11
  • @JKelly So I made the suggested changes. Started IDLE (Python GUI) mode->Python 3.4.0 shell->file->open->test.py->Run Module. Now the shell window >>>=====RESTART======= . Nothing else happens. How do I know this is working? Do I need to manually create a txt.file in the mentioned folder ? Noob questions, sorry – gurung Mar 23 '14 at 15:23
  • Read up on try/except to see how to monitor your progress. You should not need to create the txt file. – J Kelly Mar 23 '14 at 19:32

1 Answers1

1

For those who may find it useful. This script will run fine on Python 3.4 with these changes.

save_string = "F:\\result\\email_" + str(x) + ".txt"

myfile.write(str(body)).

Just change the directory and the folder name according to your need. Thanks to the original code provider.

gurung
  • 628
  • 2
  • 11
  • 33