I have started working with 2.7 simply because I don't know when the error is caused by me or because of version issues. This is how far I have gotten, I can access and pull emails that only come from a selected email address (OWNER) I have it parsed down to print the subject only And I have it saving the string to a file output.txt And I have it deleting the emails so they don't reread, Now I want to eval the output.txt and act on the keyword that is written,, each key word will be a python script name. So if I sent in the subject line "status" It saves the word as status.py in output.txt, then at the bottom I tried eval output.txt but get the error Traceback (most recent call last): File "C:\Python27\New folder\start.py", line 82, in <module> start_command = eval(open("output.txt").read()) File "<string>", line 1, in <module> NameError: name 'test' is not defined
import sys import imaplib import getpass import email import email.header import datetime EMAIL_ACCOUNT = "myem...@gmail.com" EMAIL_FOLDER = "INBOX" OWNER = "willie14...@outlook.com" COMMAND_FILE = open("output.txt","w") def process_mailbox(M): rv, data = M.search(None, "From", (OWNER)) if rv != 'OK': print "No messages found!" return for num in data[0].split(): rv, data = M.fetch(num, '(RFC822)') if rv != 'OK': print "ERROR getting message", num return msg = email.message_from_string(data[0][1]) decode = email.header.decode_header(msg['Subject'])[0] subject = unicode(decode[0]) print 'Message %s: %s' % (num, subject) COMMAND_FILE.write('%s' % (subject)) COMMAND_FILE.close() #print 'Raw Date:', msg['Date'] M = imaplib.IMAP4_SSL('imap.gmail.com') try: rv, data = M.login(EMAIL_ACCOUNT, getpass.getpass()) except imaplib.IMAP4.error: print "LOGIN FAILED!!! " sys.exit(1) print rv, data rv, mailboxes = M.list() if rv == 'OK': print "Mailboxes:" print mailboxes rv, data = M.select(EMAIL_FOLDER) if rv == 'OK': print "Processing mailbox...\n" process_mailbox(M) M.select('INBOX') # select all trash M.store("1:*", '+FLAGS', '\\Deleted') #Flag all Trash as Deleted M.expunge() M.close() else: print "ERROR: Unable to open mailbox ", rv M.logout() start_command = eval(open("output.txt").read()) Traceback (most recent call last): File "C:\Python27\New folder\start.py", line 82, in <module> start_command = eval(open("output.txt").read()) File "<string>", line 1, in <module> NameError: name 'test' is not defined -----Original Message----- From: Tutor [mailto:tutor-bounces+willie14228=outlook....@python.org] On Behalf Of Alan Gauld Sent: Thursday, February 26, 2015 6:18 PM To: tutor@python.org Subject: Re: [Tutor] Use python to parse the subject line of emails, listen for and react to commands On 26/02/15 20:51, Willie Sims wrote: > I know py can parse emails, but I cant figure out how to limit to > only > the subject and only if its in that certain format > and only if its coming from a recognized email address OK, Show us what you've tried. That makes it easier for us to see what is tripping you up. Otherwise we are just guessing. All 3 things you ask for can be done but we need a starting point and the best one is your existing, but faulty, code. > and since I'm using 3.4 it seems all the other kind of examples don't > work A lot of old examples on the web will be for Python v32 and there are significant differences to v3. However, Python v3 has not lost any important functionality so whatever worked in v2 can be made to work in v3. Again we just need a clue as to what you are trying to do. (show us your code - and any error messages and tell us your OS too) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor