Commit 8d7948d6 authored by stuart.waters's avatar stuart.waters

Final IMAP Commit

parent 45c01ca3
...@@ -2,3 +2,11 @@ Content-Type: text/plain; charset="iso-8859-1" ...@@ -2,3 +2,11 @@ Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable Content-Transfer-Encoding: quoted-printable
HI. HI.
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
Li4uLi4uLi4uLi4uLi5BIG5vYj8/Pw0KDQrwn5iKDQo=
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Woah, Look you can see this secret message
...@@ -5,95 +5,132 @@ import email ...@@ -5,95 +5,132 @@ import email
import socket import socket
# Tests that there is an active internet connection # Tests that there is an active internet connection
# Can get rid of 'www.google.com' to check offline mode works
try: try:
print "checking internet connection.." print "Checking Internet Connection"
host = socket.gethostbyname("www.google.com") host = socket.gethostbyname("www.google.com")
s = socket.create_connection((host, 80), 2) s = socket.create_connection((host, 80), 2)
s.close() s.close()
print 'Internet Connection Working'
internet = True # If internet connection then true
print 'Active Internet Connection.'
internetConnection = True
except Exception,e: except Exception,e:
print e
print "No Internet Connection"
internet = False
HOST = 'elwood.yorkdc.net' #If no internet connection then false
print "No Internet Connection."
internetConnection = False
HOST = 'elwood.yorkdc.net'
ssl = False ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl) server = IMAPClient(HOST, use_uid=True, ssl=ssl)
login = False
# If there is an active internet connection this is run # If there is an active internet connection this is run
if internet == True: if internetConnection == True:
try:
# asks for username and stores it within USERNAME # While login is false loops over asking user for username and password
# When logged in login is changed to true and the loop is broken
while login == False:
try:
# Asks for username and stores it within USERNAME
USERNAME = raw_input('Username: ') USERNAME = raw_input('Username: ')
# Asks for password and stores it within PASSWORD
# asks for password and stores it within PASSWORD
PASSWORD = raw_input('Password: ') PASSWORD = raw_input('Password: ')
# Attempts login with given username and password
server.login(USERNAME,PASSWORD) server.login(USERNAME,PASSWORD)
login = True
# If login fails then prints error
except server.Error, e: except server.Error, e:
print 'Username or Password incorrect' print 'Username or Password Incorrect. Please Try Again'
login = False
select_info = server.select_folder('INBOX')
messages = server.search(['NOT', 'DELETED'])
response = server.fetch(messages, ['FLAGS', 'RFC822.SIZE'])
#runs through code until user chooses to logout, then program terminates
# Runs through code until user chooses to logout, then program terminates
while True: while True:
user_input = raw_input('Please choose an option: \n 1: Logout \n 2: List Folders \n 3: Create Folder \n 4: Delete Folder \n 5: Select Folder \n 6: Examine \n 7: Status \n 8: Fetch \n') user_input = raw_input('Please choose an option: \n 1: Logout \n 2: List Folders \n 3: Create Folder \n 4: Delete Folder \n 5: Select Folder \n 6: Examine \n 7: Status \n 8: Fetch \n')
# Lists the folders # Lists folders
if user_input == '2': if user_input == '2':
list = server.list_folders(directory=u'', pattern=u'*') list = server.list_folders(directory=u'', pattern=u'*')
print(list) print(list)
# Create a folder # Creating a folder
if user_input == '3': if user_input == '3':
folder_name = raw_input('Please enter a name for the folder: ') folder_name = raw_input('Please enter a name for the folder: ')
server.create_folder(folder_name) server.create_folder(folder_name)
print(folder_name + ' has been created') print(folder_name + ' has been created')
# Delete a folder # Deleting a folder
if user_input == '4': if user_input == '4':
try:
folder_name = raw_input('Please enter the name of the folder you wish to delete: ') folder_name = raw_input('Please enter the name of the folder you wish to delete: ')
server.delete_folder(folder_name) server.delete_folder(folder_name)
print(folder_name + ' has been deleted') print(folder_name + ' has been deleted')
except server.Error, e:
print('Folder does not exist.')
#Selecting a Folder # Selecting a folder
if user_input == '5': if user_input == '5':
folder = raw_input('Enter the folder you wish to select: ') folder = raw_input('Enter the folder you wish to select: ')
try:
server.select_folder(folder, readonly = False) server.select_folder(folder, readonly = False)
print(folder + ' has been selected') print(folder + ' has been selected')
# When folder is selected this menu appears
# Once a folder is selected menu pops up copySearch = raw_input('Please Select if you wish to copy or search : \n 1: Copy \n 2: Search \n 3: Fetch')
copySearch = raw_input('Please Select if you wish to copy or search : \n 1: Copy \n 2: Search \n') # Copying a folder
# Copying the selected folder
if copySearch == '1': if copySearch == '1':
copyTo = raw_input('Enter the name of the folder you wish to copy to: ') copyTo = raw_input('Enter the name of the folder you wish to copy to: ')
server.copy(messages, copyTo) server.copy(messages, copyTo)
print('Copy to ' + copyTo + ' has been successful') print('Copy to ' + copyTo + ' has been successful')
# Searching the folder if copySearch == '2':
else: # Searching a folder
print server.search(criteria=u'ALL',charset=None) print server.search(criteria=u'ALL',charset=None)
else:
try:
response = server.fetch(messages, ['RFC822', 'BODY[TEXT]'])
f = open('emails.txt','w')
for msgid, data in response.iteritems():
msg = email.message_from_string(data['RFC822'])
message = msg.get_payload(0).as_string()
f.write(message)
print message
f.close()
except server.Error, e:
print('No emails to display.')
except server.Error, e:
print('Folder does not exist.')
# Examine a folder # Examine a folder
if user_input == '6': if user_input == '6':
folder = raw_input('Enter the folder you wish to examine: ') folder = raw_input('Enter the folder you wish to examine: ')
try:
server.select_folder(folder, readonly = True) server.select_folder(folder, readonly = True)
print(folder + ' is being examined') print(folder + ' is being examined')
except server.Error, e:
print('Folder does not exist.')
# Status of folder # Status of folder
if user_input == '7': if user_input == '7':
folder = raw_input('Enter the name of the folder you wish to see the status of: ') folder = raw_input('Enter the name of the folder you wish to see the status of: ')
try:
print server.folder_status(folder, what=None) print server.folder_status(folder, what=None)
except server.Error, e:
print('Folder does not exist.')
# Fetches and prints the emails. # Fetching emails in selected folder
# Also writes them onto a text document for offline mode
if user_input == '8': if user_input == '8':
try:
response = server.fetch(messages, ['RFC822', 'BODY[TEXT]']) response = server.fetch(messages, ['RFC822', 'BODY[TEXT]'])
f = open('emails.txt','w') f = open('emails.txt','w')
for msgid, data in response.iteritems(): for msgid, data in response.iteritems():
...@@ -102,15 +139,19 @@ if internet == True: ...@@ -102,15 +139,19 @@ if internet == True:
f.write(message) f.write(message)
print message print message
f.close() f.close()
except server.Error, e:
print('No emails to display.')
# User logout # Logout
if user_input == '1': if user_input == '1':
print(USERNAME + ' is being logged out') print(USERNAME + ' is being logged out')
server.logout() server.logout()
break break
# If no internet connection then offline mode can be run # If no internet connection then offline mode can be run
if internet == False: if internetConnection == False:
# Offline mode
user_input = raw_input('Press 1 for offline mode: ') user_input = raw_input('Press 1 for offline mode: ')
if user_input == '1': if user_input == '1':
f = open('emails.txt','r') f = open('emails.txt','r')
......
...@@ -3,6 +3,8 @@ Password = PQ7RDEAE ...@@ -3,6 +3,8 @@ Password = PQ7RDEAE
To choose something from the command line menu press the corresponding number key for the chosen option To choose something from the command line menu press the corresponding number key for the chosen option
To force offline mode delete 'www.google.com' from line 11. The program won't recieve a ping and therefore allow offline mode
Files included: Files included:
imap.py imap.py
emails.txt emails.txt
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment