mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Update IMAP node to use new 0.8.x API
Fixes Issue #96 this necessitates an update to the underlying npm npm install --force imap
This commit is contained in:
parent
7040aaa179
commit
ab04fcf7c0
@ -15,23 +15,24 @@
|
||||
-->
|
||||
|
||||
<script type="text/x-red" data-template-name="imap">
|
||||
<div class="form-row node-input-repeat">
|
||||
<label for="node-input-repeat"><i class="icon-repeat"></i>Repeat (S)</label>
|
||||
<input type="text" id="node-input-repeat" placeholder="300">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
<div class="form-row node-input-repeat">
|
||||
<label for="node-input-repeat"><i class="icon-repeat"></i>Repeat (S)</label>
|
||||
<input type="text" id="node-input-repeat" placeholder="300">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="imap">
|
||||
<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 body text. <b>msg.from</b> is also set if you need it.</p>
|
||||
<p>Uses the imap module - you also need to pre-configure your email settings in a file emailkeys.js as per below.</p>
|
||||
<p><pre>module.exports = { service: "Gmail", user: "blahblah@gmail.com", pass: "password", server: "imap.gmail.com", port: "993" }</pre></p>
|
||||
<p>This <b>must</b> be located in the diectory above node-red.</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>
|
||||
<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>Uses the imap module - you also need to pre-configure your email settings in a file emailkeys.js as per below.</p>
|
||||
<p><pre>module.exports = { service: "Gmail", user: "blahblah@gmail.com", pass: "password", server: "imap.gmail.com", port: "993" }</pre></p>
|
||||
<p>This <b>must</b> be located in the directory above node-red.</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>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
@ -31,14 +31,12 @@ var imap = new Imap({
|
||||
password: emailkey.pass,
|
||||
host: emailkey.server||"imap.gmail.com",
|
||||
port: emailkey.port||"993",
|
||||
secure: true
|
||||
tls: true,
|
||||
tlsOptions: { rejectUnauthorized: false }
|
||||
});
|
||||
|
||||
function openInbox(cb) {
|
||||
imap.connect(function(err) {
|
||||
if (err) util.log("[imap] : error : "+err);
|
||||
imap.openBox('INBOX', true, cb);
|
||||
});
|
||||
imap.openBox('INBOX', true, cb);
|
||||
}
|
||||
|
||||
function ImapNode(n) {
|
||||
@ -56,42 +54,64 @@ function ImapNode(n) {
|
||||
}
|
||||
|
||||
this.on("input", function(msg) {
|
||||
openInbox(function(err, mailbox) {
|
||||
if (err) node.log("error : "+err);
|
||||
imap.seq.fetch(mailbox.messages.total + ':*', { struct: false },
|
||||
{ headers: ['from', 'subject'],
|
||||
body: true,
|
||||
cb: function(fetch) {
|
||||
fetch.on('message', function(msg) {
|
||||
node.log('Read message no. ' + msg.seqno);
|
||||
var pay = {};
|
||||
var body = '';
|
||||
msg.on('headers', function(hdrs) {
|
||||
pay.from = hdrs.from[0];
|
||||
pay.topic = hdrs.subject[0];
|
||||
});
|
||||
msg.on('data', function(chunk) {
|
||||
body += chunk.toString('utf8');
|
||||
});
|
||||
msg.on('end', function() {
|
||||
pay.payload = body;
|
||||
if ((pay.topic !== oldmail.topic)|(pay.payload !== oldmail.payload)) {
|
||||
oldmail = pay;
|
||||
//node.log("From: "+pay.from);
|
||||
node.log("Subj: "+pay.topic);
|
||||
//node.log("Body: "+pay.payload);
|
||||
node.send(pay);
|
||||
}
|
||||
});
|
||||
imap.once('ready', function() {
|
||||
var pay = {};
|
||||
openInbox(function(err, box) {
|
||||
//if (err) throw err;
|
||||
var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM SUBJECT DATE)','TEXT'] });
|
||||
f.on('message', function(msg, seqno) {
|
||||
node.log('message: #'+ seqno);
|
||||
var prefix = '(#' + seqno + ') ';
|
||||
msg.on('body', function(stream, info) {
|
||||
var buffer = '';
|
||||
stream.on('data', function(chunk) {
|
||||
buffer += chunk.toString('utf8');
|
||||
});
|
||||
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];
|
||||
} else {
|
||||
var parts = buffer.split("Content-Type");
|
||||
for (var p in parts) {
|
||||
if (parts[p].indexOf("text/plain") >= 0) {
|
||||
pay.payload = parts[p].split("\n").slice(1,-2).join("\n").trim();
|
||||
}
|
||||
if (parts[p].indexOf("text/html") >= 0) {
|
||||
pay.html = parts[p].split("\n").slice(1,-2).join("\n").trim();
|
||||
}
|
||||
}
|
||||
//pay.body = buffer;
|
||||
}
|
||||
});
|
||||
});
|
||||
msg.on('end', function() {
|
||||
//node.log('Finished: '+prefix);
|
||||
});
|
||||
});
|
||||
}
|
||||
}, function(err) {
|
||||
if (err) node.log("error : "+err);
|
||||
node.log("Done fetching messages.");
|
||||
imap.logout();
|
||||
}
|
||||
);
|
||||
f.on('error', function(err) {
|
||||
node.warn('fetch error: ' + err);
|
||||
});
|
||||
f.on('end', function() {
|
||||
if (JSON.stringify(pay) !== oldmail) {
|
||||
node.send(pay);
|
||||
oldmail = JSON.stringify(pay);
|
||||
}
|
||||
imap.end();
|
||||
node.log('done fetching message');
|
||||
});
|
||||
});
|
||||
});
|
||||
imap.connect();
|
||||
});
|
||||
|
||||
imap.on('error', function(err) {
|
||||
util.log(err);
|
||||
});
|
||||
|
||||
imap.on('end', function() {
|
||||
//util.log('Connection ended');
|
||||
});
|
||||
|
||||
this.on("close", function() {
|
||||
@ -100,6 +120,10 @@ function ImapNode(n) {
|
||||
}
|
||||
});
|
||||
|
||||
this.on("error", function(err) {
|
||||
node.log("error: ",err);
|
||||
});
|
||||
|
||||
node.emit("input",{});
|
||||
}
|
||||
RED.nodes.registerType("imap",ImapNode);
|
||||
|
Loading…
Reference in New Issue
Block a user