Advertisement
Guest User

Email Client GUI v2 Error 001

a guest
Jun 17th, 2013
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.99 KB | None | 0 0
  1. #Email Send v2 Program. It has a basic GUI to send emails.
  2.  
  3. from Tkinter import * #GUI Module
  4. import smtplib #SMTP Library
  5. import linecache
  6. import imaplib #Import the IMAP library
  7. import email #Email Parsing Library
  8.  
  9.  
  10.  
  11. #----------------------------------
  12.  
  13.  
  14. class Authen(Frame): #Authentication Class. First Popup Window
  15.     """Holds authentication code and basic GUI stuff"""
  16.     def __init__(self, master):
  17.         """Start Authen GUI"""
  18.         Frame.__init__(self, master)
  19.         self.grid()
  20.         self.Create_Widgets()
  21.         self.Funcurrent()
  22.  
  23.        
  24.     def Create_Widgets(self):
  25.         """Spawns the widgets"""
  26.  
  27.         #Username Label
  28.         self.Usertext = Label(self, text = "Username: ")
  29.         self.Usertext.grid(row = 0, column = 0)
  30.  
  31.         #Username Entry
  32.         self.Username = Entry(self)
  33.         self.Username.grid(row = 0, column = 1)
  34.         self.Username.insert("0", linecache.getline("current.txt", 1))
  35.  
  36.         #Password Label
  37.         self.Passtext = Label (self, text = "Password: ")
  38.         self.Passtext.grid(row = 1, column = 0)
  39.  
  40.         #Password Entry
  41.         self.Password = Entry (self, show = "*")
  42.         self.Password.grid(row = 1, column = 1)
  43.         self.Password.insert("0", linecache.getline("current.txt",2))
  44.  
  45.         #Submit Credits Button
  46.         self.Submit = Button(self, text = "Submit Credits",
  47.                         command = self.remember_Authen)
  48.         self.Submit.grid( row = 2, column = 0)
  49.  
  50.         #Create the Remember-Me Checkbutton
  51.  
  52.         self.remember = BooleanVar()
  53.  
  54.         check = Checkbutton(self,
  55.                             text = "Remember Me",
  56.                             variable = self.remember#command is carried out by
  57.                             #the submit button
  58.                             )
  59.         check.grid(row = 2, column =1)
  60.         check.select()
  61.  
  62.     def remember_me(self):
  63.         if self.remember.get():
  64.             current = open("current.txt", "w")
  65.             current.write("%s\n" % self.Username.get())
  66.             current.write("%s" % self.Password.get())
  67.             current.close()
  68.         else: #Clear the file
  69.             current = open("current.txt", "w")
  70.             current.close()
  71.  
  72.     def Funcurrent(self):
  73.         current = open("current.txt", "r")
  74.         self.user_line = linecache.getline("current.txt", 1)
  75.         self.password_line = linecache.getline("current.txt", 2)
  76.        
  77.        
  78.     def authen_credits(self):
  79.         """Backbone of authen process"""
  80.  
  81.         #Get the Username and Password
  82.         Username_mine = self.Username.get()
  83.         Password_mine = self.Password.get()
  84.  
  85.         #Store for future use
  86.         entry = open("%s.txt" % (Username_mine), "w")
  87.         entry.write("USERNAME: %s\n" % (Username_mine))
  88.         entry.write("PASSWORD: %s\n" % (Password_mine))
  89.         entry.close()
  90.  
  91.         #Contact the Gmail Server and Sign In
  92.         server = smtplib.SMTP('smtp.gmail.com:587')
  93.         server.ehlo()
  94.         server.starttls()
  95.        
  96.         try:
  97.             server.login(Username_mine,Password_mine)
  98.             #make my screen dimensions work
  99.             w = 750
  100.             h = 1000
  101.             app = Main_Window()
  102.             app.title("SMTP Mail Client")
  103.             app.geometry("%dx%d" % (w, h))
  104.            
  105.  
  106.         #If there was an error, print it in the console.
  107.         except smtplib.SMTPAuthenticationError:
  108.             print "Login Failed"
  109.  
  110.     def remember_Authen(self):
  111.         self.authen_credits()
  112.         self.remember_me()
  113.        
  114.  
  115.  
  116.  
  117.  
  118. class Send_Mail(Toplevel): #This is where you send your mail
  119.     """Toplevel Frame, holds sendmail stuff"""
  120.     def __init__(self):
  121.         """Start the toplevel Frame"""
  122.         Toplevel.__init__(self)
  123.         self.grid()
  124.         self.sideFun()
  125.         self.entryFun()
  126.         self.mainFun()
  127.        
  128.  
  129.     def sideFun(self):
  130.         """Sidebar Method"""
  131.  
  132.         #Sidebar Frame
  133.         self.sideFrame = Frame(self, background = "black", width = 100, height = 1000)
  134.         self.sideFrame.grid(row = 0, column = 0, sticky = W, rowspan = 4, padx = 10)
  135.         self.sideFrame.grid_propagate(0)
  136.  
  137.         #Label
  138.         self.sideLabel = Label (self.sideFrame, text = "Toolbar\n")
  139.         self.sideLabel.grid()
  140.  
  141.         #Send Mail Button
  142.         self.sendmailbttn = Button(self.sideFrame, text = "Send Mail")
  143.         self.sendmailbttn.grid(sticky = N)
  144.  
  145.         #Read Mail Button
  146.         self.readmailbttn = Button(self.sideFrame, text = "Read Mail")
  147.         self.readmailbttn.grid(sticky = N)
  148.        
  149.        
  150.     def entryFun(self):
  151.         """Holds entry Frame"""
  152.         #This is the Frame
  153.         self.entryFrame = Frame(self)
  154.         self.entryFrame.grid(row = 0, column = 1, sticky = NW)
  155.  
  156.         #To Label
  157.         self.To_Lbl = Label(self.entryFrame, text = "To:  ")
  158.         self.To_Lbl.grid(row = 0, column = 0, sticky = W)
  159.  
  160.         #To Entry
  161.         self.To_Entry = Entry(self.entryFrame)
  162.         self.To_Entry.grid(row = 0, column = 1, sticky = W)
  163.  
  164.         #Subject Label
  165.         self.Subject_Lbl = Label(self.entryFrame, text = "Subject:  ")
  166.         self.Subject_Lbl.grid(row = 1, column = 0, sticky = W)
  167.  
  168.         #Subject Entry
  169.         self.Subject_Entry = Entry(self.entryFrame)
  170.         self.Subject_Entry.grid(row = 1, column = 1, sticky = W)
  171.        
  172.     def mainFun(self):
  173.         """Holds body of thing"""
  174.  
  175.         #Main Frame
  176.         self.entryFrame = Frame(self)
  177.         self.entryFrame.grid(row = 1, column = 1, sticky = N)
  178.  
  179.         #Where you enter your message
  180.         self.Body = Text(self.entryFrame, width = 75,
  181.                          height = 25, relief = GROOVE,
  182.                          bd = 5, wrap = WORD)
  183.         self.Body.grid(row = 0, column = 0)
  184.  
  185.         #The Sendmail Button
  186.         self.send_mail = Button(self, text = "Send Mail",
  187.                                 command = self.send_cmd)
  188.         self.send_mail.grid(row = 2, column = 1, sticky = N)
  189.  
  190.     def send_cmd(self):
  191.  
  192.         #Get the Username, Password, and the message contents
  193.         username = passcheck.Username.get()
  194.         password = passcheck.Password.get()
  195.         contents = self.Body.get("0.0", END)
  196.  
  197.         #0000 ERROR: Figure out how to get the FROM address
  198.         FROM = "Unknown"
  199.  
  200.         #Get the subject and recipients
  201.         subject = self.Subject_Entry.get()
  202.         recipients = self.To_Entry.get()
  203.  
  204.         #Craft the Message subject and contents
  205.         message = "Subject: %s\n%s" % (subject, contents)
  206.  
  207.         #Connect to the server
  208.         server = smtplib.SMTP('smtp.gmail.com:587')
  209.         server.ehlo()
  210.         server.starttls()
  211.  
  212.         #Login
  213.         server.login(username, password)
  214.  
  215.         #Send the Email
  216.         server.sendmail(FROM, recipients, message)
  217.  
  218. class Main_Window(Toplevel): #This is where you recieve emails
  219.     """Toplevel Frame, holds sendmail stuff"""
  220.     def __init__(self):
  221.         """Start the toplevel Frame"""
  222.         Toplevel.__init__(self)
  223.         self.grid()
  224.         self.sideFun()
  225.         self.mainFun()
  226.        
  227.  
  228.     def sideFun(self):
  229.         """Sidebar Method"""
  230.  
  231.         #Sidebar Frame
  232.         self.sideFrame = Frame(self, background = "black", width = 100, height = 1000)
  233.         self.sideFrame.grid(row = 0, column = 0, sticky = W, rowspan = 4, padx = 10)
  234.         self.sideFrame.grid_propagate(0)
  235.  
  236.         #Label
  237.         self.sideLabel = Label (self.sideFrame, text = "Toolbar\n")
  238.         self.sideLabel.grid()
  239.  
  240.         #Send Mail Button
  241.         self.sendmailbttn = Button(self.sideFrame, text = "Send Mail")
  242.         self.sendmailbttn.grid(sticky = N)
  243.  
  244.         #Read Mail Button
  245.         self.readmailbttn = Button(self.sideFrame, text = "Read Mail")
  246.         self.readmailbttn.grid(sticky = N)
  247.        
  248.        
  249.  
  250.        
  251.     def mainFun(self):
  252.         """Holds body of thing"""
  253.  
  254.         #Main Frame
  255.         self.entryFrame = Frame(self)
  256.         self.entryFrame.grid(row = 1, column = 1, sticky = N)
  257.  
  258.         mail = imaplib.IMAP4_SSL('imap.gmail.com')#Connect to the Server
  259.         mail.login('[email protected]', 'myPassword')#Login
  260.         mail.list() #Different Gmail Labels
  261.         mail.select("inbox") #Select the Label "inbox"
  262.  
  263.         result, data = mail.uid('search',None, "ALL")
  264.  
  265.         uid = data[0] #The data is a list
  266.         uid_list = uid.split() #ids is a space separated string
  267.         latest_email_uid = uid_list[-2] #Get the Latest
  268.  
  269.         for i in range(1,11):
  270.             """
  271.            x = -i
  272.            email_uid = uid_list[x]
  273.            result, data = mail.uid('fetch',email_uid, "(RFC822)") #Fetch the Email's body (RFC822) for the given ID
  274.  
  275.            raw_email = data[0][1] #Raw data of the Email - needs to be parsed. Includes headers and the body
  276.  
  277.            email_message = email.message_from_string(raw_email) #Convert the raw email into string format
  278.            print "Email %d:  %s\n" % (i, email_message['Subject'])
  279.            """
  280.             self.newmessage = Button(self, text = "%d" % i)
  281.             Main_Window.entryFrame.append(self.newmessage)
  282.  
  283.  
  284.     def send_cmd(self):
  285.  
  286.         #Get the Username, Password, and the message contents
  287.         username = passcheck.Username.get()
  288.         password = passcheck.Password.get()
  289.         contents = self.Body.get("0.0", END)
  290.  
  291.         #0000 ERROR: Figure out how to get the FROM address
  292.         FROM = "Unknown"
  293.  
  294.         #Get the subject and recipients
  295.         subject = self.Subject_Entry.get()
  296.         recipients = self.To_Entry.get()
  297.  
  298.         #Craft the Message subject and contents
  299.         message = "Subject: %s\n%s" % (subject, contents)
  300.  
  301.         #Connect to the server
  302.         server = smtplib.SMTP('smtp.gmail.com:587')
  303.         server.ehlo()
  304.         server.starttls()
  305.  
  306.         #Login
  307.         server.login(username, password)
  308.  
  309.         #Send the Email
  310.         server.sendmail(FROM, recipients, message)
  311.  
  312.  
  313.        
  314. #Geometry and Root Stuff
  315. root = Tk()
  316. root.title("Login")
  317. root.geometry("500x500")
  318. passcheck = Authen(root)
  319.  
  320. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement