Posts: 10
Threads: 2
Joined: Aug 2017
Hello I am very new to Python and need help with this script I found online. Every time I run it I get this error in PyCharm
FOLDER = EMPLOYMENT
NameError: name 'EMPLOYMENT' is not defined
The script is supposed to get all the email addresses in my gmail account and put them in a folder
#!/usr/bin/python
import imaplib
import sys
import email
import re
FOLDER = sys.argv[0]
FOLDER = EMPLOYMENT
LOGIN = yourusername
PASSWORD = yourpassword
IMAP_HOST = imap.gmail.com # Change this according to your provider
email_list = []
email_unique = []
mail = imaplib.IMAP4_SSL(IMAP_HOST)
mail.login(LOGIN, PASSWORD)
mail.select(FOLDER)
result, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
for i in id_list:
typ, data = mail.fetch(i,'(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
sender = msg['from'].split()[-1]
address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
email_list.append(address)
print(address)
Posts: 4,220
Threads: 97
Joined: Sep 2016
On line 9, you have EMPLOYMENT. That is a variable name, and you have not defined a value for it by line 9. Variable names have to be on the left side of an assignment before they can be on the right side of an assignment.
Posts: 10
Threads: 2
Joined: Aug 2017
Aug-08-2017, 03:39 AM
(This post was last modified: Aug-08-2017, 03:40 AM by new2py.)
Hi I'm new at this as this is my first python script I found it online. The program below is supposed to get all the email addresses from my inbox and save them to a folder named EMPLOYMENT. I got the program to run with no errors:
Process finished with exit code 0
but nothing happened, I didn't see one single email address. Could you please help me understand what I'm missing?
#!/usr/bin/python
import imaplib
import sys
import email
import re
FOLDER = sys.argv[0]
FOLDER = "EMPLOYMENT"
LOGIN = "[email protected]"
PASSWORD = "yourpassword"
IMAP_HOST = "imap.gmail.com" # Change this according to your provider
email_list = []
email_unique = []
mail = imaplib.IMAP4_SSL(IMAP_HOST)
mail.login(LOGIN, PASSWORD)
mail.select(FOLDER)
result, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
for i in id_list:
typ, data = mail.fetch(i,'(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
sender = msg['from'].split()[-1]
address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
email_list.append(address)
print(address)
Posts: 101
Threads: 7
Joined: Aug 2017
As the other answer suggested, you may want to assign value to EMPLOYMENT before line 9. Or put 'EMPLOYMENT' in quotes if its the folder name
Posts: 8,160
Threads: 160
Joined: Sep 2016
(Aug-08-2017, 03:39 AM)new2py Wrote: The program below is supposed to get all the email addresses from my inbox and save them to a folder named EMPLOYMENT
Actually it is looking for folder EMPLOYMENT in your mailbox and should search emails in this folder. I would comment out line#19 (the default is INBOX) and see what would happen. Note that it will just print e-mail addresses in the console. You are doing nothing with email_list that is created
Posts: 10
Threads: 2
Joined: Aug 2017
Thanks, I added quotes to identify the names, the script ran successfully, but nothing happened, it did nothing.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Aug-08-2017, 03:39 AM)new2py Wrote: result, data = mail.search(None, 'ALL')
That's line 21. If you add some print functions, do you get any output?
result, data = mail.search(None, 'ALL')
print(result)
print(data)
Posts: 10
Threads: 2
Joined: Aug 2017
Aug-09-2017, 05:43 PM
(This post was last modified: Aug-09-2017, 05:45 PM by new2py.)
I added the following like you said and ran in PyCharm:
#!/usr/bin/python
import imaplib
import sys
import email
import re
FOLDER = sys.argv[0]
FOLDER = "EMPLOYMENT"
LOGIN = "emailaddresshere"
PASSWORD = "passwordhere"
IMAP_HOST = "imap.gmail.com" # Change this according to your provider
email_list = []
email_unique = []
mail = imaplib.IMAP4_SSL(IMAP_HOST)
mail.login(LOGIN, PASSWORD)
mail.select(FOLDER)
result, data = mail.search(None, 'ALL')
print(result)
print(data)
ids = data[0]
id_list = ids.split()
for i in id_list:
typ, data = mail.fetch(i,'(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
sender = msg['from'].split()[-1]
address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
email_list.append(address)
print(address) and nothing happened all i got was this:
C:\Users\mattis\AppData\Local\Programs\Python\Python36\python.exe C:/Users/mattis/Documents/email_addy_extrac.py
OK
[b'']
Process finished with exit code 0
Posts: 10
Threads: 2
Joined: Aug 2017
Aug-09-2017, 06:00 PM
(This post was last modified: Aug-09-2017, 06:02 PM by new2py.)
I found this script online that is supposed to get all the email addresses in my Inbox and print them to a folder, it successfully ran in PyCharm but nothing happened. I didn't see any email addresses, please help
#!/usr/bin/python
import imaplib
import sys
import email
import re
FOLDER = sys.argv[0]
FOLDER = "EMPLOYMENT"
LOGIN = "emailaddresshere"
PASSWORD = "passwordhere"
IMAP_HOST = "imap.gmail.com" # Change this according to your provider
email_list = []
email_unique = []
mail = imaplib.IMAP4_SSL(IMAP_HOST)
mail.login(LOGIN, PASSWORD)
mail.select(FOLDER)
result, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
for i in id_list:
typ, data = mail.fetch(i,'(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
sender = msg['from'].split()[-1]
address = re.sub(r'[<>]','',sender)
# Ignore any occurences of own email address and add to list
if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list:
email_list.append(address)
print(address) result after running the script:
Process finished with exit code 0
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Aug-09-2017, 05:43 PM)new2py Wrote: OK
[b''] So you're communicating fine with the imap server, but it's telling you that there's no messages in the EMPLOYMENT folder.
Maybe try mail.list() ( https://docs.python.org/3.6/library/imap...IMAP4.list) so you can see what it is, exactly, that the server recognizes as folder names?
|