befly 3.18.8 → 3.18.9

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.
@@ -26,7 +26,7 @@ export default {
26
26
 
27
27
  const startTime = Date.now();
28
28
 
29
- const result = await befly.email.send({
29
+ const result = await befly.email.sendMail({
30
30
  to: ctx.body.to,
31
31
  subject: ctx.body.subject,
32
32
  html: ctx.body.isHtml ? ctx.body.content : undefined,
@@ -50,7 +50,7 @@ export default {
50
50
  sendTime: startTime,
51
51
  sendResult: result.success ? 1 : 0,
52
52
  messageId: result.messageId || "",
53
- failReason: result.error || ""
53
+ failReason: result.reason || ""
54
54
  }
55
55
  });
56
56
  } catch (logError) {
@@ -61,6 +61,6 @@ export default {
61
61
  return befly.tool.Yes("发送成功", { messageId: result.messageId });
62
62
  }
63
63
 
64
- return befly.tool.No(result.error || "发送失败");
64
+ return befly.tool.No(result.reason || "发送失败");
65
65
  }
66
66
  };
@@ -42,6 +42,15 @@
42
42
  "expireDays": 7
43
43
  },
44
44
 
45
+ "email": {
46
+ "host": "smtp.qq.com",
47
+ "port": 465,
48
+ "secure": true,
49
+ "user": "",
50
+ "pass": "",
51
+ "label": "befly"
52
+ },
53
+
45
54
  "cors": {
46
55
  "origin": "*",
47
56
  "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "befly",
3
- "version": "3.18.8",
4
- "gitHead": "28fcd2cee85330bc97d228712c4cbb2252ee0a06",
3
+ "version": "3.18.9",
4
+ "gitHead": "1ef40fc05e0a2ed143aaa8803901fd58aa668a73",
5
5
  "private": false,
6
6
  "description": "Befly - 为 Bun 专属打造的 JavaScript API 接口框架核心引擎",
7
7
  "keywords": [
@@ -52,7 +52,7 @@
52
52
  "test": "bun test"
53
53
  },
54
54
  "dependencies": {
55
- "fast-xml-parser": "^5.3.7",
55
+ "fast-xml-parser": "^5.4.1",
56
56
  "nodemailer": "^8.0.1",
57
57
  "pathe": "^2.0.3",
58
58
  "ua-parser-js": "^2.0.9",
package/plugins/email.js CHANGED
@@ -3,25 +3,67 @@
3
3
  * 提供邮件发送功能,支持 SMTP 配置
4
4
  */
5
5
 
6
- import { EmailHelper } from "../lib/emailHelper.js";
7
-
8
- /** 默认配置 */
9
- const defaultConfig = {
10
- host: "smtp.qq.com",
11
- port: 465,
12
- secure: true,
13
- user: "",
14
- pass: "",
15
- fromName: "Befly System"
16
- };
6
+ import { Logger } from "../lib/logger.js";
7
+ import nodemailer from "nodemailer";
17
8
 
18
9
  export default {
19
10
  order: 7,
20
11
  async handler(befly) {
21
- // befly.config.email 获取配置
22
- const emailConfigRaw = (befly.config ? befly.config["email"] : undefined) || {};
23
- const emailConfig = Object.assign({}, defaultConfig, emailConfigRaw);
12
+ const config = befly?.config?.email || {};
13
+ let transporter = null;
14
+
15
+ if (config && config.host && config.user) {
16
+ try {
17
+ transporter = nodemailer.createTransport({
18
+ host: config.host,
19
+ port: config.port || 25,
20
+ secure: config.ssl,
21
+ auth: {
22
+ user: config.user,
23
+ pass: config.pass
24
+ },
25
+ connectionTimeout: config.timeout || 10000
26
+ });
27
+ } catch (error) {
28
+ Logger.warn("邮件服务初始化失败", { err: error });
29
+ }
30
+ }
31
+
32
+ const sendMail = async function (options) {
33
+ if (!transporter) {
34
+ Logger.warn("邮件未配置,已禁用发送");
35
+ return {
36
+ success: false,
37
+ reason: "邮件未配置,已禁用发送"
38
+ };
39
+ }
24
40
 
25
- return new EmailHelper(befly, emailConfig);
41
+ try {
42
+ const info = await transporter.sendMail({
43
+ text: options.text,
44
+ html: options.html,
45
+ from: options.from || config.from || config.user,
46
+ to: options.to,
47
+ cc: options.cc,
48
+ bcc: options.bcc,
49
+ subject: options.subject,
50
+ attachments: options.attachments
51
+ });
52
+ return {
53
+ success: true,
54
+ reason: "发送成功",
55
+ messageId: info?.messageId || ""
56
+ };
57
+ } catch (error) {
58
+ Logger.warn("发送邮件失败", { err: error });
59
+ return {
60
+ success: false,
61
+ reason: error?.message || "发送邮件失败"
62
+ };
63
+ }
64
+ };
65
+ return {
66
+ sendMail: sendMail
67
+ };
26
68
  }
27
69
  };
@@ -1,110 +0,0 @@
1
- /**
2
- * 邮件发送工具类 - JavaScript 版本
3
- */
4
-
5
- import { Logger } from "./logger.js";
6
-
7
- /**
8
- * 邮件发送工具类
9
- */
10
- export class EmailHelper {
11
- /**
12
- * 创建 EmailHelper 实例
13
- * @param config - 邮件配置
14
- */
15
- constructor(config) {
16
- this.config = config;
17
- }
18
-
19
- /**
20
- * 发送邮件
21
- * @param options - 邮件选项
22
- * @returns 是否发送成功
23
- */
24
- async send(options) {
25
- const config = this.getConfig();
26
-
27
- if (!config || !config.host || !config.user) {
28
- Logger.warn("邮件未配置,已禁用发送");
29
- return false;
30
- }
31
-
32
- try {
33
- const result = await this.doSend(config, options);
34
- if (result.success) {
35
- return true;
36
- }
37
-
38
- Logger.warn("发送邮件失败", { err: result.error });
39
- return false;
40
- } catch (error) {
41
- Logger.error("发送邮件异常", error);
42
- return false;
43
- }
44
- }
45
-
46
- /**
47
- * 获取邮件配置
48
- * @returns 完整配置
49
- */
50
- getConfig() {
51
- if (!this.config) {
52
- return null;
53
- }
54
-
55
- const result = Object.assign({}, this.config);
56
- result.pass = this.config.pass || process.env.SMTP_PASS;
57
- result.ssl = Boolean(this.config.ssl);
58
- return result;
59
- }
60
-
61
- /**
62
- * 实际发送邮件
63
- * @param config - 邮件配置
64
- * @param options - 邮件选项
65
- * @returns 发送结果
66
- */
67
- async doSend(config, options) {
68
- let emailService;
69
-
70
- try {
71
- // 动态加载 emailjs
72
- const emailjs = await import("emailjs");
73
- emailService = emailjs.server.connect({
74
- user: config.user,
75
- password: config.pass,
76
- host: config.host,
77
- port: config.port || 25,
78
- ssl: config.ssl,
79
- timeout: config.timeout || 10000
80
- });
81
- } catch (error) {
82
- return { success: false, error: error };
83
- }
84
-
85
- try {
86
- await emailService.send({
87
- text: options.text || options.html,
88
- from: options.from || config.from || config.user,
89
- to: options.to,
90
- cc: options.cc,
91
- bcc: options.bcc,
92
- subject: options.subject,
93
- attachment: options.attachments
94
- });
95
-
96
- return { success: true };
97
- } catch (error) {
98
- return { success: false, error: error };
99
- }
100
- }
101
- }
102
-
103
- /**
104
- * 创建邮件工具实例
105
- * @param config - 邮件配置
106
- * @returns EmailHelper 实例
107
- */
108
- export function createEmailHelper(config) {
109
- return new EmailHelper(config);
110
- }