Python error fetch email from Gmail -
I have a python script like this to get an email from my Gmail. In some emails I can get a CSV file , But an email has encountered an error.
This is my script:
import import import import import OS detach_dir = '.' #Directory where the attachment is to be saved (Default: Current) Category GMTST (object): def __init __ (self): Self-submitter = "/ TMP" def test_save_attach (self): auto.connection = poplib.pop3_ssl (' .com ', 995) self.connection.set_debuglevel (1) self.connection.user ("email.google") self.connection.pass_ ("password") email, total_bytes = self.connection.stat () print (" Email in {0} Inbox, total of {1} bytes. "Format (email, total_bytes)) Return format: (feedback, ['Message_Num Oak TTS ', ...], octet) msg_list = self.connection.list () print (msg_list) # Border for processing in message (email): # return in format: (feedback, [' line ', ... ], Octetate response = self.connection.retr (i + 1) raw_message = response [1] str_message = email.message_from_string ('\ n'.join (raw_message)) # Save attachment for part in str_message.walk () : Print (part.get_content_type ()) if part.get_content _maintype () == 'multipart': If there is no part.get ('content-break'), continue: print ("no content dispatch") File name = part.get_filename () co Unter = 1 # Continue if there is no filename, we create one to avoid the duplicate, if the file is not named: file name = 'part-% 03d% s'% (counter, 'bin') counter = 1 et_path = os.path.join (detach_dir, filename) # Check it already is not there if os.path.isfile (att_path) is not: # Finally stuff fp = open (att_path, 'wb') fp.write Write (part.get_payload (decode = True)) fp.close () # if not (filename): file name = "test.txt" # print (filename) #fp = open (OS path.joint (self tester) , Filename), 'WB') # FPWritten (part.get_payload (decode = 1)) # # fp.close # I exit to exit instead of Pop3 Lib, to ensure that the message has not been deleted in Gmail import Sys sys.exit (0) d = GmailTest () d.test_save_attach ()
There is an error:
Traceback (most recent call final): The file " Getmail.py ", line 71, & lt; Module & gt;
Please help, thank you ...
/ P>
This is a variable scope issue.
In the row 22, filename = any
.
You are getting this error because the variable filename
has not been declared in line 47. You declare it in Late 43 for the loop
and when the loop leaves, then there is no
More information can be found.
Comments
Post a Comment