懒人必备Python代码之自动发送邮件
import wx
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.**init**(self, None, title="发送邮件", size=(400, 300))
panel = wx.Panel(self)
# 创建控件
self.title = wx.StaticText(panel, label="邮件标题:", pos=(20, 20))
self.title_input = wx.TextCtrl(panel, pos=(100, 20), size=(250, -1))
self.memo = wx.StaticText(panel, label="邮件正文:", pos=(20, 60))
self.memo_input = wx.TextCtrl(panel, style=wx.TE_MULTILINE, pos=(100, 60), size=(250, 100))
self.send_button = wx.Button(panel, label="发送", pos=(150, 200), size=(80, -1))
# 设置默认值
today = datetime.date.today().strftime("%Y-%m-%d")
self.title_input.SetValue(today + "会议纪要")
#显示对齐的多行文本
self.memo_input.SetValue("大家好:附件中是今天的会议纪要")
# 绑定事件
self.send_button.Bind(wx.EVT_BUTTON, self.on_send)
def on_send(self, event):
# 获取邮件信息
title = self.title_input.GetValue()
memo = self.memo_input.GetValue()
# 设置邮件内容
message = MIMEMultipart()
message['From'] = "skruggswzq@zusmail.com"
message['To'] = "lgalperinpyf@zusmail.com,mtarasciovgz@zusmail.com,sharvestkxw@zusmail.com,ebazinetbto@zusmail.com,spatryjpn@zusmail.com,dsittnerxko@zusmail.com,tbrassilqoy@zusmail.com,ihartgravesvmt@zusmail.com"
message['Cc'] = "greihlingmjo@zusmail.com"
message['Subject'] = title
message.attach(MIMEText(memo))
# 添加附件
filepath = "D:\\每周例会纪要.xlsx"
with open(filepath, "rb") as f:
attachment = MIMEApplication(f.read(), _subtype="xlsx")
attachment.add_header('Content-Disposition', 'attachment', filename=filepath.split("\\")[-1])
message.attach(attachment)
# 发送邮件
smtp = smtplib.SMTP()
smtp.connect("smtp.exmail.qq.com")
smtp.login("skruggswzq@zusmail.com", "password") # 请替换为您的邮箱密码
smtp.sendmail("skruggswzq@zusmail.com", message['To'].split(",") + message['Cc'].split(","), message.as_string())
smtp.quit()
# 提示发送成功
wx.MessageBox("邮件发送成功!", "提示")
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()