mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Add new/updated Email node including credentials per node.
Deprecating old IMAP node as now part of this general Email node
This commit is contained in:
		@@ -31,13 +31,13 @@
 | 
			
		||||
       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>This <b>must</b> be located in the directory <b>above</b> 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">
 | 
			
		||||
    RED.nodes.registerType('imap',{
 | 
			
		||||
        category: 'social-input',
 | 
			
		||||
        category: 'deprecated',
 | 
			
		||||
        color:"#c7e9c0",
 | 
			
		||||
        defaults: {
 | 
			
		||||
            repeat: {value:"300",required:true},
 | 
			
		||||
							
								
								
									
										138
									
								
								nodes/core/deprecated/61-imap.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										138
									
								
								nodes/core/deprecated/61-imap.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,138 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright 2013 IBM Corp.
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 * http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 **/
 | 
			
		||||
 | 
			
		||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
 | 
			
		||||
var Imap = require('imap');
 | 
			
		||||
var util = require('util');
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    var emailkey = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
 | 
			
		||||
} catch (err) {
 | 
			
		||||
    util.log("[61-imap.js] Advise : No Email credentials found.");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if (emailkey) {
 | 
			
		||||
    var imap = new Imap({
 | 
			
		||||
        user: emailkey.user,
 | 
			
		||||
        password: emailkey.pass,
 | 
			
		||||
        host: emailkey.server||"imap.gmail.com",
 | 
			
		||||
        port: emailkey.port||"993",
 | 
			
		||||
        tls: true,
 | 
			
		||||
        tlsOptions: { rejectUnauthorized: false }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    function openInbox(cb) {
 | 
			
		||||
        imap.openBox('INBOX', true, cb);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function ImapNode(n) {
 | 
			
		||||
    RED.nodes.createNode(this,n);
 | 
			
		||||
    this.name = n.name;
 | 
			
		||||
    this.repeat = n.repeat * 1000 || 300000;
 | 
			
		||||
    var node = this;
 | 
			
		||||
    this.interval_id = null;
 | 
			
		||||
    var oldmail = {};
 | 
			
		||||
 | 
			
		||||
    if (!isNaN(this.repeat) && this.repeat > 0) {
 | 
			
		||||
        node.log("repeat = "+this.repeat);
 | 
			
		||||
        this.interval_id = setInterval( function() {
 | 
			
		||||
            node.emit("input",{});
 | 
			
		||||
        }, this.repeat );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.on("input", function(msg) {
 | 
			
		||||
        if (imap) {
 | 
			
		||||
            imap.once('ready', function() {
 | 
			
		||||
                var pay = {};
 | 
			
		||||
                openInbox(function(err, box) {
 | 
			
		||||
                    if (box.messages.total > 0) {
 | 
			
		||||
                        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);
 | 
			
		||||
                            });
 | 
			
		||||
                        });
 | 
			
		||||
                        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);
 | 
			
		||||
                                node.log('sent new message: '+pay.topic);
 | 
			
		||||
                            }
 | 
			
		||||
                            else { node.log('duplicate not sent: '+pay.topic); }
 | 
			
		||||
                            imap.end();
 | 
			
		||||
                        });
 | 
			
		||||
                    }
 | 
			
		||||
                    else {
 | 
			
		||||
                        // node.log("you have achieved inbox zero");
 | 
			
		||||
                        imap.end();
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
            });
 | 
			
		||||
            imap.connect();
 | 
			
		||||
        }
 | 
			
		||||
        else { node.warn("No Email credentials found. See info panel."); }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (imap) {
 | 
			
		||||
        imap.on('error', function(err) {
 | 
			
		||||
            util.log(err);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.on("error", function(err) {
 | 
			
		||||
        node.log("error: ",err);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    this.on("close", function() {
 | 
			
		||||
        if (this.interval_id != null) {
 | 
			
		||||
            clearInterval(this.interval_id);
 | 
			
		||||
        }
 | 
			
		||||
        if (imap) { imap.destroy(); }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    node.emit("input",{});
 | 
			
		||||
}
 | 
			
		||||
RED.nodes.registerType("imap",ImapNode);
 | 
			
		||||
@@ -14,38 +14,223 @@
 | 
			
		||||
  limitations under the License.
 | 
			
		||||
-->
 | 
			
		||||
 | 
			
		||||
<script type="text/x-red" data-template-name="email">
 | 
			
		||||
<script type="text/x-red" data-template-name="e-mail">
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-input-name"><i class="icon-envelope"></i> To</label>
 | 
			
		||||
        <input type="text" id="node-input-name" placeholder="email@address.com">
 | 
			
		||||
    </div>
 | 
			
		||||
    <!-- <div class="form-row">
 | 
			
		||||
        <label for="node-input-pin"><i class="icon-asterisk"></i> Service</label>
 | 
			
		||||
        <select type="text" id="node-input-pin" style="width: 150px;">
 | 
			
		||||
            <option value="-" disabled> </option>
 | 
			
		||||
            <option value="DynectEmail">DynectEmail</option>
 | 
			
		||||
            <option value="Gmail">Gmail</option>
 | 
			
		||||
            <option value="hot.ee">hot.ee</option>
 | 
			
		||||
            <option value="Hotmail">Hotmail</option>
 | 
			
		||||
            <option value="iCloud">iCloud</option>
 | 
			
		||||
            <option value="mail.ee">mail.ee</option>
 | 
			
		||||
            <option value="Mail.Ru">Mail.Ru</option>
 | 
			
		||||
            <option value="Mailgun">Mailgun</option>
 | 
			
		||||
            <option value="Mailjet">Mailjet</option>
 | 
			
		||||
            <option value="Mandrill">Mandrill</option>
 | 
			
		||||
            <option value="Postmark">Postmark</option>
 | 
			
		||||
            <option value="QQ">QQ</option>
 | 
			
		||||
            <option value="QQex">QQex</option>
 | 
			
		||||
            <option value="SendGrid">SendGrid</option>
 | 
			
		||||
            <option value="SendCloud">SendCloud</option>
 | 
			
		||||
            <option value="SES">SES</option>
 | 
			
		||||
            <option value="Yahoo">Yahoo</option>
 | 
			
		||||
            <option value="yandex">yandex</option>
 | 
			
		||||
            <option value="Zoho">Zoho</option>
 | 
			
		||||
         </select>
 | 
			
		||||
    </div> -->
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-input-server"><i class="icon-tag"></i> Server</label>
 | 
			
		||||
        <input type="text" id="node-input-server" placeholder="smtp.gmail.com">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-input-port"><i class="icon-random"></i> Port</label>
 | 
			
		||||
        <input type="text" id="node-input-port" placeholder="465">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-config-input-userid"><i class="icon-user"></i> Userid</label>
 | 
			
		||||
        <input type="text" id="node-config-input-userid">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
 | 
			
		||||
        <input type="password" id="node-config-input-password">
 | 
			
		||||
    </div>
 | 
			
		||||
    <br/>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-input-dname"><i class="icon-tag"></i> Name</label>
 | 
			
		||||
        <input type="text" id="node-input-dname" placeholder="Name">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-tips" id="node-tip"><b>Note:</b> Using credentials from global emailkeys.js file.</div>
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<script type="text/x-red" data-help-name="email">
 | 
			
		||||
	<p>Sends the <b>msg.payload</b> as an email, with a subject of <b>msg.topic</b>.</p>
 | 
			
		||||
	<p>It sends the message to the configured recipient <i>only</i>.</p>
 | 
			
		||||
	<p><b>msg.topic</b> is used to set the subject of the email, and <b>msg.payload</b> is the body text.</p>
 | 
			
		||||
	<p>Uses the nodemailer module - you also need to pre-configure your email SMTP settings in a file emailkeys.js like that 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>
 | 
			
		||||
<script type="text/x-red" data-help-name="e-mail">
 | 
			
		||||
    <p>Sends the <b>msg.payload</b> as an email, with a subject of <b>msg.topic</b>.</p>
 | 
			
		||||
    <p>It sends the message to the configured recipient <i>only</i>.</p>
 | 
			
		||||
    <p><b>msg.topic</b> is used to set the subject of the email, and <b>msg.payload</b> is the body text.</p>
 | 
			
		||||
    <p>Uses the nodemailer module.</p>
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<script type="text/javascript">
 | 
			
		||||
    RED.nodes.registerType('email',{
 | 
			
		||||
(function() {
 | 
			
		||||
    RED.nodes.registerType('e-mail',{
 | 
			
		||||
        category: 'social-output',
 | 
			
		||||
        color:"#c7e9c0",
 | 
			
		||||
        defaults: {
 | 
			
		||||
            name: {value:"",required:true}
 | 
			
		||||
            server: {value:"smtp.gmail.com",required:true},
 | 
			
		||||
            port: {value:"465",required:true},
 | 
			
		||||
            name: {value:"",required:true},
 | 
			
		||||
            dname: {value:""}
 | 
			
		||||
        },
 | 
			
		||||
        inputs:1,
 | 
			
		||||
        outputs:0,
 | 
			
		||||
        icon: "envelope.png",
 | 
			
		||||
        align: "right",
 | 
			
		||||
        label: function() {
 | 
			
		||||
            return this.name;
 | 
			
		||||
            return this.dname||this.name||"email";
 | 
			
		||||
        },
 | 
			
		||||
        labelStyle: function() {
 | 
			
		||||
            return (this.dname||!this.topic)?"node_label_italic":"";
 | 
			
		||||
        },
 | 
			
		||||
        oneditprepare: function() {
 | 
			
		||||
            $.getJSON('email/'+this.id,function(data) {
 | 
			
		||||
                if (data.userid) {
 | 
			
		||||
                    $('#node-config-input-userid').val(data.userid);
 | 
			
		||||
                }
 | 
			
		||||
                if (data.hasPassword) {
 | 
			
		||||
                    $('#node-config-input-password').val('__PWRD__');
 | 
			
		||||
                } else {
 | 
			
		||||
                    $('#node-config-input-password').val('');
 | 
			
		||||
                }
 | 
			
		||||
                if (data.global) $('#node-tip').show();
 | 
			
		||||
                else $('#node-tip').hide();
 | 
			
		||||
            });
 | 
			
		||||
        },
 | 
			
		||||
        oneditsave: function() {
 | 
			
		||||
            var credentials = {};
 | 
			
		||||
            var newUser = $('#node-config-input-userid').val();
 | 
			
		||||
            var newPass = $('#node-config-input-password').val();
 | 
			
		||||
            credentials.userid = newUser;
 | 
			
		||||
            if (newPass != '__PWRD__') {
 | 
			
		||||
                credentials.password = newPass;
 | 
			
		||||
            }
 | 
			
		||||
            $.ajax({
 | 
			
		||||
                url: 'email/'+this.id,
 | 
			
		||||
                type: 'POST',
 | 
			
		||||
                data: credentials,
 | 
			
		||||
                success: function(result){}
 | 
			
		||||
            });
 | 
			
		||||
        },
 | 
			
		||||
        ondelete: function() {
 | 
			
		||||
            $.ajax({
 | 
			
		||||
                url: 'email/'+this.id,
 | 
			
		||||
                type: 'DELETE',
 | 
			
		||||
                success: function(result) {}
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
})();
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
<script type="text/x-red" data-template-name="e-mail in">
 | 
			
		||||
    <div class="form-row node-input-repeat">
 | 
			
		||||
        <label for="node-input-repeat"><i class="icon-repeat"></i> Check Repeat (S)</label>
 | 
			
		||||
        <input type="text" id="node-input-repeat" placeholder="300">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-input-server"><i class="icon-tag"></i> Server</label>
 | 
			
		||||
        <input type="text" id="node-input-server" placeholder="imap.gmail.com">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-input-port"><i class="icon-random"></i> Port</label>
 | 
			
		||||
        <input type="text" id="node-input-port" placeholder="993">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-config-input-userid"><i class="icon-user"></i> Userid</label>
 | 
			
		||||
        <input type="text" id="node-config-input-userid">
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
        <label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
 | 
			
		||||
        <input type="password" id="node-config-input-password">
 | 
			
		||||
    </div>
 | 
			
		||||
    <br/>
 | 
			
		||||
    <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-tips" id="node-tip"><b>Note:</b> Using credentials from global emailkeys.js file.</div>
 | 
			
		||||
    <div id="node-input-tip" class="form-tips">Tip: <b>ONLY</b> retrieves the single most recent email.</div>
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<script type="text/x-red" data-help-name="e-mail in">
 | 
			
		||||
    <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.</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">
 | 
			
		||||
(function() {
 | 
			
		||||
    RED.nodes.registerType('e-mail in',{
 | 
			
		||||
        category: 'social-input',
 | 
			
		||||
        color:"#c7e9c0",
 | 
			
		||||
        defaults: {
 | 
			
		||||
            repeat: {value:"300",required:true},
 | 
			
		||||
            server: {value:"imap.gmail.com",required:true},
 | 
			
		||||
            port: {value:"993",required:true},
 | 
			
		||||
            name: {value:""}
 | 
			
		||||
        },
 | 
			
		||||
        inputs:0,
 | 
			
		||||
        outputs:1,
 | 
			
		||||
        icon: "envelope.png",
 | 
			
		||||
        label: function() {
 | 
			
		||||
            return this.name||"email";
 | 
			
		||||
        },
 | 
			
		||||
        labelStyle: function() {
 | 
			
		||||
            return (this.name||!this.topic)?"node_label_italic":"";
 | 
			
		||||
        },
 | 
			
		||||
        oneditprepare: function() {
 | 
			
		||||
            $.getJSON('email/'+this.id,function(data) {
 | 
			
		||||
                if (data.userid) {
 | 
			
		||||
                    $('#node-config-input-userid').val(data.userid);
 | 
			
		||||
                }
 | 
			
		||||
                if (data.hasPassword) {
 | 
			
		||||
                    $('#node-config-input-password').val('__PWRD__');
 | 
			
		||||
                } else {
 | 
			
		||||
                    $('#node-config-input-password').val('');
 | 
			
		||||
                }
 | 
			
		||||
                if (data.global) $('#node-tip').show();
 | 
			
		||||
                else $('#node-tip').hide();
 | 
			
		||||
            });
 | 
			
		||||
        },
 | 
			
		||||
        oneditsave: function() {
 | 
			
		||||
            var credentials = {};
 | 
			
		||||
            var newUser = $('#node-config-input-userid').val();
 | 
			
		||||
            var newPass = $('#node-config-input-password').val();
 | 
			
		||||
            credentials.userid = newUser;
 | 
			
		||||
            if (newPass != '__PWRD__') {
 | 
			
		||||
                credentials.password = newPass;
 | 
			
		||||
            }
 | 
			
		||||
            $.ajax({
 | 
			
		||||
                url: 'email/'+this.id,
 | 
			
		||||
                type: 'POST',
 | 
			
		||||
                data: credentials,
 | 
			
		||||
                success: function(result){}
 | 
			
		||||
            });
 | 
			
		||||
        },
 | 
			
		||||
        ondelete: function() {
 | 
			
		||||
            $.ajax({
 | 
			
		||||
                url: 'email/'+this.id,
 | 
			
		||||
                type: 'DELETE',
 | 
			
		||||
                success: function(result) {}
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
})();
 | 
			
		||||
</script>
 | 
			
		||||
 
 | 
			
		||||
@@ -15,41 +15,240 @@
 | 
			
		||||
 **/
 | 
			
		||||
 | 
			
		||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
 | 
			
		||||
var util = require('util');
 | 
			
		||||
var nodemailer = require("nodemailer");
 | 
			
		||||
var emailkey = require(process.env.NODE_RED_HOME+"/../emailkeys.js");
 | 
			
		||||
var Imap = require('imap');
 | 
			
		||||
 | 
			
		||||
var smtpTransport = nodemailer.createTransport("SMTP",{
 | 
			
		||||
	service: emailkey.service,
 | 
			
		||||
	auth: {
 | 
			
		||||
		user: emailkey.user,
 | 
			
		||||
		pass: emailkey.pass
 | 
			
		||||
	}
 | 
			
		||||
});
 | 
			
		||||
//console.log(nodemailer.Transport.transports.SMTP.wellKnownHosts);
 | 
			
		||||
 | 
			
		||||
// module.exports = { service: "Gmail", user: "blahblah@gmail.com", pass: "password", server: "imap.gmail.com", port: "993" }
 | 
			
		||||
try { var globalkeys = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js"); }
 | 
			
		||||
catch(err) { }
 | 
			
		||||
 | 
			
		||||
function EmailNode(n) {
 | 
			
		||||
	RED.nodes.createNode(this,n);
 | 
			
		||||
	this.topic = n.topic;
 | 
			
		||||
	this.name = n.name;
 | 
			
		||||
	var node = this;
 | 
			
		||||
	this.on("input", function(msg) {
 | 
			
		||||
		//node.log("email :",this.id,this.topic," received",msg.payload);
 | 
			
		||||
		if (msg != null) {
 | 
			
		||||
    RED.nodes.createNode(this,n);
 | 
			
		||||
    this.topic = n.topic;
 | 
			
		||||
    this.name = n.name;
 | 
			
		||||
    this.outserver = n.server;
 | 
			
		||||
    this.outport = n.port;
 | 
			
		||||
    var flag = false;
 | 
			
		||||
    var credentials = RED.nodes.getCredentials(n.id);
 | 
			
		||||
    if ((credentials) && (credentials.hasOwnProperty("userid"))) { this.userid = credentials.userid; }
 | 
			
		||||
    else {
 | 
			
		||||
        if (globalkeys) { this.userid = globalkeys.user; flag = true; }
 | 
			
		||||
        else { this.error("No e-mail userid set"); }
 | 
			
		||||
    }
 | 
			
		||||
    if ((credentials) && (credentials.hasOwnProperty("password"))) { this.password = credentials.password; }
 | 
			
		||||
    else {
 | 
			
		||||
        if (globalkeys) { this.password = globalkeys.pass; flag = true; }
 | 
			
		||||
        else { this.error("No e-mail password set"); }
 | 
			
		||||
    }
 | 
			
		||||
    if (flag) { RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true}); }
 | 
			
		||||
    var node = this;
 | 
			
		||||
 | 
			
		||||
			smtpTransport.sendMail({
 | 
			
		||||
				from: emailkey.user, // sender address
 | 
			
		||||
				to: node.name, // comma separated list of receivers
 | 
			
		||||
				subject: msg.topic, // Subject line
 | 
			
		||||
				text: msg.payload // plaintext body
 | 
			
		||||
			}, function(error, response) {
 | 
			
		||||
				if (error) {
 | 
			
		||||
					node.error(error);
 | 
			
		||||
				} else {
 | 
			
		||||
					node.log("Message sent: " + response.message);
 | 
			
		||||
				}
 | 
			
		||||
			});
 | 
			
		||||
    var smtpTransport = nodemailer.createTransport("SMTP",{
 | 
			
		||||
        //service: emailkey.service,
 | 
			
		||||
        // {
 | 
			
		||||
        //transport: 'SMTP',
 | 
			
		||||
        host: node.outserver,
 | 
			
		||||
        port: node.outport,
 | 
			
		||||
        requiresAuth: true,
 | 
			
		||||
        secureConnection: true,
 | 
			
		||||
        //domains: [ 'gmail.com', 'googlemail.com' ],
 | 
			
		||||
        //},
 | 
			
		||||
        auth: {
 | 
			
		||||
            user: node.userid,
 | 
			
		||||
            pass: node.password
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	});
 | 
			
		||||
    this.on("input", function(msg) {
 | 
			
		||||
        //node.log("email :",this.id,this.topic," received",msg.payload);
 | 
			
		||||
        if (msg != null) {
 | 
			
		||||
            if (smtpTransport) {
 | 
			
		||||
                smtpTransport.sendMail({
 | 
			
		||||
                    from: node.userid, // sender address
 | 
			
		||||
                    to: node.name, // comma separated list of receivers
 | 
			
		||||
                    subject: msg.topic, // subject line
 | 
			
		||||
                    text: msg.payload // plaintext body
 | 
			
		||||
                }, function(error, response) {
 | 
			
		||||
                    if (error) {
 | 
			
		||||
                        node.error(error);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        node.log("Message sent: " + response.message);
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
            else { node.warn("No Email credentials found. See info panel."); }
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
RED.nodes.registerType("e-mail",EmailNode);
 | 
			
		||||
 | 
			
		||||
RED.nodes.registerType("email",EmailNode);
 | 
			
		||||
 | 
			
		||||
function EmailInNode(n) {
 | 
			
		||||
    RED.nodes.createNode(this,n);
 | 
			
		||||
    this.name = n.name;
 | 
			
		||||
    this.repeat = n.repeat * 1000 || 300000;
 | 
			
		||||
    this.inserver = n.server || emailkey.server || "imap.gmail.com";
 | 
			
		||||
    this.inport = n.port || emailkey.port || "993";
 | 
			
		||||
    var flag = false;
 | 
			
		||||
    var credentials = RED.nodes.getCredentials(n.id);
 | 
			
		||||
    if ((credentials) && (credentials.hasOwnProperty("userid"))) { this.userid = credentials.userid; }
 | 
			
		||||
    else {
 | 
			
		||||
        if (globalkeys) { this.userid = globalkeys.user; flag = true; }
 | 
			
		||||
        else { this.error("No e-mail userid set"); }
 | 
			
		||||
    }
 | 
			
		||||
    if ((credentials) && (credentials.hasOwnProperty("password"))) { this.password = credentials.password; }
 | 
			
		||||
    else {
 | 
			
		||||
        if (globalkeys) { this.password = globalkeys.pass; flag = true; }
 | 
			
		||||
        else { this.error("No e-mail password set"); }
 | 
			
		||||
    }
 | 
			
		||||
    if (flag) { RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true}); }
 | 
			
		||||
    var node = this;
 | 
			
		||||
    this.interval_id = null;
 | 
			
		||||
    var oldmail = {};
 | 
			
		||||
 | 
			
		||||
    var imap = new Imap({
 | 
			
		||||
        user: node.userid,
 | 
			
		||||
        password: node.password,
 | 
			
		||||
        host: node.inserver,
 | 
			
		||||
        port: node.inport,
 | 
			
		||||
        tls: true,
 | 
			
		||||
        tlsOptions: { rejectUnauthorized: false },
 | 
			
		||||
        connTimeout: node.repeat,
 | 
			
		||||
        authTimeout: node.repeat
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (!isNaN(this.repeat) && this.repeat > 0) {
 | 
			
		||||
        node.log("repeat = "+this.repeat);
 | 
			
		||||
        this.interval_id = setInterval( function() {
 | 
			
		||||
            node.emit("input",{});
 | 
			
		||||
        }, this.repeat );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.on("input", function(msg) {
 | 
			
		||||
        imap.once('ready', function() {
 | 
			
		||||
            var pay = {};
 | 
			
		||||
            imap.openBox('INBOX', true, function(err, box) {
 | 
			
		||||
                if (box.messages.total > 0) {
 | 
			
		||||
                    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);
 | 
			
		||||
                        });
 | 
			
		||||
                    });
 | 
			
		||||
                    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);
 | 
			
		||||
                            node.log('received new email: '+pay.topic);
 | 
			
		||||
                        }
 | 
			
		||||
                        else { node.log('duplicate not sent: '+pay.topic); }
 | 
			
		||||
                        imap.end();
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    node.log("you have achieved inbox zero");
 | 
			
		||||
                    imap.end();
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
        imap.on('error', function(err) {
 | 
			
		||||
            node.log(err);
 | 
			
		||||
        });
 | 
			
		||||
        imap.connect();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    this.on("error", function(err) {
 | 
			
		||||
        node.log("error: ",err);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    this.on("close", function() {
 | 
			
		||||
        if (this.interval_id != null) {
 | 
			
		||||
            clearInterval(this.interval_id);
 | 
			
		||||
        }
 | 
			
		||||
        if (imap) { imap.destroy(); }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    node.emit("input",{});
 | 
			
		||||
}
 | 
			
		||||
RED.nodes.registerType("e-mail in",EmailInNode);
 | 
			
		||||
 | 
			
		||||
var querystring = require('querystring');
 | 
			
		||||
 | 
			
		||||
RED.httpAdmin.get('/email/global',function(req,res) {
 | 
			
		||||
    res.send(JSON.stringify({hasToken:!(globalkeys && globalkeys.userid && globalkeys.password)}));
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
RED.httpAdmin.get('/email/:id',function(req,res) {
 | 
			
		||||
    var credentials = RED.nodes.getCredentials(req.params.id);
 | 
			
		||||
    if (credentials) {
 | 
			
		||||
        res.send(JSON.stringify({userid:credentials.userid,hasPassword:(credentials.password&&credentials.password!="")}));
 | 
			
		||||
    }
 | 
			
		||||
    else if (globalkeys && globalkeys.user && globalkeys.pass) {
 | 
			
		||||
        RED.nodes.addCredentials(req.params.id,{userid:globalkeys.user, password:globalkeys.pass, global:true});
 | 
			
		||||
        credentials = RED.nodes.getCredentials(req.params.id);
 | 
			
		||||
        res.send(JSON.stringify({userid:credentials.userid,global:credentials.global,hasPassword:(credentials.password&&credentials.password!="")}));
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        res.send(JSON.stringify({}));
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
RED.httpAdmin.delete('/email/:id',function(req,res) {
 | 
			
		||||
    RED.nodes.deleteCredentials(req.params.id);
 | 
			
		||||
    res.send(200);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
RED.httpAdmin.post('/email/:id',function(req,res) {
 | 
			
		||||
    var body = "";
 | 
			
		||||
    req.on('data', function(chunk) {
 | 
			
		||||
        body+=chunk;
 | 
			
		||||
    });
 | 
			
		||||
    req.on('end', function(){
 | 
			
		||||
        var newCreds = querystring.parse(body);
 | 
			
		||||
        var credentials = RED.nodes.getCredentials(req.params.id)||{};
 | 
			
		||||
        if (newCreds.userid == null || newCreds.userid == "") {
 | 
			
		||||
            delete credentials.userid;
 | 
			
		||||
        } else {
 | 
			
		||||
            credentials.userid = newCreds.userid;
 | 
			
		||||
        }
 | 
			
		||||
        if (newCreds.password == "") {
 | 
			
		||||
            delete credentials.password;
 | 
			
		||||
        } else {
 | 
			
		||||
            credentials.password = newCreds.password||credentials.password;
 | 
			
		||||
        }
 | 
			
		||||
        RED.nodes.addCredentials(req.params.id,credentials);
 | 
			
		||||
        res.send(200);
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -1,132 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright 2013 IBM Corp.
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 * http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 **/
 | 
			
		||||
 | 
			
		||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
 | 
			
		||||
var Imap = require('imap');
 | 
			
		||||
var util = require('util');
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
    var emailkey = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
 | 
			
		||||
} catch (err) {
 | 
			
		||||
    util.log("[imap] : Failed to load Email credentials");
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var imap = new Imap({
 | 
			
		||||
    user: emailkey.user,
 | 
			
		||||
    password: emailkey.pass,
 | 
			
		||||
    host: emailkey.server||"imap.gmail.com",
 | 
			
		||||
    port: emailkey.port||"993",
 | 
			
		||||
    tls: true,
 | 
			
		||||
    tlsOptions: { rejectUnauthorized: false }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
function openInbox(cb) {
 | 
			
		||||
    imap.openBox('INBOX', true, cb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function ImapNode(n) {
 | 
			
		||||
    RED.nodes.createNode(this,n);
 | 
			
		||||
    this.name = n.name;
 | 
			
		||||
    this.repeat = n.repeat * 1000 || 300000;
 | 
			
		||||
    var node = this;
 | 
			
		||||
    this.interval_id = null;
 | 
			
		||||
    var oldmail = {};
 | 
			
		||||
 | 
			
		||||
    if (!isNaN(this.repeat) && this.repeat > 0) {
 | 
			
		||||
        node.log("repeat = "+this.repeat);
 | 
			
		||||
        this.interval_id = setInterval( function() {
 | 
			
		||||
            node.emit("input",{});
 | 
			
		||||
        }, this.repeat );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.on("input", function(msg) {
 | 
			
		||||
        imap.once('ready', function() {
 | 
			
		||||
            var pay = {};
 | 
			
		||||
            openInbox(function(err, box) {
 | 
			
		||||
                if (box.messages.total > 0) {
 | 
			
		||||
                    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);
 | 
			
		||||
                        });
 | 
			
		||||
                    });
 | 
			
		||||
                    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);
 | 
			
		||||
                            node.log('sent new message: '+pay.topic);
 | 
			
		||||
                        }
 | 
			
		||||
                        else { node.log('duplicate not sent: '+pay.topic); }
 | 
			
		||||
                        imap.end();
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    // node.log("you have achieved inbox zero");
 | 
			
		||||
                    imap.end();
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
        imap.connect();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    imap.on('error', function(err) {
 | 
			
		||||
        util.log(err);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    this.on("error", function(err) {
 | 
			
		||||
        node.log("error: ",err);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    this.on("close", function() {
 | 
			
		||||
        if (this.interval_id != null) {
 | 
			
		||||
            clearInterval(this.interval_id);
 | 
			
		||||
        }
 | 
			
		||||
        imap.destroy();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    node.emit("input",{});
 | 
			
		||||
}
 | 
			
		||||
RED.nodes.registerType("imap",ImapNode);
 | 
			
		||||
		Reference in New Issue
	
	Block a user