python使用tkinter写的邮件群发软件-python图形界面编程.docx
学习 python 不久,学软件编程,最有兴趣的就是图形界面编程了。用生涩的代码,写下了这个简陋的邮件群发程序。Tkinter 这个 GUI 模块,windows 下安装了 python 环境的默认已经安装了这个模块,不用另外下载安装见笑了#-*- coding:utf-8 -*- #file:smtp.pyimport Tkinter#GUI 图形界面模块import smtplib#邮件 smtplib 模块from email.mime.text import MIMEText# 邮 件 MIMEText from ConfigParser import ConfigParser# 配 置 文 件 模 块 import osimport tkFileDialog import reclass Window:def init (self, root): #Label 标签Host = Tkinter.Label(root, text = '服务器')Port = Tkinter.Label(root, text = '端口') User = Tkinter.Label(root, text = '用户名') Passwd = Tkinter.Label(root, text = '密码') Subject = Tkinter.Label(root, text = '主题')To = Tkinter.Label(root, text = 收' 件人')MailFile = Tkinter.Button(root, text = '浏览 ', command = self.MailFile)MailFile 函数#定义 Label 的位置Host.place(x = 5, y = 5)Port.place(x = 200, y = 5)User.place(x = 5, y = 30)Passwd.place(x = 200, y = 30)Subject.place(x = 5, y = 55)To.place(x = 5, y = 83) #定义浏览按钮的位置MailFile.place(x = 345, y = 80) #Entry 文本框self.entryHost = Tkinter.Entry(root) self.entryUser = Tkinter.Entry(root) self.entryPasswd = Tkinter.Entry(root, show = '*') self.entryTo = Tkinter.Entry(root, width = 40) self.entryPort = Tkinter.Entry(root)self.entrySub = Tkinter.Entry(root, width = 40) #读取配置文件config = ConfigParser() config.read('smtp.conf')# 调用Host = config.get('setting', 'Host') Port = config.get('setting', 'Port') User = config.get('setting', 'User')Passwd = config.get('setting', 'Passwd') #将配置文件里的值放入文本框self.entryHost.insert(Tkinter.END, Host) self.entryPort.insert(Tkinter.END, Port) self.entryUser.insert(Tkinter.END, User) self.entryPasswd.insert(Tkinter.END, Passwd) #文本框的位置self.entryHost.place(x = 50, y = 5)self.entryPort.place(x = 235, y = 5)self.entryUser.place(x = 50, y = 30)self.entryPasswd.place(x = 235, y = 30)self.entryTo.place(x = 50, y = 83)self.entrySub.place(x = 50, y = 55) #发送按钮,调用 MailSend 函数self.mailSend = Tkinter.Button(root, text = '开始发送 ', width= 20 , command =self.MailSend)#调用 SaveConfig 函数保存配置self.save = Tkinter.Button(root, text = '保存配置', command = self.SaveConfig) #调用 Help 函数打开帮助self.help = Tkinter.Button(root, text = '使用帮助', command = self.Help) self.mailSend.place(x = 430, y = 20)self.save.place(x = 430, y = 60)self.help.place(x = 520, y = 60)#多行文本框,用来输入邮件内容self.text = Tkinter.Text(root) self.text.place(y = 120)def MailFile(self):#该函数用来读取放有邮件地址的文本文件r = tkFileDialog.askopenfilename(title = '打开文件', filetypes = ('txt','*.txt') if r:self.entryTo.delete(0, Tkinter.END) self.entryTo.insert(Tkinter.END, r)#MailSend 用于发送邮件def MailSend(self):#使用 get()获取各文本框中的内容host = self.entryHost.get()port = self.entryPort.get() user = self.entryUser.get() pw = self.entryPasswd.get() fromaddr = usersubject = self.entrySub.get()text = self.text.get(1.0, Tkinter.END) #读取文件mailfile = open(self.entryTo.get(), 'r') mailaddr = mailfile.read()#使用正则表达式分割字符串,这里用逗号分割mail = re.split(',', mailaddr)#设置邮件内容为 utf-8 编码msg = MIMEText(text, _charset='utf-8') msg'From' = fromaddr#收件人msg'Subject' = subject #主题smtp = smtplib.SMTP() smtp.connect(host,port)#连接 smtp 服务器smtp.login(user, pw)#登陆 smtp 服务器#使用循环读取分割出来的邮件地址,同时实现邮件群发for toaddr in mail:msg'To' = toaddr#收件地址smtp.sendmail(fromaddr, toaddr, msg.as_string() #进行发送邮件smtp.close()#保存配置def SaveConfig(self):#获取文本框内容Host = self.entryHost.get() Port = self.entryPort.get() User = self.entryUser.get()Passwd = self.entryPasswd.get()#对需要保存的配置写入文件 smtp.conf 进行保存config = ConfigParser() config.add_section('setting')config.set('setting', 'Host', Host) config.set('setting', 'Port', Port) config.set('setting', 'User', User) config.set('setting', 'Passwd', Passwd) config.write(open('smtp.conf', 'w')#使用帮助def Help(self):help_str = '''帮助:1.服务器是 SMTP 服务器,QQ 的为 ,126 邮箱为 2.用户名必须带后缀,例:,3.收件人使用 txt 文件,邮件地址之间用","分隔'''self.text.insert(Tkinter.END, help_str) #检测配置文件是否存在,不存在则创建if(not os.path.isfile('smtp.conf'):config = ConfigParser() config.add_section('setting') config.set('setting', 'Host', '')config.set('setting', 'Port', '25')config.set('setting', 'User', '123456')config.set('setting', 'Passwd', '123456') config.write(open('smtp.conf', 'w')root = Tkinter.Tk()root.title("发你妹邮件群发")#标题root.geometry('650x500') window = Window(root) root.mainloop()#窗口大小