先上图
wxPython代码如下:
# -*- coding: utf-8 -*-
import wx
import os
import SendMailTools
class app_001(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,u"自动发送邮件工具--V1.0",size=(800,600))
panel = wx.Panel(self,-1)
openbutton = wx.Button(panel, label = u"打开文件") #创建主件1
self.Bind(wx.EVT_BUTTON,self.OnOpen,openbutton)
savebutton = wx.Button(panel, label = u"发送")
self.Bind(wx.EVT_BUTTON,self.OnSend,savebutton)
stopbutton = wx.Button(panel, label = u"暂停")
self.filename = wx.TextCtrl(panel) #创建主件3
self.contents = wx.TextCtrl(panel, style = wx.TE_MULTILINE | wx.HSCROLL) #创建主件4
self.qq = wx.TextCtrl(panel, style = wx.TE_MULTILINE | wx.HSCROLL)
hbox = wx.BoxSizer() #创建尺寸器1,作用是管理添加在其中的组件布局的位置
#下面是把已经创建好的主件以一定的方式添加到布局管理器1中
hbox.Add(self.filename, proportion = 1, flag = wx.EXPAND)
hbox.Add(openbutton, proportion = 0, flag = wx.LEFT, border = 5)
hbox.Add(savebutton, proportion = 0, flag = wx.LEFT, border = 5)
hbox.Add(stopbutton, proportion = 0, flag = wx.LEFT, border = 5)
mailList = ['qq','163','126']
choiceText = wx.StaticText(panel,-1,u"请选择邮箱类型:")
self.mailChoice = wx.Choice(panel, -1, (0, 18), choices = mailList)
self.username = wx.StaticText(panel,-1,u"发件人帐号:")
self.username_text =wx.TextCtrl(panel)
self.password = wx.StaticText(panel,-1,u"发件人密码:")
self.password_text =wx.TextCtrl(panel,-1,size=(100,-1),style=wx.TE_PASSWORD)
dbox = wx.BoxSizer()
dbox.Add(choiceText, proportion = 0, flag = wx.ALL,border=5)
dbox.Add(self.mailChoice, proportion = 0, flag = wx.ALL,border=5)
dbox.Add(self.username, proportion = 0, flag = wx.ALL,border=5)
dbox.Add(self.username_text, proportion = 0, flag = wx.ALL,border=5)
dbox.Add(self.password, proportion = 0, flag = wx.ALL,border=5)
dbox.Add(self.password_text, proportion = 0, flag = wx.ALL,border=5)
self.subjectLabel = wx.StaticText(panel,-1,u"邮件标题:")
self.subject = wx.TextCtrl(panel)
ebox = wx.BoxSizer()
ebox.Add(self.subjectLabel,proportion = 0, flag = wx.ALL,border=5)
ebox.Add(self.subject,proportion = 1, flag = wx.EXPAND)
cbox = wx.BoxSizer()
cbox.Add(self.qq, proportion = 0, flag = wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT
, border = 5)
cbox.Add(self.contents, proportion = 1, flag = wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT
, border = 5)
bbox = wx.BoxSizer(wx.VERTICAL) #创建尺寸器2(布局管理器)
#下面是把已经创建好的主件和尺寸器1以一定的方式添加到尺寸器2中
bbox.Add(hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
bbox.Add(dbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
bbox.Add(ebox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
bbox.Add(cbox, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
panel.SetSizer(bbox) #把尺寸器,放置在画布中(自动设置大小)
def OnOpen(self, event):
'''''
打开开文件对话框
'''
file_wildcard = "Paint files(*.txt)|*.txt|All files(*.*)|*.*"
dlg = wx.FileDialog(self, "Open paint file...",
os.getcwd(),
style = wx.OPEN,
wildcard = file_wildcard)
if dlg.ShowModal() == wx.ID_OK:
file_path = dlg.GetPath()
self.filename.SetValue(file_path)
self.qq.SetValue(self.readFile(file_path))
dlg.Destroy()
def readFile(self,file_path):
if os.path.exists(file_path):
file_content = ''
for line in open(file_path):
file_content +=line
return file_content
def OnSend(self,event):
qq_file = self.filename.GetValue()
smtp = self.mailChoice.GetStringSelection()
username = self.username_text.GetValue()
password = self.password_text.GetValue()
subject = self.subject.GetValue()
content = self.contents.GetValue()
if qq_file != '':
headMsg = {}
contentMsg = {}
if os.path.exists(qq_file):
headMsg['smtp'] = smtp
headMsg['username'] = username
headMsg['password'] = password
contentMsg['subject'] = subject
contentMsg['content'] = content
for line in open(qq_file):
#print line
line = line.strip()
headMsg['receive_user'] = line+"@qq.com"
res = SendMailTools.SendGroupMail(headMsg,contentMsg)
if res:
print headMsg['receive_user']+" send OK"
else:
print headMsg['receive_user']+" send lose"
if __name__=="__main__":
app = wx.App(redirect=True)
frame = app_001()
frame.Show(True)
app.MainLoop()
群发邮件的代码 SendMailTools.py如下:
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
def SendGroupMail(*args,**kwargs):
if len(args) == 2:
ret = True
headMsg,contentMsg = args
mail_account = headMsg['username']
mail_password = headMsg['password']
mail_subject = contentMsg['subject']
receive_user = headMsg['receive_user']
try:
msg = MIMEText(contentMsg['content'],'plain','utf-8')
msg['From']=formataddr([mail_account,mail_account])
msg['To']=formataddr([receive_user,receive_user])
msg['Subject']=mail_subject
server=smtplib.SMTP("smtp.163.com",25)
server.login(mail_account,mail_password)
server.sendmail(mail_account,[receive_user,],msg.as_string())
server.quit()
except Exception:
ret = False
return ret