UNPKG

node-red-contrib-feishu

Version:

Node-RED节点用于发送飞书消息

87 lines (76 loc) 2.89 kB
module.exports = function(RED) { /** * 邮件配置节点构造函数 * @param {Object} config - 节点配置对象 */ function EmailConfigNode(config) { RED.nodes.createNode(this, config); this.host = config.host; this.port = config.port; this.secure = config.secure; this.user = config.user; this.password = config.password; } // 注册邮件配置节点 RED.nodes.registerType("email-config", EmailConfigNode); /** * 邮件节点构造函数 * @param {Object} config - 节点配置对象 */ function EmailNode(config) { RED.nodes.createNode(this, config); const node = this; const nodemailer = require('nodemailer'); // 获取邮件配置 const emailConfig = RED.nodes.getNode(config.emailConfig); // 创建邮件传输对象 const transporter = nodemailer.createTransport({ host: emailConfig.host, port: emailConfig.port, secure: emailConfig.secure, auth: { user: emailConfig.user, pass: emailConfig.password } }); node.on('input', async function(msg) { try { // 验证必要参数 if (!msg.payload) { throw new Error('邮件内容是必需的'); } // 构建邮件选项 const mailOptions = { from: emailConfig.user, to: config.to || msg.to, subject: config.subject || msg.subject || '', }; // 根据消息类型设置内容 const messageType = config.messageType || msg.messageType || 'text'; if (messageType === 'html') { mailOptions.html = typeof msg.payload === 'string' ? msg.payload : JSON.stringify(msg.payload); } else { mailOptions.text = typeof msg.payload === 'string' ? msg.payload : JSON.stringify(msg.payload); } // 发送邮件 const info = await transporter.sendMail(mailOptions); // 发送成功 node.status({fill: "green", shape: "dot", text: "发送成功"}); msg.payload = info; node.send(msg); } catch (error) { // 发送失败 node.status({fill: "red", shape: "ring", text: "发送失败"}); node.error(error.message, msg); } }); // 节点关闭时清除状态 node.on('close', function() { node.status({}); }); } // 注册邮件节点 RED.nodes.registerType('email', EmailNode); };