In this article, we will develop an application using Python. The standard library tkinter is used as the GUI library. The standard library smtplib is used for sending mail. If you understand python to some extent by reading this article, you should be able to learn how to use tkinter and smtplib to some extent.
It is one of the standard Python libraries. A library for building GUI applications. A GUI library featuring a simple grammar and quick startup.
First, let's create a window that will be.
import tkinter as tk
if __name__ == "__main__":
root = tk.Tk()
root.mainloop()
We will create a class that inherits the Frame class in order to write with object orientation in mind. Create the necessary parts (widgets) in create_widgets (), arrange them, and create the screen.
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.pack()
#Set screen size and title
master.geometry("300x200")
master.title("Simple email sending app")
self.create_widgets()
#Method to generate widget
def create_widgets(self):
self.label = tk.Label(self,text="Simple email sending app")
self.label.grid(row=0, column=0, columnspan=3)
self.to_label = tk.Label(self, text="destination:")
self.to_entry = tk.Entry(self, width=20)
self.to_label.grid(row=1, column=0)
self.to_entry.grid(row=1, column=1, columnspan=2)
self.subject_label = tk.Label(self, text="subject:")
self.subject_entry = tk.Entry(self, width=20)
self.subject_label.grid(row=2, column=0)
self.subject_entry.grid(row=2, column=1, columnspan=2)
self.body_label = tk.Label(self, text="Text:")
self.body_text = tk.Entry(self, width=20)
self.body_label.grid(row=3, column=0)
self.body_text.grid(row=3, column=1, columnspan=2)
self.button = tk.Button(self, text="Send")
self.button.grid(row=4, column=2, sticky=tk.E, pady=10)
if __name__ == "__main__":
root = tk.Tk()
Application(master=root)
root.mainloop()
Set event processing for the send button. Event processing is the processing (method) that is called when the button is pressed. Set event processing using a lambda expression in the argument command when the button is generated. Set the process to output the input value to the standard output (console) for the time being.
#Method to generate widget
def create_widgets(self):
#~ Omitted ~
self.button = tk.Button(self, text="Send", command=lambda:self.click_event())
self.button.grid(row=4, column=2, sticky=tk.E, pady=10)
def click_event(self):
to = self.to_entry.get()
subject = self.subject_entry.get()
body = self.body_text.get()
print(f"destination:{to}")
print(f"subject:{subject}")
print(f"Text:{body}")
destination:[email protected]
subject:About the other day's meeting
Text:Thank you for the meeting the other day.
A little preparation is required before creating the email sending process. This time, I will create it assuming that it will be sent from a Gmail account. First of all, if you do not have a Gmail account, please create one from the link below. If you already have one, we recommend that you get a sub-account for development. (As you can see just below, you need to lower your account security settings a bit.) https://accounts.google.com/SignUp
If you already have an account, or if you're done creating a new one, you'll need to make a few changes to your account settings to send emails from the Python program. Follow the steps below to display the "Account Setting Screen".
Then select Security from the menu on the left.
Scroll down to the bottom of the screen and turn on "Access to insecure apps".
Now you're ready to log in to Gmail from your Python program and send your email.
I will make the mail transmission processing part. First, import the library required for sending emails.
from smtplib import SMTP
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
Next, we will create a method for sending emails. The part that changes dynamically each time you send an email is received as a method argument. A constant is defined in the method for a value that does not change each time an email is sent. The ID and PASS will be the sender's Gmail email address and password. Please set this according to your own environment.
#Email sending process
def send_mail(self, to, subject, body):
#Define the information required for transmission with a constant
ID = "mail_address"
PASS = "password"
HOST = "smtp.gmail.com"
PORT = 587
#Set email body
msg = MIMEMultipart()
msg.attach(MIMEText(body, "html"))
#Set subject, source address, destination address
msg["Subject"] = subject
msg["From"] = ID
msg["To"] = to
#Connect to the SMTP server and start TLS communication
server=SMTP(HOST, PORT)
server.starttls()
server.login(ID, PASS) #Login authentication process
server.send_message(msg) #Email sending process
server.quit() #TLS communication end
Finally, if you change the event processing part to call the method created earlier, the simple mail sending application is completed.
def click_event(self):
to = self.to_entry.get()
subject = self.subject_entry.get()
body = self.body_text.get()
# print(f"destination:{to}")
# print(f"subject:{subject}")
# print(f"Text:{body}")
self.send_mail(to=to, subject=subject, body=body)
The completed source code is as follows.
import tkinter as tk
from smtplib import SMTP
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class Application(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.pack()
#Set screen size and title
master.geometry("300x200")
master.title("Simple email sending app")
self.create_widgets()
#Method to generate widget
def create_widgets(self):
self.label = tk.Label(self,text="Simple email sending app")
self.label.grid(row=0, column=0, columnspan=3)
self.to_label = tk.Label(self, text="destination:")
self.to_entry = tk.Entry(self, width=20)
self.to_label.grid(row=1, column=0)
self.to_entry.grid(row=1, column=1, columnspan=2)
self.subject_label = tk.Label(self, text="subject:")
self.subject_entry = tk.Entry(self, width=20)
self.subject_label.grid(row=2, column=0)
self.subject_entry.grid(row=2, column=1, columnspan=2)
self.body_label = tk.Label(self, text="Text:")
self.body_text = tk.Entry(self, width=20)
self.body_label.grid(row=3, column=0)
self.body_text.grid(row=3, column=1, columnspan=2)
self.button = tk.Button(self, text="Send", command=lambda:self.click_event())
self.button.grid(row=4, column=2, sticky=tk.E, pady=10)
def click_event(self):
to = self.to_entry.get()
subject = self.subject_entry.get()
body = self.body_text.get()
self.send_mail(to=to, subject=subject, body=body)
#Email sending process
def send_mail(self, to, subject, body):
#Define the information required for transmission with a constant
ID = "mail_address"
PASS = "password"
HOST = "smtp.gmail.com"
PORT = 587
#Set email body
msg = MIMEMultipart()
msg.attach(MIMEText(body, "html"))
#Set subject, source address, destination address
msg["Subject"] = subject
msg["From"] = ID
msg["To"] = to
#Connect to the SMTP server and start TLS communication
server=SMTP(HOST, PORT)
server.starttls() #TLS communication started
server.login(ID, PASS) #Login authentication process
server.send_message(msg) #Email sending process
server.quit() #TLS communication end
if __name__ == "__main__":
root = tk.Tk()
Application(master=root)
root.mainloop()
If you are interested in creating an email sending application after trying it all, restore the security settings of your Google account.
Recommended Posts