Skip to content

Commit

Permalink
Improvements and fixes for 61-email component (node-red#1075)
Browse files Browse the repository at this point in the history
* 61-email: fix useTLS and its span in HTML. Removes unused code in 61-email.js. Adds description on how to test the entity

* 61-email: fix useTLS and its span in HTML. Removes unused code in 61-email.js. Adds description on how to test the entity
  • Loading branch information
LaQuay authored Jul 4, 2024
1 parent 35c4061 commit 45d4fd0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion social/email/61-email.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</div>
<br/>
<div class="form-row node-input-useTLS">
<label for="node-input-useTLS"><i class="fa fa-lock"></i> <span data-i18n="email.label.useTLS"></label>
<label for="node-input-tls"><i class="fa fa-lock"></i> <span data-i18n="email.label.useTLS"></span></label>
<input type="checkbox" id="node-input-tls" style="display:inline-block; width:20px; vertical-align:baseline;">
<span data-i18n="email.label.rejectUnauthorised"></span>
</div>
Expand Down
16 changes: 7 additions & 9 deletions social/email/61-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const { domainToUnicode } = require("url");

module.exports = function(RED) {
"use strict";
var util = require("util");
var Imap = require('imap');
var Pop3Command = require("node-pop3");
var nodemailer = require("nodemailer");
Expand All @@ -34,7 +33,7 @@ module.exports = function(RED) {
this.outserver = n.server;
this.outport = n.port;
this.secure = n.secure;
this.tls = true;
this.tls = n.tls;
this.authtype = n.authtype || "BASIC";
if (this.authtype !== "BASIC") {
this.inputs = 1;
Expand Down Expand Up @@ -62,7 +61,6 @@ module.exports = function(RED) {
this.error(RED._("email.errors.notoken"));
}
}
if (n.tls === false) { this.tls = false; }
var node = this;

var smtpOptions = {
Expand All @@ -87,9 +85,9 @@ module.exports = function(RED) {
}

this.on("input", function(msg, send, done) {

if (node.authtype === "XOAUTH2") {
if (node.token) {
var saslxoauth2 = "";
var value = RED.util.getMessageProperty(msg,node.token);
if (value !== undefined) {
if (node.saslformat) {
Expand Down Expand Up @@ -119,7 +117,7 @@ module.exports = function(RED) {
node.warn(RED._("node-red:common.errors.nooverride"));
}
var sendopts = { from: ((msg.from) ? msg.from : node.userid) }; // sender address
sendopts.to = node.name || msg.to; // comma separated list of addressees
sendopts.to = node.name || msg.to; // comma separated list of addresses
if (node.name === "") {
sendopts.cc = msg.cc;
sendopts.bcc = msg.bcc;
Expand Down Expand Up @@ -167,9 +165,9 @@ module.exports = function(RED) {
if (msg.attachments) {
if (!Array.isArray(msg.attachments)) { sendopts.attachments = [ msg.attachments ]; }
else { sendopts.attachments = msg.attachments; }
for (var a=0; a < sendopts.attachments.length; a++) {
if (sendopts.attachments[a].hasOwnProperty("content")) {
if (typeof sendopts.attachments[a].content !== "string" && !Buffer.isBuffer(sendopts.attachments[a].content)) {
for (const attachment of sendopts.attachments) {
if (attachment.hasOwnProperty('content')) {
if (typeof attachment.content !== 'string' && !Buffer.isBuffer(attachment.content)) {
node.error(RED._("email.errors.invalidattachment"),msg);
node.status({fill:"red",shape:"ring",text:"email.status.sendfail"});
return;
Expand All @@ -194,7 +192,7 @@ module.exports = function(RED) {
else { node.warn(RED._("email.errors.nopayload")); }
});
}
RED.nodes.registerType("e-mail",EmailNode,{
RED.nodes.registerType("e-mail", EmailNode, {
credentials: {
userid: {type:"text"},
password: {type: "password"},
Expand Down
52 changes: 50 additions & 2 deletions social/email/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ or enable <a target="_new" href="https://support.google.com/accounts/answer/6010
Office 365 users
----------------

If you are accessing Exchnage you will need to register an application through their platform and use OAuth2.0.
If you are accessing Exchange you will need to register an application through their platform and use OAuth2.0.
<a target="_new" href="https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#get-an-access-token">Details on how to do this can be found here.</a>

Usage
Expand Down Expand Up @@ -71,7 +71,7 @@ address (userxx@some_domain.com), you may see *(No Sender)* in the email.

The payload can be html format. You can also specify `msg.plaintext` if the main payload is html.

If the payload is a binary buffer then it will be converted to an attachment.
If the payload is a binary buffer, then it will be converted to an attachment.

The filename should be set using `msg.filename`. Optionally
`msg.description` can be added for the body text.
Expand All @@ -86,3 +86,51 @@ If you have own signed certificates, Nodemailer can complain about that and refu
Use secure connection - If enabled the connection will use TLS when connecting to server. If disabled then TLS is used if server supports the STARTTLS extension. In most cases set this to enabled if you are connecting to port 465. For port 587 or 25 keep it disabled.

This node uses the *nodemailer* npm module.

Testing
-----

You can pass the credentials object to the `node-red-node-test-helper` by doing the following:

```js
const emailNode = require("./61-email");

const testFlows = [{
id: "n1", type: "e-mail", name: "Email",
from: "email1test@example.com", subject: "TestSubject", server: "testServer",
port: "1111", secure: "X", tls: true, authtype: "BASIC",
}];

const testCredentials = {
n1: {
userid: "ExampleUser",
password: "ExamplePassword",
global: false
}
};

it('should be loaded', function (done) {
helper.load(emailNode, testFlows, testCredentials, function () {
const n1 = helper.getNode("n1");
try {
n1.should.have.property('name', 'Email');
n1.should.have.property('from', 'email1test@example.com');
n1.should.have.property('subject', 'TestSubject');
n1.should.have.property('outserver', 'testServer'); // Gathered via server
n1.should.have.property('outport', '1111'); // Gathered via port
n1.should.have.property('secure', 'X');
n1.should.have.property('tls', true);
n1.should.have.property('authtype', 'BASIC');
n1.should.have.property('credentials');
n1.should.have.property('credentials', {
userid: "ExampleUser",
password: "ExamplePassword",
global: false
});
done();
} catch (err) {
done(err);
}
});
});
```

0 comments on commit 45d4fd0

Please sign in to comment.