Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Email Send v2 Program. It has a basic GUI to send emails.
- from Tkinter import * #GUI Module
- import smtplib #SMTP Library
- import linecache
- import imaplib #Import the IMAP library
- import email #Email Parsing Library
- #----------------------------------
- class Authen(Frame): #Authentication Class. First Popup Window
- """Holds authentication code and basic GUI stuff"""
- def __init__(self, master):
- """Start Authen GUI"""
- Frame.__init__(self, master)
- self.grid()
- self.Create_Widgets()
- self.Funcurrent()
- def Create_Widgets(self):
- """Spawns the widgets"""
- #Username Label
- self.Usertext = Label(self, text = "Username: ")
- self.Usertext.grid(row = 0, column = 0)
- #Username Entry
- self.Username = Entry(self)
- self.Username.grid(row = 0, column = 1)
- self.Username.insert("0", linecache.getline("current.txt", 1))
- #Password Label
- self.Passtext = Label (self, text = "Password: ")
- self.Passtext.grid(row = 1, column = 0)
- #Password Entry
- self.Password = Entry (self, show = "*")
- self.Password.grid(row = 1, column = 1)
- self.Password.insert("0", linecache.getline("current.txt",2))
- #Submit Credits Button
- self.Submit = Button(self, text = "Submit Credits",
- command = self.remember_Authen)
- self.Submit.grid( row = 2, column = 0)
- #Create the Remember-Me Checkbutton
- self.remember = BooleanVar()
- check = Checkbutton(self,
- text = "Remember Me",
- variable = self.remember#command is carried out by
- #the submit button
- )
- check.grid(row = 2, column =1)
- check.select()
- def remember_me(self):
- if self.remember.get():
- current = open("current.txt", "w")
- current.write("%s\n" % self.Username.get())
- current.write("%s" % self.Password.get())
- current.close()
- else: #Clear the file
- current = open("current.txt", "w")
- current.close()
- def Funcurrent(self):
- current = open("current.txt", "r")
- self.user_line = linecache.getline("current.txt", 1)
- self.password_line = linecache.getline("current.txt", 2)
- def authen_credits(self):
- """Backbone of authen process"""
- #Get the Username and Password
- Username_mine = self.Username.get()
- Password_mine = self.Password.get()
- #Store for future use
- entry = open("%s.txt" % (Username_mine), "w")
- entry.write("USERNAME: %s\n" % (Username_mine))
- entry.write("PASSWORD: %s\n" % (Password_mine))
- entry.close()
- #Contact the Gmail Server and Sign In
- server = smtplib.SMTP('smtp.gmail.com:587')
- server.ehlo()
- server.starttls()
- try:
- server.login(Username_mine,Password_mine)
- #make my screen dimensions work
- w = 750
- h = 1000
- app = Main_Window()
- app.title("SMTP Mail Client")
- app.geometry("%dx%d" % (w, h))
- #If there was an error, print it in the console.
- except smtplib.SMTPAuthenticationError:
- print "Login Failed"
- def remember_Authen(self):
- self.authen_credits()
- self.remember_me()
- class Send_Mail(Toplevel): #This is where you send your mail
- """Toplevel Frame, holds sendmail stuff"""
- def __init__(self):
- """Start the toplevel Frame"""
- Toplevel.__init__(self)
- self.grid()
- self.sideFun()
- self.entryFun()
- self.mainFun()
- def sideFun(self):
- """Sidebar Method"""
- #Sidebar Frame
- self.sideFrame = Frame(self, background = "black", width = 100, height = 1000)
- self.sideFrame.grid(row = 0, column = 0, sticky = W, rowspan = 4, padx = 10)
- self.sideFrame.grid_propagate(0)
- #Label
- self.sideLabel = Label (self.sideFrame, text = "Toolbar\n")
- self.sideLabel.grid()
- #Send Mail Button
- self.sendmailbttn = Button(self.sideFrame, text = "Send Mail")
- self.sendmailbttn.grid(sticky = N)
- #Read Mail Button
- self.readmailbttn = Button(self.sideFrame, text = "Read Mail")
- self.readmailbttn.grid(sticky = N)
- def entryFun(self):
- """Holds entry Frame"""
- #This is the Frame
- self.entryFrame = Frame(self)
- self.entryFrame.grid(row = 0, column = 1, sticky = NW)
- #To Label
- self.To_Lbl = Label(self.entryFrame, text = "To: ")
- self.To_Lbl.grid(row = 0, column = 0, sticky = W)
- #To Entry
- self.To_Entry = Entry(self.entryFrame)
- self.To_Entry.grid(row = 0, column = 1, sticky = W)
- #Subject Label
- self.Subject_Lbl = Label(self.entryFrame, text = "Subject: ")
- self.Subject_Lbl.grid(row = 1, column = 0, sticky = W)
- #Subject Entry
- self.Subject_Entry = Entry(self.entryFrame)
- self.Subject_Entry.grid(row = 1, column = 1, sticky = W)
- def mainFun(self):
- """Holds body of thing"""
- #Main Frame
- self.entryFrame = Frame(self)
- self.entryFrame.grid(row = 1, column = 1, sticky = N)
- #Where you enter your message
- self.Body = Text(self.entryFrame, width = 75,
- height = 25, relief = GROOVE,
- bd = 5, wrap = WORD)
- self.Body.grid(row = 0, column = 0)
- #The Sendmail Button
- self.send_mail = Button(self, text = "Send Mail",
- command = self.send_cmd)
- self.send_mail.grid(row = 2, column = 1, sticky = N)
- def send_cmd(self):
- #Get the Username, Password, and the message contents
- username = passcheck.Username.get()
- password = passcheck.Password.get()
- contents = self.Body.get("0.0", END)
- #0000 ERROR: Figure out how to get the FROM address
- FROM = "Unknown"
- #Get the subject and recipients
- subject = self.Subject_Entry.get()
- recipients = self.To_Entry.get()
- #Craft the Message subject and contents
- message = "Subject: %s\n%s" % (subject, contents)
- #Connect to the server
- server = smtplib.SMTP('smtp.gmail.com:587')
- server.ehlo()
- server.starttls()
- #Login
- server.login(username, password)
- #Send the Email
- server.sendmail(FROM, recipients, message)
- class Main_Window(Toplevel): #This is where you recieve emails
- """Toplevel Frame, holds sendmail stuff"""
- def __init__(self):
- """Start the toplevel Frame"""
- Toplevel.__init__(self)
- self.grid()
- self.sideFun()
- self.mainFun()
- def sideFun(self):
- """Sidebar Method"""
- #Sidebar Frame
- self.sideFrame = Frame(self, background = "black", width = 100, height = 1000)
- self.sideFrame.grid(row = 0, column = 0, sticky = W, rowspan = 4, padx = 10)
- self.sideFrame.grid_propagate(0)
- #Label
- self.sideLabel = Label (self.sideFrame, text = "Toolbar\n")
- self.sideLabel.grid()
- #Send Mail Button
- self.sendmailbttn = Button(self.sideFrame, text = "Send Mail")
- self.sendmailbttn.grid(sticky = N)
- #Read Mail Button
- self.readmailbttn = Button(self.sideFrame, text = "Read Mail")
- self.readmailbttn.grid(sticky = N)
- def mainFun(self):
- """Holds body of thing"""
- #Main Frame
- self.entryFrame = Frame(self)
- self.entryFrame.grid(row = 1, column = 1, sticky = N)
- mail = imaplib.IMAP4_SSL('imap.gmail.com')#Connect to the Server
- mail.list() #Different Gmail Labels
- mail.select("inbox") #Select the Label "inbox"
- result, data = mail.uid('search',None, "ALL")
- uid = data[0] #The data is a list
- uid_list = uid.split() #ids is a space separated string
- latest_email_uid = uid_list[-2] #Get the Latest
- for i in range(1,11):
- """
- x = -i
- email_uid = uid_list[x]
- result, data = mail.uid('fetch',email_uid, "(RFC822)") #Fetch the Email's body (RFC822) for the given ID
- raw_email = data[0][1] #Raw data of the Email - needs to be parsed. Includes headers and the body
- email_message = email.message_from_string(raw_email) #Convert the raw email into string format
- print "Email %d: %s\n" % (i, email_message['Subject'])
- """
- self.newmessage = Button(self, text = "%d" % i)
- Main_Window.entryFrame.append(self.newmessage)
- def send_cmd(self):
- #Get the Username, Password, and the message contents
- username = passcheck.Username.get()
- password = passcheck.Password.get()
- contents = self.Body.get("0.0", END)
- #0000 ERROR: Figure out how to get the FROM address
- FROM = "Unknown"
- #Get the subject and recipients
- subject = self.Subject_Entry.get()
- recipients = self.To_Entry.get()
- #Craft the Message subject and contents
- message = "Subject: %s\n%s" % (subject, contents)
- #Connect to the server
- server = smtplib.SMTP('smtp.gmail.com:587')
- server.ehlo()
- server.starttls()
- #Login
- server.login(username, password)
- #Send the Email
- server.sendmail(FROM, recipients, message)
- #Geometry and Root Stuff
- root = Tk()
- root.title("Login")
- root.geometry("500x500")
- passcheck = Authen(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement