Python sends an instance of the message and implements the UI with Tkinter

Source: Internet
Author: User

An accidental opportunity for me to come into contact with the Python language, personally feeling that Python is a powerful tool language. Make up your mind to study it well. Talk less, tell the next demo of the implementation process and source code. Thank you for correcting me!


The first step: the Main toolkit is: Smtplib. Here is the core code for the basic operation:

<span style= "FONT-SIZE:14PX;" >#-*-coding:utf-8-*-# Import the email modules we ' ll needimport smtplibfrom email.mime.text Import mimetextif __nam e__== ' __main__ ': #设置基本信息 mail_server = ' smtp.sina.com ' user_address = ' sender's email address ' User_pass = ' sender's email password ' #实例 Smtp_ssl _smtp = Smtplib.    Smtp_ssl () _smtp.connect (mail_server) #登录邮箱 _smtp.login (User_address,user_pass) #下面是创建邮件信息, mainly used in Mimetext. #设置收件人列表, the type can be ganso and list receivers = [' [email protected] ', ' [email protected] '] #设置邮件主题 subject = ' Test Mail ' #设置邮件内    Content = ' Hello,guys\nhow is you? '  #设置邮件实例 msg = mimetext (content,_subtyp= ' plain ', _charset= ' gb2312 ') #对应设置 msg[' from '] = user_address msg[' to '] = ";". Join (receivers) msg[' Subject '] = Subject #发送邮件 _smtp.sendmail (user_address,receivers,msg.as_string ()) print ' S Uccessfully sent email ' #测试结束 </span></span>


Step two: Implement the UI with Tkinter. This is relatively straightforward and gives the code directly:

<span style= "FONT-SIZE:14PX;" > #定义邮件管理窗口类class mailgui:def __init__ (self): Self.root = Tk () self.root.title (' mail-issuing ') WOR Karea = Frame (self.root,width=500,height=500) workarea.pack () #tkMessageBox. Askquestion (title= ' user login ', message = ' Open Login window ') #初始化窗口 labelframe = Labelframe (workarea,padx=5,pady=10) #text can empty Labelframe.pack (fil L= ' both ') #发送按钮 Btn_frame = FRAME (labelframe,width=400,height=50,bg= ' #CCCCCC ') btn_frame.pack (fill= ' Y ') Sand_btn=button (btn_frame,text= ' send ', padx=3,pady=2, state= ' active ', Command=self.runsendmai        L) Sand_btn.pack () #定义变量, save customer input results Self.v_subject=stringvar () Self.v_receiver=stringvar () Self.v_content=stringvar () #接收人 receiver_frame = FRAME (labelframe,width=400,height=50) rece Iver_frame.pack (fill= ' y ') Label (receiver_frame,width=20,pady=5,justify= ' left ', text=' Recipient '). Pack (side= ' left ') Entry (receiver_frame,width=50,textvariable=self.v_receiver). Pack (side= ' to ") #主题 Subject_frame = FRAME (labelframe,width=400,height=50) subject_frame.pack (fill= ' y ') Label (subject_frame , width=20,pady=5,justify= ' left ', text= ' theme '). Pack (side= ' left ') Entry (subject_frame,width=50         , Textvariable=self.v_subject). Pack (side= ' right ') #邮件内容 Content_frame = FRAME (labelframe,width=400,height=50) Content_frame.pack (fill= ' y ') Label (content_frame,width=20,pady=5,justify= ' left ', Tex t= ' content '). Pack (side= ' left ') self. T_content = Text (content_frame,width=38,height=4) self. T_content.pack (side= ' right ') Self.root.mainloop () </span>

Note: To get the dynamic input results, use the Stringvar ()

     <span style= "FONT-SIZE:14PX;" >           #定义变量, save the customer input result        Self.v_receiver=stringvar ()        #textvariable bind the predefined variable       Entry (Receiver_frame, width=50,textvariable=self.v_receiver). Pack (side= ' left ') </span>

The end of the step,


The operating interface is as follows:


The complete code is attached below:

<span style= "FONT-SIZE:14PX;" >#-*-coding:utf-8-*-# Import the email modules we ' ll needimport smtplibfrom email.mime.text Import mimetextfrom Tki        Nter Import *import tkmessageboximport sys# define the Mail Management window classes class Mailgui:def __init__ (self): Self.root = Tk ()        Self.root.title (' mailer ') Workarea = Frame (self.root,width=500,height=500) workarea.pack () #初始化窗口        Labelframe = Labelframe (workarea,padx=5,pady=10) #text can empty labelframe.pack (fill= ' both ') #发送按钮 Btn_frame = FRAME (labelframe,width=400,height=50,bg= ' #CCCCCC ') btn_frame.pack (fill= ' y ') Sand_btn=button (        btn_frame,text= ' send ', padx=3,pady=2, state= ' active ', Command=self.runsendmail) Sand_btn.pack () #定义变量, save customer input results Self.v_subject=stringvar () Self.v_receiver=stringvar () Self.v_content=stringvar () #接收人 Receiver_frame = FRAME (labelframe,width=400,height=50) Receiver_frame.pack (fiLl= ' y ') Label (receiver_frame,width=20,pady=5,justify= ' left ', text= ' receiver '). Pack (side= ' ") Entry (Receiver_frame,width=50,textvariable=self.v_receiver). Pack (side= ' left ') #主题 subject_frame = Fram E (labelframe,width=400,height=50) subject_frame.pack (fill= ' y ') Label (subject_frame,width=20,pady=5,justify= ' Left ', text= ' theme '). Pack (side= ' left ') Entry (subject_frame,width=50,textvariable=self.v_subje CT). Pack (side= ' right ') #邮件内容 Content_frame = FRAME (labelframe,width=400,height=50) content_frame.pack        (fill= ' y ') Label (content_frame,width=20,pady=5,justify= ' left ', text= ' content '). Pack (side= ' left ') self. T_content = Text (content_frame,width=38,height=4) self. T_content.pack (side= ' right ') Self.root.mainloop () def runsendmail (self): #tkMessageBox. Askquestio        N (title= ' send mail ', message= ' send? ')   Receiver=self.v_receiver.get ()     Sub=self.v_subject.get () to_list=receiver.split (', ') content=str (self). T_content.get (' 0.0 ', END)). Strip () self.initsmtp (' xxxxx ', ' @sina. com ', ' sender password ') Self.sendmail (sub,to_list,conte NT) Tkmessagebox.showinfo (title= ' System prompt ', message= ' send Success ') def INITSMTP (Self,mail_user,mail_server_name,mail_passwor D): Self.mail_user = Mail_user + mail_server_name Self.mail_password = Mail_password self.mail_server _name = Mail_server_name Self._smtp = smtplib.    Smtp_ssl () Self._smtp.connect (Self.findmailserver ()) Self._smtp.login (Self.mail_user,self.mail_password) def sendMail (self,sub,to_list,mail_body): msg = Mimetext (mail_body,_subtype= ' plain ', _charset= ' utf-8 ') msg["A Ccept-language "]=" ZH-CN "msg[" Accept-charset "]=" Iso-8859-1,utf-8 "msg[' Subject '] = Sub msg[' from '] = Self.mail_user msg[' to '] = ";".      Join (to_list) Self._smtp.sendmail (self.mail_user,to_list,msg.as_string ())  Self._smtp.quit () def findmailserver (self): server_name = str (self.mail_server_name). Strip () Server_dic t = {' @sina. com ': ' smtp.sina.com ', ' @126.com ': ' smtp.126.com ', ' @163.com ': ' SMTP.            163.com ', ' @qq. com ': ' smtp.qq.com '} return Server_dict[server_name] Try:reload (SYS)     Sys.setdefaultencoding (' Utf-8 ') Mailgui () except Exception,e:print str (e) </span>


Thank you for reading!


Python sends an instance of the message and implements the UI with Tkinter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.