学习python不久,学软件编程,最有兴趣的就是图形界面编程了。
用生涩的代码,写下了这个简陋的邮件发程序。
Tkinter这个GUI模块,windows下安装了python环境的默认已经安装了这个模块,不用另外下载安装
见笑了
#-*- coding:utf-8 -*-
#file:smtp.py
import Tkinter #GUI图形界面模块
import smtplib #邮件smtplib模块
from import MIMEText #邮件MIMEText
from ConfigParser import ConfigParser #配置文件模块
import os
import tkFileDialog
import re
class 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文本框
Host = Tkinter.Entry(root)
User = Tkinter.Entry(root)
Passwd = Tkinter.Entry(root, show = '*')
To = Tkinter.Entry(root, width = 40)
Port = Tkinter.Entry(root)
Sub = Tkinter.Entry(root, width = 40)
#读取配置文件
config = ConfigParser()
ad('f')
Host = ('setting', 'Host')
Port = ('setting', 'Port')
User = ('setting', 'User')
Passwd = ('setting', 'Passwd')
#将配置文件里的值放入文本框
Host.insert(Tkinter.END, Host)
Port.insert(Tkinter.END, Port)
User.insert(Tkinter.END, User)
Passwd.insert(Tkinter.END, Passwd)
#文本框的位置
Host.place(x = 50, y = 5)
Port.place(x = 235, y = 5)
User.place(x = 50, y = 30)
Passwd.place(x = 235, y = 30)
To.place(x = 50, y = 83)
Sub.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)
#多行文本框,用来输入邮件内容
= Tkinter.Text(root)
place(y = 120)
def MailFile(self):
#该函数用来读取放有邮件地址的文本文件
r = tkFileDialog.askopenfilename(title = '打开文件', filetypes = [('txt','*.txt')])
if r:
To.delete(0, Tkinter.END)
To.insert(Tkinter.END, r)
#MailSend用于发送邮件
def MailSend(self):
#使用get()获取各文本框中的内容
host = ()
port = ()
user = ()
pw = ()
fromaddr = user
subject = ()
text = (1.0, Tkinter.END)
#读取文件
mailfile = To.get(), 'r')
mailaddr = ad()
#使用正则表达式分割字符串,这里用逗号分割
mail = re.split(',', mailaddr)
#设置邮件内容为utf-8编码
msg = MIMEText(text, _charset='utf-8')
msg['From'] = fromaddr #收件人
msg['Subject'] = subject #主题
smtp = smtplib.SMTP()
t(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 = ()
Port = ()
User = ()
Passwd = ()
#对需要保存的配置写入文件f进行保存
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('f', 'w'))
#使用帮助
def Help(self):
help_str = '''
帮助:
1.服务器是SMTP服务器,QQ的为smtp.qq,126邮箱为smtp.126
2.用户名必须带后缀,例:*************,**************
3.收件人使用txt文件,邮件地址之间用","分隔
'''
insert(Tkinter.END, help_str)
#检测配置文件是否存在,不存在则创建
if(not os.path.isfile('f')):
config = ConfigParser()
config.add_section('setting')
config.set('setting', 'Host', 'smtp.qq')
config.set('setting', 'Port', '25')
config.set('setting', 'User', '123456')
config.set('setting', 'Passwd', '123456')
config.write(open('f', 'w'))
root = Tkinter.Tk()
发布评论