Demonstrate the use of QQ mailbox to send emails, first get their QQ mailbox authorization code. Because when you send emails later, you need to use your own authorization code as the password to log in the mailbox to send emails.
Import the bounding box related to UI processing.
from import * from import * from import * # Libraries related to application operations import sys # Mail delivery related libraries import smtplib from import MIMEText
In order not to conflict with the main thread of the UI interface where the email is sent, the email is created using a QThread sub-thread.
class EmailWork(QThread): trigger = pyqtSignal(str) finished = pyqtSignal(bool) def __init__(self, parent=None): super(EmailWork, self).__init__(parent) = parent = True def __del__(self): = False () def run(self): email_subject_text = .email_subject_text.text().strip() recipient_text = .recipient_text.text().strip() current_text = .current_text.toPlainText().strip() print(email_subject_text) print(recipient_text) print(current_text) ("Mail message reading complete!") # Sender's e-mail send_email_name = '1342929047@' # Sender authorization code passwd = 'fjyjqlzxprzihcii' (send_email_name) ("Sender information initialization complete!") # Recipient Email msg_to = recipient_text.split(';') (recipient_text) ("Recipient information initialization complete!") print(msg_to) # Setting up mail msg = MIMEText(current_text) msg['subject'] = email_subject_text # Set sender msg['From'] = 'A wolf from the north.' # Setting up recipients msg['To'] = ';'.join(msg_to) # Connect to the server smtp = smtplib.SMTP_SSL('', 465) ("Server connection successful!") # Login Mailbox (send_email_name, passwd) ("E-mail login successful!") # Send mail (send_email_name, msg_to, msg.as_string()) ("Mail sent successfully!") (True)
Here is the UI layout of the main page and the code block for the semaphore.
class StmpEmail(QWidget): def __init__(self): super(StmpEmail, self).__init__() self.init_ui() def init_ui(self): ('Bulk mail tool public number: [Python centralized camp]') (QIcon('mail.ico')) (500, 400) hbox = QHBoxLayout() self.send_btn = QPushButton() self.send_btn.setText('Send') self.send_btn.(self.send_btn_click) = QTextBrowser() (QFont('Song Style', 8)) (True) ('Execution progress display area...') () (self.send_btn) self.email_subject_text = QLineEdit() self.email_subject_text.setPlaceholderText('Please enter a subject') self.recipient_text = QLineEdit() self.recipient_text.setPlaceholderText('Please enter the recipient, example: 134047@;092837@') self.current_text = QTextEdit() self.current_text.setPlaceholderText('Please enter the body of the message') self.thread_ = EmailWork(self) self.thread_.(self.update_log) self.thread_.() vbox = QVBoxLayout() (self.email_subject_text) (self.recipient_text) (self.current_text) () (hbox) (vbox) def update_log(self, text): ''' Slot function: write content to text browser :param text. :return. ''' cursor = () () (text) (cursor) () def finished(self, finished): if finished is True: self.send_btn.setEnabled(True) def send_btn_click(self): self.send_btn.setEnabled(False) self.thread_.start()
Finally, just add the page layout to the body loop via the main function.
if __name__ == '__main__': app = QApplication() main = StmpEmail() () (app.exec_())
The above is the main block of code for email sending, copy it to your own development tools and run it (provided that you have installed the required python modules).
Final Result
to this article on the production of a mass e-mail tool based on PyQt5 article is introduced to this, more related PyQt5 mass e-mail content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!