您好,登錄后才能下訂單哦!
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)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。