2 * Copyright 2013,2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 module.exports = function(RED) {
19 var nodemailer = require("nodemailer");
20 var Imap = require('imap');
22 //console.log(nodemailer.Transport.transports.SMTP.wellKnownHosts);
25 var globalkeys = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
29 function EmailNode(n) {
30 RED.nodes.createNode(this,n);
33 this.outserver = n.server;
34 this.outport = n.port;
36 if (this.credentials && this.credentials.hasOwnProperty("userid")) {
37 this.userid = this.credentials.userid;
40 this.userid = globalkeys.user;
43 this.error("No e-mail userid set");
46 if (this.credentials && this.credentials.hasOwnProperty("password")) {
47 this.password = this.credentials.password;
50 this.password = globalkeys.pass;
53 this.error("No e-mail password set");
57 RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true});
61 var smtpTransport = nodemailer.createTransport({
71 this.on("input", function(msg) {
73 node.status({fill:"blue",shape:"dot",text:"sending"});
74 var payload = RED.util.ensureString(msg.payload);
75 smtpTransport.sendMail({
76 from: node.userid, // sender address
77 to: msg.to || node.name, // comma separated list of addressees
78 subject: msg.topic, // subject line
79 text: payload // plaintext body
80 }, function(error, info) {
83 node.status({fill:"red",shape:"ring",text:"send failed"});
85 node.log("Message sent: " + info.response);
90 else { node.warn("No Email credentials found. See info panel."); }
93 RED.nodes.registerType("e-mail",EmailNode,{
95 userid: {type:"text"},
96 password: {type: "password"},
97 global: { type:"boolean"}
101 function EmailInNode(n) {
102 RED.nodes.createNode(this,n);
104 this.repeat = n.repeat * 1000 || 300000;
105 this.inserver = n.server || globalkeys.server || "imap.gmail.com";
106 this.inport = n.port || globalkeys.port || "993";
109 if (this.credentials && this.credentials.hasOwnProperty("userid")) {
110 this.userid = this.credentials.userid;
113 this.userid = globalkeys.user;
116 this.error("No e-mail userid set");
119 if (this.credentials && this.credentials.hasOwnProperty("password")) {
120 this.password = this.credentials.password;
123 this.password = globalkeys.pass;
126 this.error("No e-mail password set");
130 RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true});
134 this.interval_id = null;
137 var imap = new Imap({
139 password: node.password,
143 tlsOptions: { rejectUnauthorized: false },
144 connTimeout: node.repeat,
145 authTimeout: node.repeat
148 if (!isNaN(this.repeat) && this.repeat > 0) {
149 node.log("repeat = "+this.repeat);
150 this.interval_id = setInterval( function() {
151 node.emit("input",{});
155 this.on("input", function(msg) {
156 imap.once('ready', function() {
157 node.status({fill:"blue",shape:"dot",text:"fetching"});
159 imap.openBox('INBOX', true, function(err, box) {
160 if (box.messages.total > 0) {
161 var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM SUBJECT DATE)','TEXT'] });
162 f.on('message', function(msg, seqno) {
163 node.log('message: #'+ seqno);
164 var prefix = '(#' + seqno + ') ';
165 msg.on('body', function(stream, info) {
167 stream.on('data', function(chunk) {
168 buffer += chunk.toString('utf8');
170 stream.on('end', function() {
171 if (info.which !== 'TEXT') {
172 pay.from = Imap.parseHeader(buffer).from[0];
173 pay.topic = Imap.parseHeader(buffer).subject[0];
174 pay.date = Imap.parseHeader(buffer).date[0];
176 var parts = buffer.split("Content-Type");
177 for (var p = 0; p < parts.length; p++) {
178 if (parts[p].indexOf("text/plain") >= 0) {
179 pay.payload = parts[p].split("\n").slice(1,-2).join("\n").trim();
181 if (parts[p].indexOf("text/html") >= 0) {
182 pay.html = parts[p].split("\n").slice(1,-2).join("\n").trim();
189 msg.on('end', function() {
190 //node.log('Finished: '+prefix);
193 f.on('error', function(err) {
194 node.warn('fetch error: ' + err);
195 node.status({fill:"red",shape:"ring",text:"fetch error"});
197 f.on('end', function() {
198 if (JSON.stringify(pay) !== oldmail) {
200 oldmail = JSON.stringify(pay);
201 node.log('received new email: '+pay.topic);
203 else { node.log('duplicate not sent: '+pay.topic); }
204 //node.status({fill:"green",shape:"dot",text:"ok"});
210 node.log("you have achieved inbox zero");
211 //node.status({fill:"green",shape:"dot",text:"ok"});
217 node.status({fill:"grey",shape:"dot",text:"connecting"});
221 imap.on('error', function(err) {
223 node.status({fill:"red",shape:"ring",text:"connect error"});
226 this.on("error", function(err) {
227 node.log("error: ",err);
230 this.on("close", function() {
231 if (this.interval_id != null) {
232 clearInterval(this.interval_id);
234 if (imap) { imap.destroy(); }
237 node.emit("input",{});
239 RED.nodes.registerType("e-mail in",EmailInNode,{
241 userid: {type:"text"},
242 password: {type: "password"},
243 global: { type:"boolean"}