Commit 45c01ca3 authored by stuart.waters's avatar stuart.waters

IMAPClient

parents
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
HI.
from __future__ import unicode_literals
from imapclient import IMAPClient
import email
import socket
# Tests that there is an active internet connection
try:
print "checking internet connection.."
host = socket.gethostbyname("www.google.com")
s = socket.create_connection((host, 80), 2)
s.close()
print 'Internet Connection Working'
internet = True
except Exception,e:
print e
print "No Internet Connection"
internet = False
HOST = 'elwood.yorkdc.net'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
# If there is an active internet connection this is run
if internet == True:
try:
# asks for username and stores it within USERNAME
USERNAME = raw_input('Username: ')
# asks for password and stores it within PASSWORD
PASSWORD = raw_input('Password: ')
server.login(USERNAME,PASSWORD)
except server.Error, e:
print 'Username or Password incorrect'
#runs through code until user chooses to logout, then program terminates
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')
# Lists the folders
if user_input == '2':
list = server.list_folders(directory=u'', pattern=u'*')
print(list)
# Create a folder
if user_input == '3':
folder_name = raw_input('Please enter a name for the folder: ')
server.create_folder(folder_name)
print(folder_name + ' has been created')
# Delete a folder
if user_input == '4':
folder_name = raw_input('Please enter the name of the folder you wish to delete: ')
server.delete_folder(folder_name)
print(folder_name + ' has been deleted')
#Selecting a Folder
if user_input == '5':
folder = raw_input('Enter the folder you wish to select: ')
server.select_folder(folder, readonly = False)
print(folder + ' has been selected')
# 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')
# Copying the selected folder
if copySearch == '1':
copyTo = raw_input('Enter the name of the folder you wish to copy to: ')
server.copy(messages, copyTo)
print('Copy to ' + copyTo + ' has been successful')
# Searching the folder
else:
print server.search(criteria=u'ALL',charset=None)
# Examine a folder
if user_input == '6':
folder = raw_input('Enter the folder you wish to examine: ')
server.select_folder(folder, readonly = True)
print(folder + ' is being examined')
# Status of folder
if user_input == '7':
folder = raw_input('Enter the name of the folder you wish to see the status of: ')
print server.folder_status(folder, what=None)
# Fetches and prints the emails.
# Also writes them onto a text document for offline mode
if user_input == '8':
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()
# User logout
if user_input == '1':
print(USERNAME + ' is being logged out')
server.logout()
break
# If no internet connection then offline mode can be run
if internet == False:
user_input = raw_input('Press 1 for offline mode: ')
if user_input == '1':
f = open('emails.txt','r')
for line in f:
print line
Username = stuart.waters
Password = PQ7RDEAE
To choose something from the command line menu press the corresponding number key for the chosen option
Files included:
imap.py
emails.txt
readme.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