Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- class User:
- # Domain name for emails and location for inboxes
- global emailDomain
- emailDomain = "@test.local"
- global inboxLocation
- inboxLocation = "E:/Users/Dylan/Desktop/Inboxes/"
- # User Creation
- def __init__(self, firstName, lastName, password):
- self.firstName = firstName
- self.lastName = lastName
- self.userName = firstName[0] + lastName
- self.address = self.userName + emailDomain
- self.password = password
- self.inbox = inboxLocation + self.address
- # Create user inbox if it doesn't exist
- if not os.path.exists(self.inbox):
- os.mkdir(self.inbox)
- # Creates a header at the top of every page with the menu title and username
- def getHeader(self, currentMenu):
- header = (currentMenu + " | " + self.userName + "n")
- print(header)
- # Display information about a user
- def getInfo(self):
- print("User Information for %sn" % self.address)
- print("First Name: %s" % self.firstName)
- print("Last Name: %s" % self.lastName)
- print("Email Address: %s" % self.address)
- print("Inbox Location: %sn" % self.inbox)
- input("n[Enter to Continue...]n")
- self.mainMenu()
- # Create and send an email to a user's inbox
- def sendEmail(self):
- self.getHeader("New Email")
- sender = self.address
- receiver = input("Sender Address: ")
- subject = input("Subject: ")
- body = input("Body:n")
- # Create email file and store it in users inbox
- fileName = (inboxLocation + receiver + "/" + self.userName + " - " + subject + ".txt")
- emailFile = open(fileName, "w")
- emailContent = ("from: " + sender + "n" +
- "to: " + receiver + "n" +
- "subject: " + subject + "nn" +
- body)
- emailFile.write(emailContent)
- emailFile.close()
- # Confirm that email was sent
- if os.path.isfile(fileName):
- print("Email Sent! Returning to main menu.nn")
- self.mainMenu()
- else:
- print("Email Transmission failure, please try again.")
- self.sendEmail()
- # Look through a list of emails in inbox, delete function to be added
- def readEmails(self):
- self.getHeader("Inbox")
- # Iterate through inbox and assign numbers to each email for user selection
- print("Select an Email to readnnM) Main Menu")
- emailList = (os.listdir(self.inbox))
- num = 0
- for email in emailList:
- print(str(num) + ". " + email)
- num += 1
- # Ensure user input is a both a number and within range
- readChoice = input("nn>>> ")
- try:
- # Exit to email if selected
- if readChoice == "M" or readChoice == "m":
- self.mainMenu()
- # Open selected email
- else:
- readChoice = int(readChoice)
- readEmail = open(self.inbox + "/" + emailList[readChoice], "r")
- print(readEmail.read())
- readEmail.close()
- input("nn[Enter to Continue...]n")
- self.readEmails()
- # Catch bad inputs
- except TypeError or IndexError:
- print("Please enter a valid number")
- self.readEmail()
- # Main menu method for moving in and out of menus
- def mainMenu(self):
- self.getHeader("Main Menu")
- choice = input("A) Send EmailnB) InboxnC) My ProfilenD) Logoutnn>>> ")
- if choice == "a" or choice == 'A':
- self.sendEmail()
- elif choice == "b" or choice == 'B':
- self.readEmails()
- elif choice == "c" or choice == 'C':
- self.getInfo()
- elif choice == "d" or choice == 'D':
- self.logout()
- else:
- print("Invalid option, try again")
- self.mainMenu()
- # Logout method for changing users
- def logout(self):
- self.getHeader("logout")
- logoutChoice = input("are you sure you would like to log out?nn>>> ")
- if logoutChoice == "Yes" or logoutChoice == "yes" or logoutChoice == "Y" or logoutChoice == "y":
- login()
- # Login method called immediately after accounts are created
- def login():
- print("Please enter your credentialsn")
- try:
- userEmail = input("Username: ")
- password = input("Password: ")
- # Raises a NameError if user is not found
- userEmail = eval(userEmail)
- if password == userEmail.password:
- print("Login successful, Welcome to your inbox", userEmail.address)
- userEmail.mainMenu()
- # Raise NameError if password is incorrect
- else:
- raise NameError
- # Handle NameError, inform user of mistake, restart login method
- except NameError:
- print("Username or password is incorrectnn")
- login()
- # Creates users on start of program, method to create users will be added.
- # Users will be stored in file with hashed passwords.
- def userCreation():
- global jblow
- global bblow
- jblow = User("joe", "blow", "pass123")
- strudel = User("sylvester", "trudel", "P@ssw0rd")
- # Main method, initializes the program
- def main():
- userCreation()
- login()
- # Run Main
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement