之前制作过一个邮件发送的代码,但是,闲着也是闲着,就用 tkinter 重新包装了一下.哎!就是玩儿!
import smtplib #smtp服务器
from email.mime.text import MIMEText #邮件文本
from tkinter import *
window_w = '444' # 窗口宽
window_h = '350' # 窗口高
window_s = window_w + 'x' + window_h
window_p = "+500+150" # 窗口出现的位置
win_l_side=11
window1 = Tk() # 创建一个窗口
window1.resizable(width=False, height=False) # 设定窗口是否可以缩放,以及缩放范围
window1.title('邮件') # 标题
window1.geometry(window_s + window_p) # 窗口的大小 以及位置 放置窗口
# , command=play
font1 = ('微软雅黑', 12)
def input():
"""输入框的 放置 及 设置"""
global receive_people,message,subject
# 邮件标题
subject_L = Label(window1, text='标题', font=font1, bg='#ebeee8').place(x=win_l_side, y=10) # 放置标签
subject = Entry(window1, show=None, font=font1) # show 显示效果 正常显示
subject.place(x=win_l_side + 60, y=10)
# 收件人
receive_p = Label(window1, text='收件人', font=font1, bg='#ebeee8').place(x=win_l_side, y=40) # 放置标签
receive_people = Entry(window1, show=None, font=font1) # show 显示效果 正常显示
receive_people.place(x=win_l_side + 60, y=40)
# 邮件正文
message = Text(window1, width=46, height=10, font=font1) # 宽是高的两倍时,正方形
message.place(x=win_l_side, y=70)
def send_message():
""" 邮件内容的发送"""
recver=receive_people.get() # 获取你的输入文本 收件人输入窗口 那里的值
content=message.get("1.0", "end") #同理 得到你想发送的消息的文本 你的发送内容
message_get = MIMEText(content) # 对文本发送的进行设置
message_get['Subject'] = subject.get() # 邮件标题 这里一样,我懒得想变量名了,哈哈
message_get['To'] = recver #收件人
message_get['From']= sender #接收你的邮箱账号
smtp = smtplib.SMTP_SSL("smtp.163.com", 994) # 实例化smtp服务器
smtp.login(sender, password) # 发件人登录
smtp.sendmail(sender, [recver], message_get.as_string()) # as_string 对 message 的消息进行了封装
news.set('邮件发送成功')
smtp.close()
def buttons():
"""按钮的放置"""
# 只有一个按钮 ,就是发送,看图
play_b = Button(window1, text='发送',
font=font1,
width=8,
command=send_message,
bg='#bed2bb').place(x=330, y=300)
def show():
"""消息显示版面,我计划的是这里用来设计整个外窗口的外在形象,结果。。。还是懒得弄,噫~~~"""
# 消息提示用全局变量,因为别的函数要调用,话说还有别的方法吗 实在不想用全局变量
global news
news = StringVar()
about = Label(window1, textvariable=news,
justify='left', anchor='nw',
width=20, height=3,
font=font1,
fg='#d24735',
bg='#ebeee8').place(x=13, y=300)#,padx=20, pady=10
news.set("状态显示")
window1.config(background='#ebeee8') #窗口背景色 颜色的十六进制代码 #*****
#调用上面的函数
buttons()
input()
if __name__ == "__main__":
sender = "###########" # 你的邮箱账号
password = "**************" # 邮箱密码,这里是你的邮箱授权码哦 不是登陆密码
show()
window1.mainloop() #使窗口一直存在
其实还可以实现邮件群发,,对输入的文本进行字符串分割,遍历列表,替换收件人,然后依次发送,