Commit c359fd32 authored by gregory.swedeen's avatar gregory.swedeen

Delete main.py

parent ed24530f
import imaplib
import email
HOST = 'elwood.yorkdc.net'
ssl = False
server = imaplib.IMAP4(HOST)
Online = False
DELETED = br'\Deleted'
#If you are Greg, then the credentials to login are saved for you, if not then you will be prompted to input your username and password
def loginCredentials():
global server
print('Are you Greg Swedeen? (Y/N)')
confirmation = input()
if confirmation == 'Y':
name = 'gregory.swedeen'
passwrd = 'RS85X7WR'
elif confirmation == 'N':
print('Username: ')
name = input()
print('Password: ')
passwrd = input()
login(name, passwrd)
#Takes the credentials from before and will insert those to login to the server
def login(name, passwrd):
global server
server.login(name, passwrd)
Online = True
print("ONLINE")
def logout():
server.logout
#Shows the messages in the inbox
def inbox():
global server
status, messages = server.select('INBOX')
if status != 'OK':
print("Something went wrong")
return
#print all current emails
typ,messages = server.search(None, 'ALL')
for num in messages[0].split():
typ, fetched = server.fetch(num, '(RFC822)')
msg = email.message_from_bytes(fetched [0][1])
print("Subject: ")
print('Message %s\n%s\n' % (num, msg['Subject']))
if msg.get_content_maintype() == 'multipart':
for part in msg.walk():
if part.get_content_type() == "text/plain":
print (part.get_payload(decode=True))
#delete email option
print("1. Delete a message")
print("2. Copy a message to new folder")
print("3. Save messages for Offline viewing")
answer = input()
if answer == '1':
deleteMessage(num, messages)
elif answer == '2':
copyMessage()
elif answer == '3':
#save emails for offline use
print("Would you like to save these messages for later?(Y/N)")
answer = input()
if answer == 'Y':
saveMail()
elif anwer == 'N':
menu()
#Create a new mailbox folder
#Function to save all current emails to a word doc for offline viewing
def saveMail():
server.makedir(newDir)
newDir = '/home/gregory.swedeen/ImapClient/'
server.chdir(newDir)
file_name = BASE_NAME + num + '.eml'
fw = open(newDir + '/' + file_name, 'w', encoding = "utf-8")
fw.write( msg )
fw.close()
return
def copyMessage(): #Copy a specified email to a folder of users choice
print("Which number email would you like to copy.")
messageCopy = input()
print("Name of the folder you wish to copy the message to.")
folderCopy = input()
server.select('folderCopy')
server.copy(messageCopy, folderCopy)
print(folderCopy + 'Copied Successful')
#Parse the mailbox
def parseMailbox(data):
flags, b, c = data.partition(' ')
seperator, b, name = c.partition(' ')
return (flags, seperator.replace('""', ''), name.replace('""', ''))
#List all the folders
def listFolders():
resp, data = server.list('""', '*')
if resp == 'OK':
for mbox in data:
flags, seperator, name = parseMailbox(bytes.decode(mbox))
fmt = '{0} :[Flags = {1}; Separator = {2}'
print(fmt.format(name, flags, seperator))
menu()
#Delete the specified message
def deleteMessage(num, messages):
print("Enter the corresponding number for the message you would like to delete")
choice = input()
for num in messages[0].split():
server.store(num, '+FLAGS', '\\DELETED')
server.expunge()
#Create a new folder
def createNewFolder():
global server
print('What would you like to call your folder?')
folderName = input()
server.create(folderName)
print('You created a new folder called ' + folderName)
menu()
#Action list for the different menu options
action= {
"1":inbox,
"2":listFolders,
"3":createNewFolder,
"4":logout
}
#Menu listing the options the user may use
def menu():
global server
print('1. View all mail in the Inbox.')
print('2. View all folders.')
print('3. Create a new mailbox folder.')
print('4. Logout.')
choice = input()
action.get(choice) ()
loginCredentials()
menu()
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