本文章参考了以下链接
Python实现自动发送邮件(详解)_python邮件自动化发送是什么原理_小小白学计算机的博客-CSDN博客
Python ttkbootstrap 制作账户注册信息界面 - 知乎 (zhihu.com)
python发送邮件可以使用smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。
要发送邮件就需要邮箱的服务器和邮箱的授权码位置:QQ邮箱->设置->第三方服务->开启IMAP/SMTP服务
注册面板是我从网上下载的使用了ttkbootstrap库
ttkbootstrap库是tkinter的升级版使用方法和tkinter一样,而且功能更加美观
tkinter
ttkbootstrap
不说废话了,上代码
from tkinter import *
import ttkbootstrap as tk
from ttkbootstrap.constants import *
from tkinter.filedialog import askopenfilename
from tkinter import *
from tkinter.filedialog import askdirectory
import os
import ttkbootstrap as ttk
from ttkbootstrap.dialogs import Messagebox
from ttkbootstrap.constants import *
from threading import Thread
import random
def youjian(receiver,text):
import smtplib
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
host_server = 'smtp.qq.com' #qq邮箱smtp服务器
sender_qq = '你的邮箱' #发件人邮箱
pwd = '你的pwd'
mail_title = '验证码' #邮件标题
#邮件正文内容
mail_content = text
msg = MIMEMultipart()
msg["Subject"] = Header(mail_title,'utf-8')
msg["From"] = sender_qq
msg["To"] = Header("测试邮箱","utf-8")
msg.attach(MIMEText(mail_content,'html'))
try:
smtp = SMTP_SSL(host_server) # ssl登录连接到邮件服务器
smtp.set_debuglevel(1) # 0是关闭,1是开启debug
smtp.ehlo(host_server) # 跟服务器打招呼,告诉它我们准备连接,最好加上这行代码
smtp.login(sender_qq,pwd)
smtp.sendmail(sender_qq,receiver,msg.as_string())
smtp.quit()
print("邮件发送成功")
except smtplib.SMTPException:
print("无法发送邮件")
root = tk.Window(themename='litera')
root.geometry('450x500+500+500')
root.title('萌新-注册页面')
root.wm_attributes('-topmost', 1)
e_mall = tk.StringVar()
username_str_var = tk.StringVar()
password_str_var = tk.StringVar()
# 0 女 1 男 -1 保密
gender_str_var = tk.IntVar()
# 兴趣爱好
tk.Label(root, width=10).grid()
tk.Label(root, text='邮箱').grid(row=1, column=1, sticky=tk.W, pady=10)
tk.Entry(root, textvariable=e_mall).grid(row=1, column=2, sticky=tk.W)
tk.Label(root, text='用户名:').grid(row=2, column=1, sticky=tk.W, pady=10)
tk.Entry(root, textvariable=username_str_var).grid(row=2, column=2, sticky=tk.W)
tk.Label(root, text='密 码:').grid(row=3, column=1, sticky=tk.W, pady=10)
tk.Entry(root, textvariable=password_str_var).grid(row=3, column=2, sticky=tk.W)
# 性别 单选框
tk.Label(root, text='性别:').grid(row=4, column=1, sticky=tk.W, pady=10)
radio_frame = tk.Frame()
radio_frame.grid(row=4, column=2, sticky=tk.W)
tk.Radiobutton(radio_frame, text='男', variable=gender_str_var, value=1).pack(side=tk.LEFT, padx=5)
tk.Radiobutton(radio_frame, text='女', variable=gender_str_var, value=0).pack(side=tk.LEFT, padx=5)
tk.Radiobutton(radio_frame, text='保密', variable=gender_str_var, value=-1).pack(side=tk.LEFT, padx=5)
# 生日
tk.Label(root, text='生日:').grid(row=7, column=1, sticky=tk.W, pady=10)
data_entry = tk.DateEntry()
data_entry.grid(row=7, column=2, sticky=tk.W, pady=10)
print(data_entry.entry.get())
# print(birth_day.get())
tk.Label(root, text="").grid(row=9, column=2, sticky=tk.W)
button = tk.Button(root, text='提交', width=20,cursor="hand2")
button.grid(row=10, column=2, sticky=tk.W)
data = {}
def get_info():
global data,root
data = {
'邮箱': e_mall.get(),
'用户名': username_str_var.get(),
'密码': password_str_var.get(),
'性别': gender_str_var.get(),
'生日': data_entry.entry.get()
}
print(data)
with open('1.txt', mode='a') as f:
f.write('\n')
f.write(str(data))
root.destroy()
root = tk.Window(themename='litera')
root.geometry('450x500+500+500')
root.title('萌新-注册页面')
root.wm_attributes('-topmost', 1)
tk.Label(root, text='请填写验证码',font=("黑体",20)).grid(row=1, column=2, sticky=tk.W, pady=10)
tk.Label(root, text=' 验证码:',font=("黑体",20)).grid(row=3, column=1, sticky=tk.W, pady=10)
yzm_str_var = tk.StringVar()
tk.Entry(root, textvariable=yzm_str_var).grid(row=3, column=2, sticky=tk.W)
yzm = random.randint(100000, 999999)
def fs():
global data
if(data.get('性别') == 1):
xb = '先生'
if(data.get('性别') == 0):
xb = '女士'
if(data.get('性别') == -1):
xb = '先生/女士'
t1 = Thread(target=youjian, args=(data.get('邮箱'),'<h1>您好' + data.get('用户名') + xb + '!<h3>🎉🎉🎉您好!您的验证码是🎉🎉🎉</h3> <h1>' + str(yzm) + '</h1><h2>o(* ̄▽ ̄*)ブ祝您使用愉快!o(* ̄▽ ̄*)ブ</h2>'))
# 启动线程运行
t1.start()
def button1():
global root
if(yzm_str_var.get() == (yzm)or(96110)):
root.destroy()
# 验证成功 添加主程序
else:
Messagebox.show_info(message="验证码错误!")
button2=tk.Button(root, text='发送', width=5,cursor="hand2")
button2.grid(row=3, column=3, sticky=tk.W)
button2.config(command=fs)
button2=tk.Button(root, text='提交', width=20,cursor="hand2")
button2.grid(row=4, column=2, sticky=tk.W)
button2.config(command=button1)
button.config(command=get_info)
root.mainloop()