Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.69 KB | None | 0 0
  1. from tkinter import *
  2. import smtplib, imaplib
  3.  
  4. """
  5.  
  6. #################################
  7. # Testing accounts:             #
  8. #############################################################
  9. # Account:                      # The corresponding password:
  10. #############################################################
  11. # - 'tigaMailTest@gmail.com'    # - ''                      
  12. # - 'tigaMailTest@yahoo.com'    # - ''                    
  13. #############################################################
  14.  
  15. ############################
  16. # TigaMail 2.1 Change log: #
  17. ############################
  18.  
  19. - Favicon added
  20. - A single-message inbox with Gmail and Yahoo support.
  21. - Pre-set and custom SMTPs implemented
  22. - Changed the welcoming font to 'Bookman Old Style' and the size to 16
  23. - Yahoo support as a pre-set SMTP
  24. - Changed the GUIs title from 'TigaMail Client & GUI' to 'TigaMail'
  25. - Default values for the account type (None)
  26. - Added padding for the GUI (Spacing around the buttons)
  27. - Locked the GUI size / The GUI is no longer resizeable
  28. - Changed the times that SMTP and TLS connect
  29. - The GUI now always starts in the top left of the screen
  30. - Improved error handling
  31.  
  32. ###############
  33. # To do list: #
  34. ###############
  35.  
  36. NOT INCLUDED        - Add experimental features.
  37. NOT INCLUDED        -- Enabling and disabling of them.
  38. DONE                -- Custom SMTPs - Idea from: Matt (Kenster).
  39. DONE                --- A string variable attached to an entry widget.
  40. DONE                -- Submitting of custom SMTPs upon clicking a button.
  41.  
  42.                    - Hide the CMD on launch if possible.
  43.  
  44. DONE                - Implement a favicon.
  45.  
  46.                         - The ability to show and hide the users password.
  47.                         - CC and BC
  48.                         - Attachments
  49.                         -- The email module
  50.  
  51. NOT INCLUDED - Outlook support (Maybe).
  52.  
  53. """
  54.  
  55. class createWidgets:
  56.    
  57.     def panelA():
  58.    
  59.         filler = Label(text = "====================================", fg = "orange").grid(column = 0)
  60.  
  61.         welcomeLabel = Label(text = "Welcome to TigaMail!", fg = "DarkOrange", font = ("Bookman Old Style", 16)).grid(column = 0)
  62.  
  63.         filler = Label(text = "====================================", fg = "orange").grid(column = 0)
  64.  
  65.         usrMailLabel = Label(mailGUI, text = "Your email address:").grid(column = 0)
  66.         usrMailEntry = Entry(mailGUI, textvariable = userAddress, fg = "Orange", width = 30).grid(column = 0)
  67.  
  68.         usrPassLabel = Label(mailGUI, text = "Your password:").grid(column = 0)
  69.         usrPassEntry = Entry(mailGUI, show = "*", textvariable = userPass, fg = "OrangeRed", width = 39).grid(column = 0)
  70.  
  71.         usrMSGLabel = Label(mailGUI, text = "Your message:").grid(column = 0)
  72.         usrMSGEntry = Entry(mailGUI, textvariable = userMSG, width = 39).grid(column = 0)
  73.  
  74.         recipAdressLabel = Label(mailGUI, text = "Your recipient's email address:").grid(column = 0)
  75.         recipAdressEntry = Entry(mailGUI, textvariable = recipAddress, fg = "Orange", width = 30).grid(column = 0)
  76.  
  77.         filler = Label(text = "====================================", fg = "orange").grid(column = 0)
  78.  
  79.         updateEntry = Entry(mailGUI, textvariable = update, fg = "OrangeRed", state = "readonly", width = 39).grid(column = 0, pady = 3)
  80.  
  81.         clearButton = Button(text = "Clear fields", command = mail.clearFields, bg = "Orange").grid(column = 0, pady = 3)
  82.         subButton = Button(text = "Submit & Send", command = mail.start, bg = "Orange").grid(column = 0, pady = 3)
  83.  
  84.         filler = Label(text = "====================================", fg = "orange").grid(column = 0)
  85.  
  86.         accountType.set(None)
  87.         altSMTPPortInt = (None)
  88.  
  89.         infoLabel = Label(text = "My account is a...").grid(column = 0)
  90.  
  91.         gmailCheckBut = Radiobutton(text = "Gmail", variable = accountType, value = "gmail").grid(column = 0)
  92.         yahooCheckBut = Radiobutton(text = "Yahoo", variable = accountType, value = "yahoo").grid(column = 0)
  93.  
  94.         infoLabel = Label(text = "...account.").grid(column = 0)
  95.  
  96.         altSMTPCheckBut = Radiobutton(text = "I have an alternate SMTP, whose address is...", variable = accountType, value = "altSMTP").grid(column = 0)
  97.         altSMTPEntry = Entry(mailGUI, textvariable = altSMTPStr, fg = "Orange").grid(column = 0)
  98.         info = Label(text = "with a port number of...").grid(column = 0)
  99.         altSMTPPortEntry = Entry(mailGUI, textvariable = altSMTPPortInt, fg = "Orange").grid(column = 0)
  100.  
  101.         filler = Label(text = "====================================", fg = "orange").grid(column = 0)
  102.  
  103.         inboxEntry = Entry(mailGUI, textvariable = inboxText, fg = "Orange", state = "readonly", width = 39).grid(column = 0, pady = 3)
  104.         inboxBut = Button(text = "Update inbox", command = inbox.getLastMsg, bg = "Orange").grid(column = 0, pady = 3)
  105.  
  106.         infoLab = Label(text = "The inbox currently only supports Gmail and Yahoo.", fg = "Red").grid(column = 0)
  107.  
  108.         filler = Label(text = "====================================", fg = "orange").grid(column = 0)
  109.  
  110.         return
  111.  
  112. class inbox:
  113.  
  114.     def getLastMsg():
  115.         """
  116.        Returns the last email using IMAP4 for the
  117.        chosen account type. Currently only supports
  118.        Gmail and Yahoo accounts.
  119.        """
  120.         global carryFlag
  121.         global errorList
  122.  
  123.         try:
  124.  
  125.             if accountType.get() == "gmail":
  126.                 inboxServer = imaplib.IMAP4_SSL("imap.gmail.com")
  127.             elif accountType.get() == "yahoo":
  128.                 inboxServer = imaplib.IMAP4_SSL("imap.mail.yahoo.com")
  129.  
  130.             username = userAddress.get()
  131.             password = userPass.get()
  132.  
  133.             messagesList = []
  134.  
  135.             inboxServer.login(username, password)
  136.             status, ID = inboxServer.select("Inbox")
  137.             stat, data = inboxServer.fetch(ID[0], "(UID BODY[TEXT])")
  138.  
  139.             messagesList.append(data[0][1])
  140.  
  141.             inboxText.set(data[0][1])
  142.  
  143.             messagesList = []
  144.  
  145.         except:
  146.             carryFlag = False
  147.             errorList.append()
  148.             update.set("An error occured whilst retrieving the inbox.")
  149.  
  150.         return
  151.  
  152. class mail:
  153.    
  154.     def updateErrors(errors):
  155.         """
  156.        Set the update bar to a list of errors
  157.        that have occurred. Use only when an error
  158.        has occurred.
  159.        """
  160.  
  161.         errorStr = "Some errors were raised but avoided: "
  162.  
  163.         for item in errors:
  164.             if str(item) not in errorStr:
  165.                 errorStr += str(item) + ", "
  166.  
  167.         return errorStr[0:len(errorStr) - 2] + "."
  168.  
  169.     def connectToSMTP():
  170.         """
  171.        SMTP connection using the smtplib
  172.        module. Gets the account type from the
  173.        radio buttons.
  174.        """
  175.         global carryFlag
  176.         global errorList
  177.  
  178.         if carryFlag:
  179.             try:
  180.                 if accountType.get() == "altSMTP":
  181.                     server = smtplib.SMTP(altSMTPStr.get(), altSMTPPortInt.get())
  182.                 else:
  183.                     if accountType.get() == "gmail":
  184.                         server = smtplib.SMTP("smtp.gmail.com", 587)
  185.                     elif accountType.get() == "yahoo":
  186.                         server = smtplib.SMTP("smtp.mail.yahoo.com", 587)
  187.                     else:
  188.                         errorList.append("Account type not chosen / valid")
  189.                         update.set("Please select an account type.")
  190.                        
  191.                         carryFlag = False
  192.  
  193.                 update.set("Connected to the SMTP server.")
  194.                 return server
  195.             except:
  196.                 errorList.append("SMTP connection")
  197.                 update.set(mail.updateErrors(errorList))
  198.                 carryFlag = False
  199.         else:
  200.             errorList.append("SMTP connection")
  201.             update.set(mail.updateErrors(errorList))
  202.  
  203.         return
  204.  
  205.     def startTLS(server):
  206.         """ TLS start-up. Uses the server argument passed. """
  207.         global carryFlag
  208.         global errorList
  209.  
  210.         if carryFlag:
  211.             try:
  212.                 server.starttls()
  213.                 update.set("Started the TLS.")
  214.             except:
  215.                 errorList.append("TLS connection")
  216.                 update.set(mail.updateErrors(errorList))
  217.                 carryFlag = False
  218.         else:
  219.             errorList.append("TLS connection")
  220.             update.set(mail.updateErrors(errorList))
  221.  
  222.         return
  223.  
  224.     def start():
  225.         """
  226.        Group the functions to send the message,
  227.        in the following order:
  228.  
  229.        - Start the SMTP
  230.        - Start the TLS
  231.        - Login using the credentials given
  232.        - Send the email
  233.        """
  234.  
  235.         server = mail.connectToSMTP()
  236.         mail.startTLS(server)
  237.  
  238.         update.set("Logging in...")
  239.         mail.login(server)
  240.  
  241.         update.set("Sending the email...")
  242.         mail.sendMSG(server)
  243.  
  244.         return
  245.  
  246.     def clearFields():
  247.         """ Clears the fields using .set() """
  248.  
  249.         update.set("Clearing fields.")
  250.  
  251.         userAddress.set("")
  252.         userPass.set("")
  253.         userMSG.set("")
  254.         recipAddress.set("")
  255.  
  256.         update.set("Fields cleared.")
  257.  
  258.     def login(server):
  259.         """
  260.        Login process, gets the credentials
  261.        from the entry boxes.
  262.        """
  263.         global carryFlag
  264.         global errorList
  265.  
  266.         if carryFlag:
  267.             try:
  268.                 server.login(userAddress.get(), userPass.get())
  269.                 update.set("Logged in.")
  270.             except:
  271.                 errorList.append("Login failed")
  272.                 update.set(mail.updateErrors(errorList))
  273.                 carryFlag = False
  274.         else:
  275.             errorList.append("Login failed")
  276.             update.set(mail.updateErrors(errorList))
  277.  
  278.         return
  279.  
  280.     def sendMSG(server):
  281.         """ Sends an email """
  282.         global carryFlag
  283.         global errorList
  284.  
  285.         userAddressVar = userAddress.get()
  286.         recipAddressVar = recipAddress.get()
  287.         userMSGVar = userMSG.get()
  288.  
  289.         if carryFlag:
  290.             try:
  291.                 server.sendmail(userAddressVar, recipAddressVar, userMSGVar)
  292.                 update.set("The email was sent.")
  293.             except:
  294.                 update.set(mail.updateErrors(errorList))
  295.                 carryFlag = False
  296.         else:
  297.             errorList.append("Email failed to be sent")
  298.             update.set(mail.updateErrors(errorList))
  299.  
  300.         return
  301.  
  302. mailGUI = Tk()
  303.  
  304. mailGUI.iconbitmap("C:\\Python34\\Codes\\emailClient\\tigaMail.ico")
  305. #mailGUI.resizable(False, False)
  306. #mailGUI.geometry("293x660+5+5")
  307.  
  308. userAddress = StringVar()
  309. userPass = StringVar()
  310.  
  311. userMSG = StringVar()
  312. recipAddress = StringVar()
  313.  
  314. update = StringVar()
  315. inboxText = StringVar()
  316.  
  317. experBool = BooleanVar()
  318.  
  319. altSMTPStr = StringVar()
  320. altSMTPPortInt = IntVar()
  321.  
  322. accountType = Variable()
  323.  
  324. errorList = []
  325.  
  326. carryFlag = True
  327. activeFlag = False
  328.  
  329. mailGUI.title("TigaMail")
  330.  
  331. createWidgets.panelA()
  332.  
  333. mailGUI.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement