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)成都做網站、成都網站建設的開發,更需要了解用戶,從用戶角度來建設網站,獲得較好的用戶體驗。創新互聯建站多年互聯網經驗,見的多,溝通容易、能幫助客戶提出的運營建議。作為成都一家網絡公司,打造的就是網站建設產品直銷的概念。選擇創新互聯建站,不只是建站,我們把建站作為產品,不斷的更新、完善,讓每位來訪用戶感受到浩方產品的價值服務。
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
文章題目:pyqt5_站點管理_email_windows-創新互聯
標題鏈接:http://vcdvsql.cn/article16/ieidg.html
成都網站建設公司_創新互聯,為您提供移動網站建設、云服務器、網站維護、標簽優化、全網營銷推廣、小程序開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯