UiAuto|Email插件

通过python进行邮件发送之前写过很多,但如果要查询目标邮件呢?
通过RPA模拟人工登录邮件,然后遍历出目标邮件?这种方法看似可以,但不高效。
这里通过python的imbox库实现邮件的遍历和查询,逻辑比较简单,插件源码如下:

package.json

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{
"id": "zjtEmail",
"name": "zjtmail",
"description": "公司邮件处理插件,邮件检索、附件下载",
"version": "1.0.0",
"author": "chendi",
"language": "python",
"license": "Official",
"main": "index.py",
"uiauto_config": {
"attribution_id": "software_automation",
"attribution_name": "软件自动化",
"operations": [
{
"category_id": "mail_operation",
"category_name": "邮件",
"operation_id": "zjtEmail",
"operation_name": "邮件附件下载",
"method": "connectEmail",
"type": "Convention",
"input": [
{
"name": "必填属性",
"id": "required_params",
"properties": [
{
"id": "mailUrl",
"name": "邮箱地址",
"type": "text",
"required": true,
"value": "mail.xxx.cn"
},
{
"id": "account",
"name": "邮箱账号",
"type": "text",
"required": true,
"value": ""
},
{
"id": "password",
"name": "邮箱密码",
"type": "password",
"required": true,
"value": ""
},
{
"id": "port",
"name": "IMAP端口",
"type": "text",
"required": true,
"value": "143"
},
{
"id": "queryDate",
"name": "检索日期(yyy-mm-dd)",
"type": "text",
"required": true,
"value": ""
},
{
"id": "queryType",
"name": "检索类型",
"type": "select",
"value": "subject",
"required": true,
"options": {
"multiple": false,
"choices": [
{
"value": "subject",
"label": "邮件主题"
},
{
"value": "sentFrom",
"label": "发件人"
},
{
"value": "body",
"label": "邮件内容"
}
]
}
},
{
"id": "keyword",
"name": "检索文字",
"type": "text",
"required": true,
"value": ""
},
{
"id": "filePath",
"name": "保存路径",
"type": "path",
"required": false,
"value": "",
"options":{
"select_type":"openDirectory"
}
}
]
},
{
"name": "可选属性",
"id": "unrequired_params",
"properties": [
{
"id": "fileName",
"name": "附件名称(带后缀)",
"type": "text",
"required": false,
"value": ""
}
]
}
],
"output": {
"is_allow_global_use": true,
"description": "邮件处理结果",
"value": ""
}
},
{
"category_id": "mail_operation",
"category_name": "邮件",
"operation_id": "zjtEmailSendFile",
"operation_name": "邮件发送",
"method": "sendMail",
"type": "Convention",
"input": [
{
"name": "必填属性",
"id": "required_params",
"properties": [
{
"id": "mailUrl",
"name": "邮箱地址",
"type": "text",
"required": true,
"value": "mail.xxx.cn"
},
{
"id": "account",
"name": "邮箱账号",
"type": "text",
"required": true,
"value": ""
},
{
"id": "password",
"name": "邮箱密码",
"type": "password",
"required": true,
"value": ""
},
{
"id": "to",
"name": "接收人(分号分割)",
"type": "text",
"required": true,
"value": ""
},
{
"id": "subject",
"name": "主题",
"type": "text",
"required": true,
"value": ""
},
{
"id": "contents",
"name": "内容",
"type": "tinymce",
"required": true,
"value": ""
}
]
},
{
"name": "可选属性",
"id": "unrequired_params",
"properties": [
{
"id": "filePath",
"name": "附件路径",
"type": "path",
"required": false,
"value": "",
"options":{
"select_type":"openDirectory"
}
},
{
"id": "fileName",
"name": "附件名称(带后缀)",
"type": "text",
"required": false,
"value": ""
}
]
}
],
"output": {
"is_allow_global_use": true,
"description": "邮件处理结果",
"value": ""
}
}
]
}
}

index.py

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- encoding:utf-8 -*-
'''
@description: 基于imbox,通过imap协议遍历邮件,下载所需邮件的附件。
@Time : 2023-06-02
@Author : CHENDI
@Version : 1.0.0
@Contact : henanchendi@163.com
'''
from imbox import Imbox
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.utils import formataddr,parseaddr

'''
基于imap,遍历邮件,下载所需邮件的附件。
by chendi 20230601
'''
def connectEmail(params):
# 获取参数
mailUrl = params.get('mailUrl')
account = params.get('account')
password = params.get('password')
port = params.get('port')
queryDate = params.get('queryDate')
queryType = params.get('queryType')
keyword = params.get('keyword')
filePath = params.get('filePath')
fileName = params.get('fileName')
queryDate = queryDate.replace('-0','-')
querydate = queryDate.split('-')

with Imbox(mailUrl,port=port,username=account,password=password,ssl=False) as imbox:
all_inbox_message = imbox.messages(date__on=datetime.date(int(querydate[0]),int(querydate[1]),int(querydate[2])))
for uid,message in all_inbox_message:
# 按照发件主题遍历下载
if queryType == 'subject' and keyword in message.subject:
for attachment in message.attachments:
if fileName == '':
file_name ='{}\\{}'.format(filePath,attachment['filename'])
else :
file_name= '{}\\{}'.format(filePath,fileName)
with open(file_name,'wb') as f:
f.write(attachment['content'].getvalue())
f.close()
# 按照邮件内容遍历下载
bodyall = ''
bodyall = bodyall.join(message.body['plain']).join(message.body['html'])
if queryType == 'body' and keyword in bodyall :
for attachment in message.attachments:
if fileName == '':
file_name ='{}\\{}'.format(filePath,attachment['filename'])
else :
file_name= '{}\\{}'.format(filePath,fileName)
with open(file_name,'wb') as f:
f.write(attachment['content'].getvalue())
f.close()
# 按照发件人遍历下载
if queryType == 'sentFrom' and keyword in message.sent_from[0]['email']:
for attachment in message.attachments:
if fileName == '':
file_name ='{}\\{}'.format(filePath,attachment['filename'])
else :
file_name= '{}\\{}'.format(filePath,fileName)
with open(file_name,'wb') as f:
f.write(attachment['content'].getvalue())
f.close()

'''
邮件发送(支持收件人为多人,支持附件发送)
smtplib不好用,要美化发件人和收件人信息才可以,否则邮件展示会有问题。
以后用yagmail第三方库。
'''
# 自定义发件人和收件人
def _format_addr(s):
addr = parseaddr(s)
return formataddr(addr)
# 发送邮件
def sendMail(params):
# 获取参数
mailUrl = params.get('mailUrl')
account = params.get('account')
password = params.get('password')
subject = params.get('subject')
contents = params.get('contents')
to = params.get('to')
filePath = params.get('filePath')
fileName = params.get('fileName')
file = filePath+'\\'+fileName
toaddr = to.split(',')

smtp = smtplib.SMTP()
smtp.connect(mailUrl, port=25)
smtp.login(user=account, password=password)
message = MIMEMultipart()
message['From'] = formataddr([account,account])
message['Subject'] = Header(subject, 'utf-8')
# 自定义收件人,否则对方邮箱会不显示收件人信息
for item in toaddr:
message['To'] = _format_addr(item)

message.attach(MIMEText(contents, 'html', 'utf-8'))
if len(filePath)>0 :
att = MIMEText(open(file,'rb').read(),'base64','utf-8')
att['Content-Type'] = 'application/octet-stream'
# att["Content-Disposition"] = 'attachment; filename={}'.format(fileName)
att.add_header('Content-Disposition','attachment',filename=Header(fileName,'utf-8').encode())
message.attach(att)
smtp.sendmail(from_addr=account, to_addrs=toaddr, msg=message.as_string())
smtp.quit()

if __name__ == '__main__':
pass



商业转载请联系作者获得授权,非商业转载请注明出处。

支付宝打赏 微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者

UiAuto|Email插件
http://hncd1024.github.io/2023/06/01/UiAuto_mail/
作者
CHEN DI
发布于
2023-06-01
许可协议