Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import smtplib, imaplib
- """
- #################################
- # Testing accounts: #
- #############################################################
- # Account: # The corresponding password:
- #############################################################
- # - 'tigaMailTest@gmail.com' # - ''
- # - 'tigaMailTest@yahoo.com' # - ''
- #############################################################
- ############################
- # TigaMail 2.1 Change log: #
- ############################
- - Favicon added
- - A single-message inbox with Gmail and Yahoo support.
- - Pre-set and custom SMTPs implemented
- - Changed the welcoming font to 'Bookman Old Style' and the size to 16
- - Yahoo support as a pre-set SMTP
- - Changed the GUIs title from 'TigaMail Client & GUI' to 'TigaMail'
- - Default values for the account type (None)
- - Added padding for the GUI (Spacing around the buttons)
- - Locked the GUI size / The GUI is no longer resizeable
- - Changed the times that SMTP and TLS connect
- - The GUI now always starts in the top left of the screen
- - Improved error handling
- ###############
- # To do list: #
- ###############
- NOT INCLUDED - Add experimental features.
- NOT INCLUDED -- Enabling and disabling of them.
- DONE -- Custom SMTPs - Idea from: Matt (Kenster).
- DONE --- A string variable attached to an entry widget.
- DONE -- Submitting of custom SMTPs upon clicking a button.
- - Hide the CMD on launch if possible.
- DONE - Implement a favicon.
- - The ability to show and hide the users password.
- - CC and BC
- - Attachments
- -- The email module
- NOT INCLUDED - Outlook support (Maybe).
- """
- class createWidgets:
- def panelA():
- filler = Label(text = "====================================", fg = "orange").grid(column = 0)
- welcomeLabel = Label(text = "Welcome to TigaMail!", fg = "DarkOrange", font = ("Bookman Old Style", 16)).grid(column = 0)
- filler = Label(text = "====================================", fg = "orange").grid(column = 0)
- usrMailLabel = Label(mailGUI, text = "Your email address:").grid(column = 0)
- usrMailEntry = Entry(mailGUI, textvariable = userAddress, fg = "Orange", width = 30).grid(column = 0)
- usrPassLabel = Label(mailGUI, text = "Your password:").grid(column = 0)
- usrPassEntry = Entry(mailGUI, show = "*", textvariable = userPass, fg = "OrangeRed", width = 39).grid(column = 0)
- usrMSGLabel = Label(mailGUI, text = "Your message:").grid(column = 0)
- usrMSGEntry = Entry(mailGUI, textvariable = userMSG, width = 39).grid(column = 0)
- recipAdressLabel = Label(mailGUI, text = "Your recipient's email address:").grid(column = 0)
- recipAdressEntry = Entry(mailGUI, textvariable = recipAddress, fg = "Orange", width = 30).grid(column = 0)
- filler = Label(text = "====================================", fg = "orange").grid(column = 0)
- updateEntry = Entry(mailGUI, textvariable = update, fg = "OrangeRed", state = "readonly", width = 39).grid(column = 0, pady = 3)
- clearButton = Button(text = "Clear fields", command = mail.clearFields, bg = "Orange").grid(column = 0, pady = 3)
- subButton = Button(text = "Submit & Send", command = mail.start, bg = "Orange").grid(column = 0, pady = 3)
- filler = Label(text = "====================================", fg = "orange").grid(column = 0)
- accountType.set(None)
- altSMTPPortInt = (None)
- infoLabel = Label(text = "My account is a...").grid(column = 0)
- gmailCheckBut = Radiobutton(text = "Gmail", variable = accountType, value = "gmail").grid(column = 0)
- yahooCheckBut = Radiobutton(text = "Yahoo", variable = accountType, value = "yahoo").grid(column = 0)
- infoLabel = Label(text = "...account.").grid(column = 0)
- altSMTPCheckBut = Radiobutton(text = "I have an alternate SMTP, whose address is...", variable = accountType, value = "altSMTP").grid(column = 0)
- altSMTPEntry = Entry(mailGUI, textvariable = altSMTPStr, fg = "Orange").grid(column = 0)
- info = Label(text = "with a port number of...").grid(column = 0)
- altSMTPPortEntry = Entry(mailGUI, textvariable = altSMTPPortInt, fg = "Orange").grid(column = 0)
- filler = Label(text = "====================================", fg = "orange").grid(column = 0)
- inboxEntry = Entry(mailGUI, textvariable = inboxText, fg = "Orange", state = "readonly", width = 39).grid(column = 0, pady = 3)
- inboxBut = Button(text = "Update inbox", command = inbox.getLastMsg, bg = "Orange").grid(column = 0, pady = 3)
- infoLab = Label(text = "The inbox currently only supports Gmail and Yahoo.", fg = "Red").grid(column = 0)
- filler = Label(text = "====================================", fg = "orange").grid(column = 0)
- return
- class inbox:
- def getLastMsg():
- """
- Returns the last email using IMAP4 for the
- chosen account type. Currently only supports
- Gmail and Yahoo accounts.
- """
- global carryFlag
- global errorList
- try:
- if accountType.get() == "gmail":
- inboxServer = imaplib.IMAP4_SSL("imap.gmail.com")
- elif accountType.get() == "yahoo":
- inboxServer = imaplib.IMAP4_SSL("imap.mail.yahoo.com")
- username = userAddress.get()
- password = userPass.get()
- messagesList = []
- inboxServer.login(username, password)
- status, ID = inboxServer.select("Inbox")
- stat, data = inboxServer.fetch(ID[0], "(UID BODY[TEXT])")
- messagesList.append(data[0][1])
- inboxText.set(data[0][1])
- messagesList = []
- except:
- carryFlag = False
- errorList.append()
- update.set("An error occured whilst retrieving the inbox.")
- return
- class mail:
- def updateErrors(errors):
- """
- Set the update bar to a list of errors
- that have occurred. Use only when an error
- has occurred.
- """
- errorStr = "Some errors were raised but avoided: "
- for item in errors:
- if str(item) not in errorStr:
- errorStr += str(item) + ", "
- return errorStr[0:len(errorStr) - 2] + "."
- def connectToSMTP():
- """
- SMTP connection using the smtplib
- module. Gets the account type from the
- radio buttons.
- """
- global carryFlag
- global errorList
- if carryFlag:
- try:
- if accountType.get() == "altSMTP":
- server = smtplib.SMTP(altSMTPStr.get(), altSMTPPortInt.get())
- else:
- if accountType.get() == "gmail":
- server = smtplib.SMTP("smtp.gmail.com", 587)
- elif accountType.get() == "yahoo":
- server = smtplib.SMTP("smtp.mail.yahoo.com", 587)
- else:
- errorList.append("Account type not chosen / valid")
- update.set("Please select an account type.")
- carryFlag = False
- update.set("Connected to the SMTP server.")
- return server
- except:
- errorList.append("SMTP connection")
- update.set(mail.updateErrors(errorList))
- carryFlag = False
- else:
- errorList.append("SMTP connection")
- update.set(mail.updateErrors(errorList))
- return
- def startTLS(server):
- """ TLS start-up. Uses the server argument passed. """
- global carryFlag
- global errorList
- if carryFlag:
- try:
- server.starttls()
- update.set("Started the TLS.")
- except:
- errorList.append("TLS connection")
- update.set(mail.updateErrors(errorList))
- carryFlag = False
- else:
- errorList.append("TLS connection")
- update.set(mail.updateErrors(errorList))
- return
- def start():
- """
- Group the functions to send the message,
- in the following order:
- - Start the SMTP
- - Start the TLS
- - Login using the credentials given
- - Send the email
- """
- server = mail.connectToSMTP()
- mail.startTLS(server)
- update.set("Logging in...")
- mail.login(server)
- update.set("Sending the email...")
- mail.sendMSG(server)
- return
- def clearFields():
- """ Clears the fields using .set() """
- update.set("Clearing fields.")
- userAddress.set("")
- userPass.set("")
- userMSG.set("")
- recipAddress.set("")
- update.set("Fields cleared.")
- def login(server):
- """
- Login process, gets the credentials
- from the entry boxes.
- """
- global carryFlag
- global errorList
- if carryFlag:
- try:
- server.login(userAddress.get(), userPass.get())
- update.set("Logged in.")
- except:
- errorList.append("Login failed")
- update.set(mail.updateErrors(errorList))
- carryFlag = False
- else:
- errorList.append("Login failed")
- update.set(mail.updateErrors(errorList))
- return
- def sendMSG(server):
- """ Sends an email """
- global carryFlag
- global errorList
- userAddressVar = userAddress.get()
- recipAddressVar = recipAddress.get()
- userMSGVar = userMSG.get()
- if carryFlag:
- try:
- server.sendmail(userAddressVar, recipAddressVar, userMSGVar)
- update.set("The email was sent.")
- except:
- update.set(mail.updateErrors(errorList))
- carryFlag = False
- else:
- errorList.append("Email failed to be sent")
- update.set(mail.updateErrors(errorList))
- return
- mailGUI = Tk()
- mailGUI.iconbitmap("C:\\Python34\\Codes\\emailClient\\tigaMail.ico")
- #mailGUI.resizable(False, False)
- #mailGUI.geometry("293x660+5+5")
- userAddress = StringVar()
- userPass = StringVar()
- userMSG = StringVar()
- recipAddress = StringVar()
- update = StringVar()
- inboxText = StringVar()
- experBool = BooleanVar()
- altSMTPStr = StringVar()
- altSMTPPortInt = IntVar()
- accountType = Variable()
- errorList = []
- carryFlag = True
- activeFlag = False
- mailGUI.title("TigaMail")
- createWidgets.panelA()
- mailGUI.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement