Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

install emailjs npm

npm install emailjs-com --save
Comment

npm read email

var Imap = require("imap");
var MailParser = require("mailparser").MailParser;
var Promise = require("bluebird");
Promise.longStackTraces();

var imapConfig = {
    user: 'USERNAME',
    password: 'PASSWORD',
    host: 'HOST',
    port: 993,
    tls: true
};

var imap = new Imap(imapConfig);
Promise.promisifyAll(imap);

imap.once("ready", execute);
imap.once("error", function(err) {
    log.error("Connection error: " + err.stack);
});

imap.connect();

function execute() {
    imap.openBox("INBOX", false, function(err, mailBox) {
        if (err) {
            console.error(err);
            return;
        }
        imap.search(["UNSEEN"], function(err, results) {
            if(!results || !results.length){console.log("No unread mails");imap.end();return;}
            /* mark as seen
            imap.setFlags(results, ['Seen'], function(err) {
                if (!err) {
                    console.log("marked as read");
                } else {
                    console.log(JSON.stringify(err, null, 2));
                }
            });*/

            var f = imap.fetch(results, { bodies: "" });
            f.on("message", processMessage);
            f.once("error", function(err) {
                return Promise.reject(err);
            });
            f.once("end", function() {
                console.log("Done fetching all unseen messages.");
                imap.end();
            });
        });
    });
}


function processMessage(msg, seqno) {
    console.log("Processing msg #" + seqno);
    // console.log(msg);

    var parser = new MailParser();
    parser.on("headers", function(headers) {
        console.log("Header: " + JSON.stringify(headers));
    });

    parser.on('data', data => {
        if (data.type === 'text') {
            console.log(seqno);
            console.log(data.text);  /* data.html*/
        }

        // if (data.type === 'attachment') {
        //     console.log(data.filename);
        //     data.content.pipe(process.stdout);
        //     // data.content.on('end', () => data.release());
        // }
     });

    msg.on("body", function(stream) {
        stream.on("data", function(chunk) {
            parser.write(chunk.toString("utf8"));
        });
    });
    msg.once("end", function() {
        // console.log("Finished msg #" + seqno);
        parser.end();
    });
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: useselector 
Javascript :: javascript this = that 
Javascript :: if else javascript 
Javascript :: convert html to javascript 
Javascript :: how to use array of object in react 
Javascript :: find an object in an array by one of its properties 
Javascript :: printing in javascript 
Javascript :: javascript add onclick to multiple elements 
Javascript :: how to turn a string into an array javascript 
Javascript :: how to do subtraction in javascript 
Javascript :: react variable in stirng 
Javascript :: interface in javascript 
Javascript :: mongoose update array push multiple 
Javascript :: .remove javascript 
Javascript :: how to unfreeze object in javascript 
Javascript :: sub function javascript 
Javascript :: array example 
Javascript :: set element at index javascript array and create new array 
Javascript :: ajax form submit 
Javascript :: update text react native 
Javascript :: remove item from array 
Javascript :: remove element from object javascript 
Javascript :: where to create service angularor nodejs 
Javascript :: electron install 
Javascript :: javascript get image data from clipboard 
Javascript :: how to get font size in javascript 
Javascript :: use of parse in react 
Javascript :: if condition to whether json object has jsonarray or jsonobject 
Javascript :: delete JSON properties in place with jq 
Javascript :: windows 10 retiré le theme sombre explorateur 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =