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'%(self.email_name.text().strip(),self.email_user.text().strip()))
msg['To'] = ','.join([self.email_test.text().strip()])
msg['Subject'] = Header('測(cè)試郵件', '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,'成功','郵件已發(fā)送,請(qǐng)查收.')
except Exception as error:
print(repr(error))
QMessageBox.warning(self,'錯(cuò)誤','郵件發(fā)送失敗.')
#郵件信息保存
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,'提示','請(qǐng)先輸入郵件賬號(hào).')
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,'提示','沒(méi)有可刪除的收件人.')
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)
當(dāng)前文章:pyqt5_站點(diǎn)管理_email_windows
當(dāng)前鏈接:
http://weahome.cn/article/jgecco.html