from PyQt5.QtWidgets import QWidget,QLineEdit,QLabel,QPushButton,QGridLayout,QHBoxLayout,QGroupBox,QComboBox,QMessageBox
import pickle,smtplib,os
from email import encoders
from email.header import Header
from email.utils import parseaddr,formataddr
from email.mime.text import MIMEText
#定义email窗口
class email_window(QWidget):
def __init__(self):
super().__init__()
self.email_stmp = QLineEdit()
self.email_name = QLineEdit()
self.email_user = QLineEdit()
self.email_password = QLineEdit()
self.email_password.setEchoMode(QLineEdit.Password)
self.email_test = QLineEdit()
self.dir_email = {}
self.list_users = []
self.email_users = QLineEdit()
self.email_suers_combobox = QComboBox()
self.initUI()
self.initEmail()
def initUI(self):
stmp_label = QLabel('SMTP服务器:')
name_label = QLabel('地区/单位:')
user_label = QLabel('邮箱账号:')
password_label = QLabel('邮箱密码:')
test_label = QLabel('测试收件人:')
email_test_button = QPushButton('发送测试邮件')
email_test_button.clicked.connect(self.email_test_fun)
email_input_button = QPushButton('保存邮箱配置')
email_input_button.clicked.connect(self.email_save)
email_info_group = QGroupBox('邮箱配置')
email_info_grid = QGridLayout()
email_info_grid.addWidget(stmp_label,0,0)
email_info_grid.addWidget(self.email_stmp,0,1)
email_info_grid.addWidget(name_label,1,0)
email_info_grid.addWidget(self.email_name,1,1)
email_info_grid.addWidget(user_label,2,0)
email_info_grid.addWidget(self.email_user,2,1)
email_info_grid.addWidget(password_label,3,0)
email_info_grid.addWidget(self.email_password,3,1)
email_info_grid.addWidget(test_label,4,0)
email_info_grid.addWidget(self.email_test,4,1)
email_info_grid.addWidget(email_test_button,5,0)
email_info_grid.addWidget(email_input_button,5,1)
email_info_group.setLayout(email_info_grid)
email_users_group = QGroupBox('收件人')
email_users_label = QLabel('邮箱:')
email_users_combobox_label = QLabel('邮箱列表:')
email_users_input_button = QPushButton('添加')
email_users_delete_button = QPushButton('删除')
email_users_input_button.clicked.connect(self.email_users_save)
email_users_delete_button.clicked.connect(self.email_users_delete)
email_users_grid = QGridLayout()
email_users_grid.addWidget(email_users_label,0,0)
email_users_grid.addWidget(self.email_users,0,1)
email_users_grid.addWidget(email_users_input_button,0,2)
email_users_grid.addWidget(email_users_combobox_label,1,0)
email_users_grid.addWidget(self.email_suers_combobox,1,1)
email_users_grid.addWidget(email_users_delete_button,1,2)
email_users_group.setLayout(email_users_grid)
hbox = QHBoxLayout()
hbox.addWidget(email_info_group)
hbox.addWidget(email_users_group)
self.setLayout(hbox)
#检查邮箱信息
def check_info(self):
if self.email_stmp.text().strip() == '' or self.email_user.text().strip() == '' or self.email_name.text().strip() == '' or self.email_password.text().strip() == '':
QMessageBox.warning(self,'提示','SMTP服务器,地区/单位,邮箱账号,邮箱密码\n不能为空.')
return 1
return 0
#格式化
def _format_addr(self,s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
#邮件测试按钮
def email_test_fun(self):
if self.check_info() == 1:
return
if self.email_test.text().strip() == '':
QMessageBox.warning(self,'提示','请先输入测试收件人.')
return
try:
msg = MIMEText('测试邮件.', 'plain', 'utf-8')
msg['From'] = self._format_addr('%s<%s>'%(self.email_name.text().strip(),self.email_user.text().strip()))
msg['To'] = ','.join([self.email_test.text().strip()])
msg['Subject'] = Header('测试邮件', 'utf-8').encode()
server = smtplib.SMTP(self.email_stmp.text().strip(), 25)
server.set_debuglevel(1)
server.login(self.email_user.text().strip(), self.email_password.text().strip())
server.sendmail(self.email_user.text().strip(), [self.email_test.text().strip()], msg.as_string())
server.quit()
QMessageBox.warning(self,'成功','邮件已发送,请查收.')
except Exception as error:
print(repr(error))
QMessageBox.warning(self,'错误','邮件发送失败.')
#邮件信息保存
def email_save(self):
if self.check_info() == 1:
return
self.dir_email['stmp'] = self.email_stmp.text().strip()
self.dir_email['name'] = self.email_name.text().strip()
self.dir_email['user'] = self.email_user.text().strip()
self.dir_email['password'] = self.email_password.text().strip()
with open('.//DATA//EMAILinfo.pik','wb') as email_file:
pickle.dump(self.dir_email,email_file)
QMessageBox.about(self,'提示','邮件信息保存成功.')
#初始化邮件信息
def initEmail(self):
try:
if os.path.exists('.//DATA//EMAILinfo.pik'):
with open('.//DATA//EMAILinfo.pik','rb') as e_file:
self.dir_email = pickle.load(e_file)
self.email_user.setText(self.dir_email['user'])
self.email_password.setText(self.dir_email['password'])
self.email_name.setText(self.dir_email['name'])
self.email_stmp.setText(self.dir_email['stmp'])
if os.path.exists('.//DATA//EMAILlist.pik'):
with open('.//DATA//EMAILlist.pik','rb') as file:
self.list_users = pickle.load(file)
x = 0
for line in self.list_users:
self.email_suers_combobox.insertItem(x, line)
x = x + 1
except Exception as error:
return
#保存用户
def email_users_save(self):
if self.email_users.text().strip() == '':
QMessageBox.warning(self,'提示','请先输入邮件账号.')
return
for line in self.list_users:
if line == self.email_users.text().strip():
QMessageBox.warning(self,'提示','用户已存在.')
return
self.email_suers_combobox.insertItem(len(self.list_users),self.email_users.text().strip())
self.list_users.append(self.email_users.text().strip())
with open('.//DATA//EMAILlist.pik','wb') as file:
pickle.dump(self.list_users,file)
self.email_users.setText('')
#删除用户
def email_users_delete(self):
if self.email_suers_combobox.currentText() == '':
QMessageBox.about(self,'提示','没有可删除的收件人.')
return
x = 0
for line in self.list_users:
if line == self.email_suers_combobox.currentText():
del self.list_users[x]
self.email_suers_combobox.removeItem(x)
break
x = x + 1
with open('.//DATA//EMAILlist.pik','wb') as file:
pickle.dump(self.list_users,file)
标题名称:pyqt5_站点管理_email_windows
转载源于:
http://cxhlcq.com/article/jgecco.html