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服務(wù)器:') name_label = QLabel('地區(qū)/單位:') user_label = QLabel('郵箱賬號:') password_label = QLabel('郵箱密碼:') test_label = QLabel('測試收件人:') email_test_button = QPushButton('發(fā)送測試郵件') 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服務(wù)器,地區(qū)/單位,郵箱賬號,郵箱密碼\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,'成功','郵件已發(fā)送,請查收.') except Exception as error: print(repr(error)) QMessageBox.warning(self,'錯誤','郵件發(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,'提示','請先輸入郵件賬號.') 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)成都做網(wǎng)站、成都網(wǎng)站建設(shè)的開發(fā),更需要了解用戶,從用戶角度來建設(shè)網(wǎng)站,獲得較好的用戶體驗(yàn)。創(chuàng)新互聯(lián)建站多年互聯(lián)網(wǎng)經(jīng)驗(yàn),見的多,溝通容易、能幫助客戶提出的運(yùn)營建議。作為成都一家網(wǎng)絡(luò)公司,打造的就是網(wǎng)站建設(shè)產(chǎn)品直銷的概念。選擇創(chuàng)新互聯(lián)建站,不只是建站,我們把建站作為產(chǎn)品,不斷的更新、完善,讓每位來訪用戶感受到浩方產(chǎn)品的價值服務(wù)。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。