current position:Home>GUI interface mail sending program based on SMTP protocol server and python language

GUI interface mail sending program based on SMTP protocol server and python language

2022-05-15 05:55:47SteveDraw

One , Development needs and ideas

1. Development requirements :

Do you have such a need : Using mailbox (qq Email or 163 mailbox ) when , Many times we need to log in again ( Or at intervals ), So if you forget your password , Resetting will be a painful thing , Combined with the , We use email very often , Can there be a product that allows us not to log in to our account , At the same time, like desktop applications, you don't have to log in to the web mailbox website , So as to meet our basic function of sending e-mail ? The answer is yes !
Open source IT Time , We have many methods and tools to achieve our needs , As a geek , We can find out SMTP A special email sending process behind the protocol : Based on mailbox service provider server (IMAP,SMTP,POS3 etc. ) To bind the corresponding program , So as to achieve login free , The secondary program development realizes the mail sending function ( Just a simple understanding , Baidu ).
 Insert picture description here

2. The development train of thought :

python Is a super language with a powerful third-party library , We can choose a simple and easy-to-use third-party library controllable , After comparison, the author found that python Of yagmail Library is a good choice , in addition , What we need to develop is product level ( Simple , Hee hee ), Then you can't open the code at runtime , That would be troublesome , We need to design something with UI Interactive interface program , because python Bring their own tkinter library , We can use it to GUI Development is relatively convenient and simple .
 Insert picture description here

3. Preparation

Summarize the previous , We need to be prepared and familiar with the following things :
(1) Set up the server settings of the mailbox service provider , Prepare the corresponding service password ( Not the account login password );
(2)python Of yagmail library , Know how to use ;
(3) Make sure you bring your own tkinter The package exists ( Generally no problem ), Know how to use ;
Because the author has made this introduction before , I won't go into details here , Here's a link :
python- be based on yagmail Library to develop automatic mail sending program

4. Functional description

 Insert picture description here

Two , Write code

1. Handler module

Be careful : Privacy concerns in the code , Are shielded , The corresponding content should be introduced according to your actual situation

from tkinter import *  #tk Graphics library , One of the standard libraries 
from PIL import Image,ImageTk# Because we set the window picture background , So introduce 
import tkinter.filedialog#tk The file selection box module in the library 
import yagmail# Third party library sent by email 
import time# Used to display the time 
import datetime# Used to obtain and display calendar functions on the calendar 
import calendar
root = Tk()# Program main form 

# Set the global variables , In case of program recognition error 
global to, title, content,user, password, port, host

#yagmail Library basic parameter settings 
user = ''  #  Corresponding to your owner's mailbox 
password = ''   #  The service password corresponding to your email application , The above steps are introduced 
port = '465'  #  port 
host = ''  #  The email address of the server corresponding to 
mail = yagmail.SMTP(user, password, host, port)#yagmail Basic configuration of library 

# Control instance'background1.png')# The same as the picture file in the code file directory , For the background ,
background_image1 = ImageTk.PhotoImage(photo1.resize((500, 400)))
to_text = Text(root,width=26,height=1.55,font=(' In black ',13))# Recipient input box 
title_text = Text(root,width=26,height=1.55,font=(' In black ',13))# Mail subject input box 
content_text = Text(root,width=75,height=12,font=(' In black ',13))# Mail content input box 
tipout_label = Label(root,text=' No operation yet !',font=(' In black ',13),bg='green',fg='white')# The text content corresponds to the operation prompt of the actual operation program 
time_label=Label(root,text='',font=(' In black ',16),fg='green')# For time display 
menubar=Menu(root)# The main menu 
menuout=Menu(root,tearoff=0)# Pop up menu , Clear the collection of function realization 
root['menu']=menubar# Set up the main menu 

def gettime():# utilize time Library function to get the current time 
    timestr = time.strftime("%Y year %m month %d Japan  %H when :%M branch :%S second ,%A")# Get and format the output time 
    root.after(1000,gettime)# Every second ( Corresponding 1000) Refresh time 
gettime()# Call the time display function 

def show_datetime():# utilize calendar Library functions for displaying calendars 
    winnew=Toplevel(root)# Top level form 
    winnew.title(' Calendar window ')
    winnew.iconbitmap('mail.ico')# Set window icons , Use the same 
    tipout_label['text'] = ' You opened the calendar window !'# Operation change prompt , Basically, every module contains
    year=date_time.year# Get current year 
    month=date_time.month# Get current month 
    calendar.setfirstweekday(firstweekday=6)# Set the initial day of the calendar ( The first day )
    dates=calendar.month(year,month)# Get the string type of calendar 
    alldate=Label(winnew,text='',bg='green',fg='white',width=1000,height=600,font=(' In black ',20),image=background_image1,compound = CENTER)

global lists,textbooks# Label global variables , Under the main form frame logic , You can still have memory function when closing the window , So as to realize the saving function 
textbooks=Label(root,text=' nothing ')
lists=['']# Because the first list element is displayed later , So the initial element is set to an empty string first 
def textbook():
    text_win.title(' Draft window ')
    tipout_label['text'] = ' You opened the draft window !'
    draft_paper=Text(text_win,width=100,height=30)# This example is the function of draft box input and text display box 
    draft_paper.insert('1.0', lists[0])# Insert saved content , Quite a memory , The logic of saving function is realized 
    def saves():
        textbooks .configure(text=draft_paper.get(1.0, 'end'))
        lists[0]=textbooks['text']# Click the save button , Trigger to get the content of the text box , Can be saved at any time during the writing process , Avoid losing 
        tipout_label['text'] = " You saved the contents of the draft box !"
    def clears():
        draft_paper.delete('1.0', 'end')
        tipout_label['text'] = " You emptied the contents of the draft box !"
    save_button = Button(text_win, text=' preservation ', command=saves,relief=RAISED,fg='white',bg='green',font=(' In black ',14),width=10,height=2).place(x=19,y=430)
    clear_button=Button(text_win, text=' Empty all content ', command=clears,relief=RAISED,fg='white',bg='green',font=(' In black ',14),width=16,height=2).place(x=150,y=430),y=18)

# Add menu items :
menubar.add_command(label = "  draft  ", command=textbook)
menubar.add_command(label = "  The calendar  ", command=show_datetime)

def pops(event):# Right click the response function , Pop up menu, event.y_root)# These two variables can pop up at any trigger part of the window 

def Clear_to():# Empty recipient input box function 
    to_text.delete('1.0', 'end')
    tipout_label['text']=' You cleared the recipient window !'

def Clear_title():# Empty the mail subject input box function 
    title_text.delete('1.0', 'end')
    tipout_label['text'] =' You empty the theme window !'

def Clear_content():# Empty mail content input box function 
    content_text.delete('1.0', 'end')
    tipout_label['text'] =' You empty the content window !'

# Used for error message prompt in other modules , Call when using 
def message():
    tipout_label['text'] = " The input is incorrect or has been completely cleared ! Please re-enter everything !"
    to_text.delete('1.0', 'end')# The position of the first character in the text box control is  1.0, You can use numbers  1.0  Or a string "1.0" To express 
    title_text.delete('1.0', 'end')
    content_text.delete('1.0', 'end')

filename=''# File address variable , Initialize first 
# Attachment control function 
def Attachments():
    global filename# Within the function , Global variables need to be declared , Available Sendto Use 
    filename = tkinter.filedialog.askopenfilename()# Select File... In the pop-up box , And get the string file address 
    if filename != '':# use filename Whether it is an empty string , Whether the file attachment function and identification are relevant 
        tipout_label['text']=' The file you selected is ' + filename
        tipout_label['text']=' You have not selected any files '

# The main function : Process mail delivery 
def Sendto():
    to = to_text.get('1.0', 'end')# Get relevant content 
    title = title_text.get('1.0', 'end')
    content = content_text.get('1.0', 'end')
    if filename =='':# according to filename Value to select whether to include attachments when sending 
        try:  #  It is important to check whether the mail is sent successfully ;
            mail.send(to, title, content)
            tipout_label['text'] = " Send successfully !"
    elif filename !='':
            mail.send(to, title, content,filename)
            tipout_label['text'] = " Send successfully !"

root.bind("<Button-3>",pops)#<Button-3> For right-click Events , Used to trigger pop-up menus 
menuout.add_command(label=' Empty the recipient window ',command=Clear_to)
menuout.add_command(label=' Empty the theme window ',command=Clear_title)
menuout.add_command(label=' Empty the content window ',command=Clear_content)
menuout.add_command(label=' Clear all input windows ',command=message)

# Control property settings 
to_label=Label(root,text=' The recipient :',font=(' In black ',13)).place(x=10,y=20)# Recipient prompt label 
title_label=Label(root,text=' The theme :',font=(' In black ',13)).place(x=10,y=80)# Theme Tags 
contents_label=Label(root,text=' Content :',font=(' In black ',13)).place(x=10,y=140)# Content labels,y=20),y=80),y=140)
attachments_button=Button(root,text=' Attachment address ',font=(' In black ',13),fg='white',bg='green',command=Attachments,width=10,height=2,relief=RAISED),y=370)
check_button=Button(root,text=' Confirm sending ',font=(' In black ',13),fg='white',bg='green',command=Sendto,width=10,height=2,relief=RAISED).place(x=30,y=450)
tip_label=Label(root,text=' Program status :',font=(' In black ',13)).place(x=10,y=530),y=530),y=85)

# Main window settings 
root.title(' be based on SMTP Mailbox program automatically sent by protocol ')# Set the window title 
root.geometry('800x600+300+100')# Set the window size and position under the system display 
root.iconbitmap('mail.ico')# Set the form icon 

2. Operation effect display :

 Insert picture description here

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

3、 ... and , Program packaging

During this development , The third-party libraries used have been installed locally ,pycharm The virtual environment has also inherited ), therefore pycharm And local PythonIDLE Can run and execute code .

1. Demonstration of program packaging steps :

(1) Download and install first pyinstaller Install as follows :
pyinstaller install
(2)Windows Open the command line :
 Insert picture description here
(3) Enter where you are py File directory :
 Insert picture description here (4) After entering the catalog , Input :pyinstaller -F -w -i < The full name of the file containing the file type name >
 Insert picture description here

(5) stay py The following directory and other files will be generated under the same directory :
 Insert picture description here

(6) Get into dist file , We found that the package was finished exe Program , Because of our program exe It doesn't include the pictures used in it , The following errors will occur :
 Insert picture description here

(7) We have a solution , First, you should remove the image corresponding to the code and insert , Second, like the following , Copy or transfer the corresponding picture file to the same exe File directory , Open it again , The program runs successfully :
 Insert picture description here
 Insert picture description here

2. Summary of problems encountered in development and solutions :

Report errors :ModuleNotFoundError: No module named 'PIL’ resolvent
python Get current year and month
Python in tkinter.filedialog
Python-Tkinter Graphical interface design ( Detailed tutorial )
use tkinter.pack Design complex interface layout
python- be based on yagmail Library to develop automatic mail sending program
Last , Due to the lack of development experience and some defects in the program , I hope you understand !
Any questions about the content of the text , Welcome criticism and correction !!!

copyright notice
author[SteveDraw],Please bring the original link to reprint, thank you.

Random recommended