mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Collects every unseen email
This commit is contained in:
parent
285450523c
commit
b33bbc507c
@ -18,6 +18,7 @@ module.exports = function(RED) {
|
|||||||
"use strict";
|
"use strict";
|
||||||
var nodemailer = require("nodemailer");
|
var nodemailer = require("nodemailer");
|
||||||
var Imap = require('imap');
|
var Imap = require('imap');
|
||||||
|
var mimelib = require('mimelib');
|
||||||
|
|
||||||
//console.log(nodemailer.Transport.transports.SMTP.wellKnownHosts);
|
//console.log(nodemailer.Transport.transports.SMTP.wellKnownHosts);
|
||||||
|
|
||||||
@ -168,7 +169,8 @@ module.exports = function(RED) {
|
|||||||
tls: true,
|
tls: true,
|
||||||
tlsOptions: { rejectUnauthorized: false },
|
tlsOptions: { rejectUnauthorized: false },
|
||||||
connTimeout: node.repeat,
|
connTimeout: node.repeat,
|
||||||
authTimeout: node.repeat
|
authTimeout: node.repeat//,
|
||||||
|
//debug: function(msg) { console.log(msg); }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!isNaN(this.repeat) && this.repeat > 0) {
|
if (!isNaN(this.repeat) && this.repeat > 0) {
|
||||||
@ -188,66 +190,73 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (box.messages.total > 0) {
|
if (box.messages.total > 0) {
|
||||||
//var f = imap.seq.fetch(box.messages.total + ':*', { markSeen:true, bodies: ['HEADER.FIELDS (FROM SUBJECT DATE TO CC BCC)','TEXT'] });
|
imap.seq.search([ 'UNSEEN' ], function(err, results) {
|
||||||
var f = imap.seq.fetch(box.messages.total + ':*', { markSeen:true, bodies: ['HEADER','TEXT'] });
|
if (err) throw err;
|
||||||
f.on('message', function(msg, seqno) {
|
//console.log(results);
|
||||||
node.log(RED._("email.status.message",{number:seqno}));
|
if (results.length > 0) {
|
||||||
var prefix = '(#' + seqno + ') ';
|
var f = imap.fetch(results, { markSeen: true, bodies: ['HEADER','TEXT'] });
|
||||||
msg.on('body', function(stream, info) {
|
f.on('message', function(msg, seqno) {
|
||||||
var buffer = '';
|
node.log(RED._("email.status.message",{number:seqno}));
|
||||||
stream.on('data', function(chunk) {
|
var prefix = '(#' + seqno + ') ';
|
||||||
buffer += chunk.toString('utf8');
|
msg.on('body', function(stream, info) {
|
||||||
});
|
var buffer = '';
|
||||||
stream.on('end', function() {
|
stream.on('data', function(chunk) {
|
||||||
if (info.which !== 'TEXT') {
|
buffer += chunk.toString('utf8');
|
||||||
var head = Imap.parseHeader(buffer);
|
});
|
||||||
if (head.hasOwnProperty("from")) { pay.from = head.from[0]; }
|
stream.on('end', function() {
|
||||||
if (head.hasOwnProperty("subject")) { pay.topic = head.subject[0]; }
|
if (info.which !== 'TEXT') {
|
||||||
if (head.hasOwnProperty("date")) { pay.date = head.date[0]; }
|
var head = Imap.parseHeader(buffer);
|
||||||
pay.header = head;
|
if (head.hasOwnProperty("from")) { pay.from = head.from[0]; }
|
||||||
} else {
|
if (head.hasOwnProperty("subject")) { pay.topic = head.subject[0]; }
|
||||||
var parts = buffer.split("Content-Type");
|
if (head.hasOwnProperty("date")) { pay.date = head.date[0]; }
|
||||||
for (var p = 0; p < parts.length; p++) {
|
pay.header = head;
|
||||||
if (parts[p].indexOf("text/plain") >= 0) {
|
} else {
|
||||||
pay.payload = parts[p].split("\n").slice(1,-2).join("\n").trim();
|
var parts = buffer.split("Content-Type");
|
||||||
}
|
for (var p = 0; p < parts.length; p++) {
|
||||||
else if (parts[p].indexOf("text/html") >= 0) {
|
if (parts[p].indexOf("text/plain") >= 0) {
|
||||||
pay.html = parts[p].split("\n").slice(1,-2).join("\n").trim();
|
pay.payload = mimelib.decodeQuotedPrintable(parts[p].split("\n").slice(1,-2).join("\n").trim());
|
||||||
} else {
|
}
|
||||||
pay.payload = parts[0];
|
else if (parts[p].indexOf("text/html") >= 0) {
|
||||||
}
|
pay.html = mimelib.decodeQuotedPrintable(parts[p].split("\n").slice(1,-2).join("\n").trim());
|
||||||
}
|
}
|
||||||
//pay.body = buffer;
|
else {
|
||||||
}
|
pay.payload = mimelib.decodeQuotedPrintable(parts[0]);
|
||||||
});
|
}
|
||||||
});
|
}
|
||||||
msg.on('end', function() {
|
}
|
||||||
//node.log('finished: '+prefix);
|
});
|
||||||
});
|
});
|
||||||
});
|
msg.on('end', function() {
|
||||||
f.on('error', function(err) {
|
node.send(pay);
|
||||||
node.warn(RED._("email.errors.messageerror",{error:err}));
|
node.log(RED._("email.status.newemail",{topic: prefix+": "+pay.topic}));
|
||||||
node.status({fill:"red",shape:"ring",text:"email.status.messageerror"});
|
});
|
||||||
});
|
});
|
||||||
f.on('end', function() {
|
|
||||||
delete(pay._msgid);
|
f.once('error', function(err) {
|
||||||
if (JSON.stringify(pay) !== oldmail) {
|
node.warn(RED._("email.errors.messageerror",{error:err}));
|
||||||
oldmail = JSON.stringify(pay);
|
node.status({fill:"red",shape:"ring",text:"email.status.messageerror"});
|
||||||
node.send(pay);
|
});
|
||||||
node.log(RED._("email.status.newemail",{topic:pay.topic}));
|
|
||||||
}
|
f.once('end', function() {
|
||||||
else { node.log(RED._("email.status.duplicate",{topic:pay.topic})); }
|
node.log("All emails read and sent");
|
||||||
//node.status({fill:"green",shape:"dot",text:"node-red:common.status.ok"});
|
node.status({});
|
||||||
node.status({});
|
imap.end();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
node.log(RED._("email.status.inboxzero"));
|
||||||
|
imap.end();
|
||||||
|
node.status({});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
node.log(RED._("email.status.inboxzero"));
|
node.log(RED._("email.status.inboxzero"));
|
||||||
//node.status({fill:"green",shape:"dot",text:"node-red:common.status.ok"});
|
imap.end();
|
||||||
node.status({});
|
node.status({});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imap.end();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
node.status({fill:"grey",shape:"dot",text:"node-red:common.status.connecting"});
|
node.status({fill:"grey",shape:"dot",text:"node-red:common.status.connecting"});
|
||||||
@ -272,7 +281,7 @@ module.exports = function(RED) {
|
|||||||
credentials: {
|
credentials: {
|
||||||
userid: {type:"text"},
|
userid: {type:"text"},
|
||||||
password: {type: "password"},
|
password: {type: "password"},
|
||||||
global: { type:"boolean"}
|
global: {type:"boolean"}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user