Javascript: parsing emails into json/building json file from text

Viewed 14

I have a self-made email parser that pulls email body's that match a specific subject and writes each email to individual text files in a folder, using fs:

module.exports = {
    run: function(){
        var Imap = require('imap'),
            inspect = require('util').inspect;
        var fs = require('fs'), fileStream;
        var logger = require('./logger.js');
        var buffer = '';

        var imap = new Imap({
            user: "tracker@email.com",
            password: "xxxxxxxx",
            host: "host.secureserver.net",
            port: 993,
            tls: true,
            connTimeout: 10000,
            authTimeout: 5000,
            debug: console.log,
            tlsOptions: { rejectUnauthorized: true },
            mailbox: "INBOX",
            searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved 
            markSeen: false, // all fetched email willbe marked as seen and not fetched next time 
            fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`, 
            mailParserOptions: { streamAttachments: false }, // options to be passed to mailParser lib. 
            attachments: false, // download attachments as they are encountered to the project directory 
            attachmentOptions: { directory: "attachments/" } 
        });

        function openInbox(cb){
            imap.openBox('Inbox', false, cb);
        }

        imap.once('ready', function(){
            logger.printWriteLine('Parsing inbox for new error alerts...', 1);
            openInbox(function (err, box){
                if(err) throw err;
                    imap.search(
                        ['UNSEEN', ['SUBJECT', 'Error Alerts']], 
                        function(err, results){
                        if(err) throw err;
                    else if(!results || !results.length){
                        logger.printWriteLine('No new emails', 2);
                    }
                    else{
                        var f = imap.fetch(results, {bodies: '1', markSeen: true});
                        f.on('message', function(msg, seqno){
                            logger.printWriteLine('message #:'+seqno, 1);
                            logger.printWriteLine('message type: '+msg.txt, 1);
                            var prefix = '(#' + seqno + ') ';
                            msg.on('body', function (stream, info){
                                stream.on('data', function(chunk){
                                    buffer += chunk.toString('utf8');
                                    //console.log('Buffer: '+buffer);
                                })
                                stream.once('end', function(){
                                    if(info.which === '1'){
                                        //console.log('Buffer 2: ' + buffer);
                                    }
                                });
                                stream.pipe(fs.createWriteStream('./mailParser/'+ seqno + '-body.txt'));
                            });
                            msg.once('end', function () {
                                logger.printWriteLine(prefix + ' - End of message.', 1);
                            });
                        });
                        f.once('error', function (err) {
                            console.log('Fetch error: ' + err);
                        });
                        f.once('end', function () {
                            logger.printWriteLine('Done fetching messages.', 1);
                            imap.end();
                        });

                    }
                });
            });
        });
        imap.once('error', function (err) {
        console.log(err);
        });
        
        imap.once('end', function () {
        console.log('Connection ended');
        });

        imap.connect();
    }
}

The resulting text files are each formatted like this:

Vehicle ID720
                        DRIDT0MA12200330
                        
                        
                        Event TypeMediaError  - SD1,Protected
                        Event Local Time2022-09-20 16:54:18
                        Event Time GMT2022-09-20 20:54:18
                        URLView Event
                        More DataSD1,Protected
                        Vehicle Registration
                        Vehicle VIN
                        Vehicle Manufacturer
                        Vehicle Model
                        Vehicle FuelColorTransmissionPolicy HolderPolicy NumberCustom 1Custom 2

My goal is to parse/pull relevant data from these emails, and build the details into a json file. Right now I'm stuck trying to loop through each text file, parse/pull the relevant details with regex/"simple-text-parser", and build into a json file to be consumed by other parts of my program.

goal output would be something like this:
{
"vehicle1": {
          "number": "720", 
          "DRID": "T0MA12200330",
          "Type": "Media Error - SD1,Protected"
          "Time": "2022-09-20 16:54:18"
           },
"vehicle2: {
          ...
           }
}
      

Obviously this is an inefficient, round-about way to accomplish this (with the text files). I'm wondering if anybody can suggest a better way to parse the emails from my mail parser, so perhaps I can go straight to JSON, or any alternate methods one might use to accomplish what I'm trying to do.

Please understand I'm not a professional dev so my code may be pretty rough.

0 Answers
Related