@5minds/node-red-contrib-processcube-tools 1.0.1-feature-ecae8e-mffb2tah → 1.0.1-feature-c82263-mffckkf9

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.
@@ -1,114 +0,0 @@
1
- module.exports = function(RED) {
2
- "use strict";
3
- const nodemailer = require("nodemailer");
4
-
5
- function EmailSenderNode(config) {
6
- RED.nodes.createNode(this, config);
7
- var node = this;
8
-
9
- node.on('input', function(msg, send, done) {
10
- send = send || function() { node.send.apply(node, arguments); };
11
- done = done || function(err) { if (err) node.error(err, msg); };
12
-
13
- // Retrieve and evaluate mail configuration values
14
- const sender = RED.util.evaluateNodeProperty(config.sender, config.senderType, node, msg);
15
- const address = RED.util.evaluateNodeProperty(config.address, config.addressType, node, msg);
16
- const to = RED.util.evaluateNodeProperty(config.to, config.toType, node, msg);
17
- const cc = RED.util.evaluateNodeProperty(config.cc, config.ccType, node, msg) || "";
18
- const bcc = RED.util.evaluateNodeProperty(config.bcc, config.bccType, node, msg) || "";
19
- const subject = RED.util.evaluateNodeProperty(config.subject, config.subjectType, node, msg) || msg.topic || "Message from Node-RED";
20
- const attachments = RED.util.evaluateNodeProperty(config.attachments, config.attachmentsType, node, msg);
21
- const htmlContent = RED.util.evaluateNodeProperty(config.htmlContent, config.htmlContentType, node, msg);
22
-
23
- // Retrieve and evaluate SMTP configuration values
24
- const host = RED.util.evaluateNodeProperty(config.host, config.hostType, node, msg);
25
- const port = RED.util.evaluateNodeProperty(config.port, config.portType, node, msg);
26
- const user = RED.util.evaluateNodeProperty(config.user, config.userType, node, msg);
27
- const password = RED.util.evaluateNodeProperty(config.password, config.passwordType, node, msg);
28
- const secure = RED.util.evaluateNodeProperty(config.secure, config.secureType, node, msg);
29
- const rejectUnauthorized = RED.util.evaluateNodeProperty(config.rejectUnauthorized, config.rejectUnauthorizedType, node, msg);
30
-
31
- // Handle attachments and format them for Nodemailer
32
- let processedAttachments = [];
33
- if (attachments) {
34
- // Check if it's a single attachment or an array
35
- const attachmentArray = Array.isArray(attachments) ? attachments : [attachments];
36
-
37
- for (const attachment of attachmentArray) {
38
- try {
39
- // Assuming the attachment object has a 'filename' and 'content' property
40
- if (attachment.filename && attachment.content) {
41
- processedAttachments.push({
42
- filename: attachment.filename,
43
- content: attachment.content
44
- });
45
- } else {
46
- node.warn("Attachment object is missing 'filename' or 'content' property and will be ignored.");
47
- }
48
- } catch (e) {
49
- node.error("Failed to process attachment: " + e.message);
50
- }
51
- }
52
- }
53
-
54
- // Create SMTP transporter
55
- const transporter = nodemailer.createTransport({
56
- host: host,
57
- port: port,
58
- secure: secure,
59
- auth: {
60
- user: user,
61
- pass: password
62
- },
63
- tls: {
64
- rejectUnauthorized: rejectUnauthorized
65
- }
66
- });
67
-
68
- // Create email object
69
- const mailOptions = {
70
- from: {
71
- name: sender,
72
- address: address
73
- },
74
- to: to,
75
- cc: cc,
76
- bcc: bcc,
77
- subject: subject,
78
- html: Buffer.from(htmlContent, 'utf-8'),
79
- attachments: processedAttachments
80
- };
81
-
82
- // Send email
83
- transporter.sendMail(mailOptions, (error, info) => {
84
- if (error) {
85
- node.status({ fill: "red", shape: "dot", text: "error sending" });
86
- done(error);
87
- } else {
88
- node.log('Email sent: ' + info.response);
89
- msg.payload = info;
90
-
91
- if (msg.payload.accepted && msg.payload.accepted.length > 0) {
92
- msg.payload = msg.input;
93
- node.status({ fill: "green", shape: "dot", text: "sent" });
94
- send(msg);
95
- done();
96
- } else if (msg.payload.rejected && msg.payload.rejected.length > 0) {
97
- msg.error = { result: msg.payload.rejected };
98
- node.status({ fill: "red", shape: "dot", text: "rejected" });
99
- done(new Error('Email rejected: ' + msg.payload.rejected.join(', ')));
100
- } else if (msg.payload.pending && msg.payload.pending.length > 0) {
101
- msg.error = { result: msg.payload.pending };
102
- node.status({ fill: "yellow", shape: "dot", text: "pending" });
103
- done(new Error('Email pending: ' + msg.payload.pending.join(', ')));
104
- } else {
105
- node.status({ fill: "red", shape: "dot", text: "unknown error" });
106
- done(new Error('Unknown error while sending email.'));
107
- }
108
- }
109
- });
110
- });
111
- }
112
-
113
- RED.nodes.registerType("email-sender", EmailSenderNode);
114
- };