Click here to view code image
1: #!/usr/bin/python3
2: from tkinter import *
3: import smtplib
4:
5: class Application(Frame):
6: """Build the basic window frame template"""
7:
8: def __init__(self, master):
9: super(Application, self).__init__(master)
10: self.grid()
11: self.create_widgets()
12:
13: def create_widgets(self):
14: menubar = Menu(self)
15: menubar.add_command(label='Send', command=self.send)
16: menubar.add_command(label='Quit', command=root.quit)
17: self.label1 = Label(self, text='The Quick E-mailer')
18: self.label1.grid(row=0, columnspan=3)
19: self.label2 = Label(self, text="Enter the recipients:")
20: self.label3 = Label(self, text='Enter the Subject:')
21: self.label4 = Label(self, text='Enter your message here:')
22: self.label2.grid(row=2, column=0)
23: self.label3.grid(row=3, column=0)
24: self.label4.grid(row=4, column=0)
25:
26: self.recipients = Entry(self)
27: self.subj = Entry(self)
28: self.body = Text(self, width=50, height=10)
29: self.recipients.grid(row=2, column=1, sticky = W)
30: self.subj.grid(row=3, column=1, sticky = W)
31: self.body.grid(row=5, column=0, columnspan=2)
32:
33: self.button1 = Button(self, text="Send message",
command=self.send)
34: self.button1.grid(row=6, column=0, sticky = W)
35:
36: self.recipients.focus_set()
37: root.config(menu=menubar)
38:
39: def send(self):
40: """Retrieve the text, build the message, and send it"""
41: server = 'smtp.gmail.com'
42: port = 587
43: sender = '[email protected]'
44: password = 'xxxxxxxx'
45: to = self.recipients.get()
46: tolist = to.split(',')
47: subject = self.subj.get()
48: body = self.body.get('1.0', END)
49: header = 'To:' + to + '\n'
50: header = header + 'From:' + sender + '\n'
51: header = header + 'Subject:' + subject + '\n'
52: message = header + body
53:
54: smtpserver = smtplib.SMTP(server, port)
55: smtpserver.ehlo()
56: smtpserver.starttls()
57: smtpserver.ehlo()
58: smtpserver.login(sender, password)