演示示例使用QQ邮箱发送邮件,先获取自己的QQ邮箱的授权码。因为后面发送邮件时需要使用自己的授权码作为邮箱的密码登录邮箱最后达到发送邮件的目的。
将UI处理的相关的界面包导入进来
1
2
3
4
5
6
7
8
9
10
|
from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * # 应用操作相关的库 import sys # 邮件发送相关的库 import smtplib from email.mime.text import MIMEText |
为了不和邮件发送的UI界面主线程产生冲突,使用QThread子线程的方式制作邮件发送。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
class EmailWork(QThread): trigger = pyqtSignal( str ) finished = pyqtSignal( bool ) def __init__( self , parent = None ): super (EmailWork, self ).__init__(parent) self .parent = parent self .working = True def __del__( self ): self .working = False self .wait() def run( self ): email_subject_text = self .parent.email_subject_text.text().strip() recipient_text = self .parent.recipient_text.text().strip() current_text = self .parent.current_text.toPlainText().strip() print (email_subject_text) print (recipient_text) print (current_text) self .trigger.emit( "邮件信息读取完成!" ) # 发件人邮箱 send_email_name = '1342929047@qq.com' # 发件人授权码 passwd = 'fjyjqlzxprzihcii' self .trigger.emit(send_email_name) self .trigger.emit( "发件人信息初始化完成!" ) # 收件人邮箱 msg_to = recipient_text.split( ';' ) self .trigger.emit(recipient_text) self .trigger.emit( "收件人信息初始化完成!" ) print (msg_to) # 设置邮件 msg = MIMEText(current_text) msg[ 'subject' ] = email_subject_text # 设置发件人 msg[ 'From' ] = '一匹来自北方的狼' # 设置收件人 msg[ 'To' ] = ';' .join(msg_to) # 连接服务器 smtp = smtplib.SMTP_SSL( 'smtp.qq.com' , 465 ) self .trigger.emit( "服务器连接成功!" ) # 登录邮箱 smtp.login(send_email_name, passwd) self .trigger.emit( "邮箱登录成功!" ) # 发送邮件 smtp.sendmail(send_email_name, msg_to, msg.as_string()) self .trigger.emit( "邮件发送成功!" ) self .finished.emit( True ) |
下面是主页面的UI布局及信号量的代码块。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
class StmpEmail(QWidget): def __init__( self ): super (StmpEmail, self ).__init__() self .init_ui() def init_ui( self ): self .setWindowTitle( '批量邮件工具 公众号:[Python 集中营]' ) self .setWindowIcon(QIcon( '邮件.ico' )) self .setFixedSize( 500 , 400 ) hbox = QHBoxLayout() self .send_btn = QPushButton() self .send_btn.setText( '发送' ) self .send_btn.clicked.connect( self .send_btn_click) self .brower = QTextBrowser() self .brower.setFont(QFont( '宋体' , 8 )) self .brower.setReadOnly( True ) self .brower.setPlaceholderText( '执行进度显示区域...' ) self .brower.ensureCursorVisible() hbox.addWidget( self .send_btn) self .email_subject_text = QLineEdit() self .email_subject_text.setPlaceholderText( '请输入主题' ) self .recipient_text = QLineEdit() self .recipient_text.setPlaceholderText( '请输入收件人,示例:134047@qq.com;092837@163.com' ) self .current_text = QTextEdit() self .current_text.setPlaceholderText( '请输入邮件正文' ) self .thread_ = EmailWork( self ) self .thread_.trigger.connect( self .update_log) self .thread_.finished.connect( self .finished) vbox = QVBoxLayout() vbox.addWidget( self .email_subject_text) vbox.addWidget( self .recipient_text) vbox.addWidget( self .current_text) vbox.addWidget( self .brower) vbox.addLayout(hbox) self .setLayout(vbox) def update_log( self , text): ''' 槽函数:向文本浏览器中写入内容 :param text: :return: ''' cursor = self .brower.textCursor() cursor.movePosition(QTextCursor.End) self .brower.append(text) self .brower.setTextCursor(cursor) self .brower.ensureCursorVisible() 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() |
最后,通过main函数将页面布局加入主体循环即可。
1
2
3
4
5
|
if __name__ = = '__main__' : app = QApplication(sys.argv) main = StmpEmail() main.show() sys.exit(app.exec_()) |
以上就是邮件发送的主要代码块了,copy到自己的开发工具中运行即可(前提是你已经安装好了需要的python模块)。
最终效果图
到此这篇关于基于PyQt5制作一个群发邮件工具的文章就介绍到这了,更多相关PyQt5群发邮件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/lwsbc/p/16113978.html