@zhin.js/adapter-email 0.1.13 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +68 -311
  3. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @zhin.js/adapter-email
2
2
 
3
+ ## 0.1.14
4
+
5
+ ### Patch Changes
6
+
7
+ - 106d357: fix: ai
8
+ - Updated dependencies [106d357]
9
+ - zhin.js@1.0.26
10
+
3
11
  ## 0.1.13
4
12
 
5
13
  ### Patch Changes
package/README.md CHANGED
@@ -1,6 +1,14 @@
1
- # Email 适配器
1
+ # @zhin.js/adapter-email
2
2
 
3
- 基于 SMTP/IMAP 协议的邮件适配器,支持邮件的收发、附件处理等功能。
3
+ Zhin.js 邮件适配器,通过 SMTP 发送和 IMAP 接收邮件,将邮箱作为聊天平台接入。
4
+
5
+ ## 功能特性
6
+
7
+ - SMTP 邮件发送(基于 nodemailer)
8
+ - IMAP 邮件接收(基于 imap + mailparser)
9
+ - 支持附件处理
10
+ - 定时轮询新邮件
11
+ - TLS/SSL 加密连接
4
12
 
5
13
  ## 安装
6
14
 
@@ -8,19 +16,37 @@
8
16
  pnpm add @zhin.js/adapter-email
9
17
  ```
10
18
 
11
- ## 依赖库
12
-
13
- 本适配器使用以下库:
14
- - `nodemailer` - SMTP 邮件发送
15
- - `imap` - IMAP 邮件接收
16
- - `mailparser` - 邮件内容解析
17
-
18
19
  ## 配置
19
20
 
20
- ### 基础配置
21
+ ```yaml
22
+ # zhin.config.yml
23
+ bots:
24
+ - context: email
25
+ name: my-email-bot
26
+ smtp:
27
+ host: smtp.example.com
28
+ port: 465
29
+ secure: true
30
+ auth:
31
+ user: bot@example.com
32
+ pass: ${EMAIL_PASSWORD}
33
+ imap:
34
+ host: imap.example.com
35
+ port: 993
36
+ tls: true
37
+ user: bot@example.com
38
+ password: ${EMAIL_PASSWORD}
39
+ # checkInterval: 30000 # 轮询间隔(毫秒),默认 30 秒
40
+ # mailbox: INBOX # 监听的邮箱文件夹
41
+ # markSeen: true # 已读标记
42
+
43
+ plugins:
44
+ - adapter-email
45
+ ```
46
+
47
+ ### TypeScript 配置
21
48
 
22
49
  ```typescript
23
- // zhin.config.ts
24
50
  import { defineConfig } from 'zhin.js'
25
51
 
26
52
  export default defineConfig({
@@ -29,333 +55,64 @@ export default defineConfig({
29
55
  context: 'email',
30
56
  name: 'my-email-bot',
31
57
  smtp: {
32
- host: 'smtp.gmail.com',
33
- port: 587,
34
- secure: false, // true for 465, false for other ports
58
+ host: 'smtp.example.com',
59
+ port: 465,
60
+ secure: true,
35
61
  auth: {
36
- user: process.env.EMAIL_USER,
37
- pass: process.env.EMAIL_PASSWORD
38
- }
62
+ user: 'bot@example.com',
63
+ pass: process.env.EMAIL_PASSWORD!,
64
+ },
39
65
  },
40
66
  imap: {
41
- host: 'imap.gmail.com',
67
+ host: 'imap.example.com',
42
68
  port: 993,
43
69
  tls: true,
44
- user: process.env.EMAIL_USER,
45
- password: process.env.EMAIL_PASSWORD,
46
- checkInterval: 60000, // 检查新邮件间隔(毫秒)
47
- mailbox: 'INBOX', // 监听的邮箱文件夹
48
- markSeen: true // 是否标记已读
49
- }
70
+ user: 'bot@example.com',
71
+ password: process.env.EMAIL_PASSWORD!,
72
+ },
50
73
  }
51
74
  ],
52
75
  plugins: ['adapter-email']
53
76
  })
54
77
  ```
55
78
 
56
- ### 完整配置选项
57
-
58
- ```typescript
59
- const fullEmailConfig: EmailBotConfig = {
60
- context: 'email',
61
- name: 'advanced-email-bot',
62
- smtp: {
63
- host: 'smtp.gmail.com',
64
- port: 587,
65
- secure: false,
66
- auth: {
67
- user: 'your-email@gmail.com',
68
- pass: 'your-app-password'
69
- }
70
- },
71
- imap: {
72
- host: 'imap.gmail.com',
73
- port: 993,
74
- tls: true,
75
- user: 'your-email@gmail.com',
76
- password: 'your-app-password',
77
- checkInterval: 30000, // 30秒检查一次
78
- mailbox: 'INBOX',
79
- markSeen: true
80
- },
81
- attachments: {
82
- enabled: true,
83
- downloadPath: './downloads/email', // 附件下载路径
84
- maxFileSize: 10 * 1024 * 1024, // 10MB 最大文件大小
85
- allowedTypes: ['image/*', 'text/*', 'application/pdf'] // 允许的文件类型
86
- }
87
- }
88
- ```
89
-
90
79
  ## 使用示例
91
80
 
92
- ### 基础使用
81
+ ### 注册命令
93
82
 
94
83
  ```typescript
95
- import { createApp } from 'zhin.js'
96
- import EmailAdapter from '@zhin.js/adapter-email'
84
+ import { usePlugin, MessageCommand } from 'zhin.js'
97
85
 
98
- const app = createApp({
99
- adapters: {
100
- email: {
101
- context: 'email',
102
- name: 'email-bot',
103
- smtp: {
104
- host: 'smtp.gmail.com',
105
- port: 587,
106
- secure: false,
107
- auth: {
108
- user: 'your-email@gmail.com',
109
- pass: 'your-app-password'
110
- }
111
- },
112
- imap: {
113
- host: 'imap.gmail.com',
114
- port: 993,
115
- tls: true,
116
- user: 'your-email@gmail.com',
117
- password: 'your-app-password'
118
- }
119
- }
120
- }
121
- })
122
-
123
- // 处理接收到的邮件
124
- app.on('message.receive', (message) => {
125
- if (message.$adapter === 'email') {
126
- console.log('收到邮件:', message.$content)
127
-
128
- // 回复邮件
129
- message.$reply('感谢您的邮件!')
130
- }
131
- })
86
+ const { addCommand } = usePlugin()
132
87
 
133
- app.start()
88
+ addCommand(
89
+ new MessageCommand('status')
90
+ .desc('查询状态')
91
+ .action(() => '机器人运行中')
92
+ )
134
93
  ```
135
94
 
136
- ### 发送邮件
95
+ ### 消息处理
137
96
 
138
97
  ```typescript
139
- // 发送简单文本邮件
140
- await app.sendMessage({
141
- adapter: 'email',
142
- channel_id: 'recipient@example.com',
143
- content: '这是一条测试邮件'
144
- })
98
+ import { usePlugin } from 'zhin.js'
145
99
 
146
- // 发送带附件的邮件
147
- await app.sendMessage({
148
- adapter: 'email',
149
- channel_id: 'recipient@example.com',
150
- content: [
151
- { type: 'text', data: { content: '请查收附件' } },
152
- { type: 'file', data: { url: '/path/to/file.pdf', filename: 'report.pdf' } }
153
- ]
154
- })
155
- ```
156
-
157
- ### 处理特定邮件
100
+ const { addMiddleware } = usePlugin()
158
101
 
159
- ```typescript
160
- app.addCommand('auto-reply', {
161
- match: (message) => {
162
- return message.$adapter === 'email' &&
163
- message.$content.some(seg => seg.type === 'text' &&
164
- seg.data.content.includes('自动回复'))
165
- },
166
- action: async (message) => {
167
- await message.$reply('这是自动回复邮件。')
102
+ addMiddleware(async (message, next) => {
103
+ if (message.$adapter === 'email') {
104
+ console.log('收到邮件:', message.$sender.name, message.$content)
168
105
  }
106
+ await next()
169
107
  })
170
108
  ```
171
109
 
172
- ## 邮件服务商配置
173
-
174
- ### Gmail
175
-
176
- 1. 开启两步验证
177
- 2. 生成应用专用密码
178
- 3. 使用以下配置:
179
-
180
- ```typescript
181
- {
182
- smtp: {
183
- host: 'smtp.gmail.com',
184
- port: 587,
185
- secure: false
186
- },
187
- imap: {
188
- host: 'imap.gmail.com',
189
- port: 993,
190
- tls: true
191
- }
192
- }
193
- ```
194
-
195
- ### Outlook/Hotmail
196
-
197
- ```typescript
198
- {
199
- smtp: {
200
- host: 'smtp-mail.outlook.com',
201
- port: 587,
202
- secure: false
203
- },
204
- imap: {
205
- host: 'outlook.office365.com',
206
- port: 993,
207
- tls: true
208
- }
209
- }
210
- ```
211
-
212
- ### QQ邮箱
213
-
214
- ```typescript
215
- {
216
- smtp: {
217
- host: 'smtp.qq.com',
218
- port: 587,
219
- secure: false
220
- },
221
- imap: {
222
- host: 'imap.qq.com',
223
- port: 993,
224
- tls: true
225
- }
226
- }
227
- ```
228
-
229
- ### 163邮箱
230
-
231
- ```typescript
232
- {
233
- smtp: {
234
- host: 'smtp.163.com',
235
- port: 465,
236
- secure: true
237
- },
238
- imap: {
239
- host: 'imap.163.com',
240
- port: 993,
241
- tls: true
242
- }
243
- }
244
- ```
245
-
246
- ## 支持的消息类型
247
-
248
- ### 接收消息
249
-
250
- | 类型 | 说明 | MessageSegment |
251
- |------|------|----------------|
252
- | 文本 | 邮件正文内容 | `text` |
253
- | HTML | HTML格式邮件 | `text` (转换为纯文本) |
254
- | 图片附件 | 图片文件 | `image` |
255
- | 文件附件 | 其他文件 | `file` |
256
- | 主题 | 邮件主题 | `text` (前缀显示) |
257
-
258
- ### 发送消息
259
-
260
- | MessageSegment 类型 | 说明 | 实现方式 |
261
- |-------------------|------|----------|
262
- | `text` | 文本内容 | 作为邮件正文 |
263
- | `image` | 图片 | 作为邮件附件 |
264
- | `file` | 文件 | 作为邮件附件 |
265
-
266
- ## 频道类型
267
-
268
- | 类型 | 说明 | channel_id 格式 |
269
- |------|------|----------------|
270
- | `private` | 个人邮箱 | 邮箱地址 |
271
-
272
- 注意:邮件适配器只支持 `private` 类型的频道,因为邮件本质上是点对点的通信方式。
273
-
274
- ## 特性
275
-
276
- ### ✅ 已实现功能
277
-
278
- - **邮件接收**: 基于 IMAP 协议接收新邮件
279
- - **邮件发送**: 基于 SMTP 协议发送邮件
280
- - **附件支持**: 支持接收和发送各种类型的附件
281
- - **定时检查**: 可配置的邮件检查间隔
282
- - **HTML转换**: 自动将HTML邮件转换为纯文本
283
- - **主题解析**: 解析并显示邮件主题
284
- - **回复支持**: 支持回复邮件
285
- - **多邮箱支持**: 支持主流邮箱服务商
286
-
287
- ### 📋 配置选项
288
-
289
- - **SMTP设置**: 完整的SMTP服务器配置
290
- - **IMAP设置**: 完整的IMAP服务器配置
291
- - **附件管理**: 可配置的附件下载和处理
292
- - **检查间隔**: 自定义邮件检查频率
293
- - **邮箱选择**: 指定监听的邮箱文件夹
294
-
295
- ### 🔧 高级功能
296
-
297
- - **邮件过滤**: 只处理未读邮件
298
- - **自动标记**: 可选的已读标记
299
- - **错误处理**: 完整的错误处理和重连机制
300
- - **日志记录**: 详细的操作日志
301
-
302
- ## 安全注意事项
303
-
304
- 1. **应用专用密码**: 建议使用应用专用密码而非账户密码
305
- 2. **TLS/SSL**: 确保启用加密连接
306
- 3. **权限最小化**: 只授予必要的邮箱权限
307
- 4. **配置保护**: 不要将邮箱密码硬编码在代码中
308
-
309
- ## 故障排除
310
-
311
- ### 连接问题
312
-
313
- 1. **SMTP连接失败**:
314
- - 检查服务器地址和端口
315
- - 确认账户密码正确
316
- - 检查是否需要应用专用密码
317
-
318
- 2. **IMAP连接失败**:
319
- - 确认IMAP服务已启用
320
- - 检查TLS设置
321
- - 验证邮箱权限设置
322
-
323
- 3. **邮件接收异常**:
324
- - 检查邮箱文件夹名称
325
- - 确认检查间隔设置
326
- - 查看错误日志
327
-
328
- ### 附件问题
329
-
330
- 1. **附件下载失败**:
331
- - 检查下载路径权限
332
- - 验证文件大小限制
333
- - 确认文件类型允许
334
-
335
- 2. **附件发送失败**:
336
- - 检查文件路径是否存在
337
- - 验证文件大小限制
338
- - 确认SMTP服务器支持
339
-
340
- ## 示例项目
341
-
342
- 完整的使用示例可以在 `test-bot` 目录中找到。
343
-
344
- ## API 参考
345
-
346
- ### EmailBot 类
347
-
348
- #### 方法
349
-
350
- - `$connect()`: 连接邮件服务器
351
- - `$disconnect()`: 断开连接
352
- - `$formatMessage(emailMsg)`: 格式化邮件消息
353
- - `$sendMessage(options)`: 发送邮件
354
-
355
- #### 事件
110
+ ## 注意事项
356
111
 
357
- - `message.receive`: 接收到新邮件时触发
112
+ - IMAP 接收使用轮询机制,`checkInterval` 控制轮询频率
113
+ - 部分邮箱服务商需要开启"第三方应用访问"或"应用专用密码"
114
+ - 建议使用环境变量存储邮箱密码
358
115
 
359
- ### 配置接口
116
+ ## 许可证
360
117
 
361
- 详细的配置接口说明请参考 TypeScript 类型定义。
118
+ MIT License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/adapter-email",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "type": "module",
5
5
  "description": "Zhin.js adapter for Email (SMTP/IMAP)",
6
6
  "main": "lib/index.js",
@@ -13,13 +13,13 @@
13
13
  "nodemailer": "^7.0.11"
14
14
  },
15
15
  "peerDependencies": {
16
- "zhin.js": "1.0.25"
16
+ "zhin.js": "1.0.26"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/mailparser": "^3.4.6",
20
20
  "@types/node": "^20.10.6",
21
21
  "typescript": "^5.3.3",
22
- "zhin.js": "1.0.25"
22
+ "zhin.js": "1.0.26"
23
23
  },
24
24
  "keywords": [
25
25
  "zhin",