1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Fix email node repeat send, add to, cc, bcc fields, and ability to select inbox.

This commit is contained in:
dceejay 2015-04-11 16:09:13 +01:00
parent 6927f10f8f
commit 1014abe92f
2 changed files with 26 additions and 9 deletions

View File

@ -135,9 +135,13 @@
<input type="text" id="node-input-userid">
</div>
<div class="form-row">
<label for="node-config-input-password"><i class="fa fa-lock"></i> Password</label>
<label for="node-input-password"><i class="fa fa-lock"></i> Password</label>
<input type="password" id="node-input-password">
</div>
<div class="form-row">
<label for="node-input-box"><i class="fa fa-inbox"></i> Folder</label>
<input type="text" id="node-input-box">
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
@ -151,6 +155,7 @@
<p>Repeatedly gets a <b>single email</b> from an IMAP server and forwards on as a msg if not already seen.</p>
<p>The subject is loaded into <b>msg.topic</b> and <b>msg.payload</b> is the plain text body.
If there is text/html then that is returned in <b>msg.html</b>. <b>msg.from</b> and <b>msg.date</b> are also set if you need them.</p>
<p>Additionally <b>msg.to</b>, <b>msg.cc</b>, <b>msg.bcc</b> may exist and contain arrays of relevant email addresses.</p>
<p>Uses the imap module.</p>
<p><b>Note:</b> this node <i>only</i> gets the most recent single email from the inbox, so set the repeat (polling) time appropriately.</p>
</script>
@ -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";
}
}
});
})();

View File

@ -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); }