From 1014abe92f809b19ab2e4b2745e6d14c69606e42 Mon Sep 17 00:00:00 2001 From: dceejay Date: Sat, 11 Apr 2015 16:09:13 +0100 Subject: [PATCH] Fix email node repeat send, add to, cc, bcc fields, and ability to select inbox. --- nodes/core/social/61-email.html | 16 +++++++++++++--- nodes/core/social/61-email.js | 19 +++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/nodes/core/social/61-email.html b/nodes/core/social/61-email.html index 6a021e675..f0375baa7 100644 --- a/nodes/core/social/61-email.html +++ b/nodes/core/social/61-email.html @@ -135,9 +135,13 @@
- +
+
+ + +

@@ -151,6 +155,7 @@

Repeatedly gets a single email from an IMAP server and forwards on as a msg if not already seen.

The subject is loaded into msg.topic and msg.payload is the plain text body. If there is text/html then that is returned in msg.html. msg.from and msg.date are also set if you need them.

+

Additionally msg.to, msg.cc, msg.bcc may exist and contain arrays of relevant email addresses.

Uses the imap module.

Note: this node only gets the most recent single email from the inbox, so set the repeat (polling) time appropriately.

@@ -161,10 +166,11 @@ category: 'social-input', color:"#c7e9c0", defaults: { - repeat: {value:"300",required:true}, + name: {value:""}, server: {value:"imap.gmail.com",required:true}, port: {value:"993",required:true}, - name: {value:""} + box: {value:"INBOX"}, + repeat: {value:"300",required:true} }, credentials: { userid: {type:"text"}, @@ -186,6 +192,10 @@ } else { $('#node-tip').hide(); }; + if (typeof this.box === 'undefined') { + $("#node-input-box").val("INBOX"); + this.box = "INBOX"; + } } }); })(); diff --git a/nodes/core/social/61-email.js b/nodes/core/social/61-email.js index 1aad5b296..daccb1d16 100644 --- a/nodes/core/social/61-email.js +++ b/nodes/core/social/61-email.js @@ -121,6 +121,7 @@ module.exports = function(RED) { this.repeat = n.repeat * 1000 || 300000; this.inserver = n.server || globalkeys.server || "imap.gmail.com"; this.inport = n.port || globalkeys.port || "993"; + this.box = n.box || "INBOX"; var flag = false; if (this.credentials && this.credentials.hasOwnProperty("userid")) { @@ -173,9 +174,9 @@ module.exports = function(RED) { imap.once('ready', function() { node.status({fill:"blue",shape:"dot",text:"fetching"}); var pay = {}; - imap.openBox('INBOX', false, function(err, box) { + imap.openBox(node.box, false, function(err, box) { if (box.messages.total > 0) { - var f = imap.seq.fetch(box.messages.total + ':*', { markSeen:true, bodies: ['HEADER.FIELDS (FROM SUBJECT DATE)','TEXT'] }); + var f = imap.seq.fetch(box.messages.total + ':*', { markSeen:true, bodies: ['HEADER.FIELDS (FROM SUBJECT DATE TO CC BCC)','TEXT'] }); f.on('message', function(msg, seqno) { node.log('message: #'+ seqno); var prefix = '(#' + seqno + ') '; @@ -186,9 +187,14 @@ module.exports = function(RED) { }); stream.on('end', function() { if (info.which !== 'TEXT') { - pay.from = Imap.parseHeader(buffer).from[0]; - pay.topic = Imap.parseHeader(buffer).subject[0]; - pay.date = Imap.parseHeader(buffer).date[0]; + var head = Imap.parseHeader(buffer); + if (RED.settings.verbose) { node.log(head); } + pay.from = head.from[0]; + pay.topic = head.subject[0]; + pay.date = head.date[0]; + if (head.hasOwnProperty("to")) { pay.to = head.to; } + if (head.hasOwnProperty("cc")) { pay.cc = head.cc; } + if (head.hasOwnProperty("bcc")) { pay.bcc = head.bcc; } } else { var parts = buffer.split("Content-Type"); for (var p = 0; p < parts.length; p++) { @@ -212,9 +218,10 @@ module.exports = function(RED) { node.status({fill:"red",shape:"ring",text:"fetch error"}); }); f.on('end', function() { + delete(pay._msgid); if (JSON.stringify(pay) !== oldmail) { - node.send(pay); oldmail = JSON.stringify(pay); + node.send(pay); node.log('received new email: '+pay.topic); } else { node.log('duplicate not sent: '+pay.topic); }