Last active
May 14, 2022 10:55
-
-
Save harish2704/273f910610d28e2960ca6ec89ef93b04 to your computer and use it in GitHub Desktop.
Revisions
-
harish2704 revised this gist
May 14, 2022 . 1 changed file with 25 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,32 +1,52 @@ #!/usr/bin/env node const fs = require('fs'); const path = require('path'); const mailDir = path.join( __dirname, '..', '..', 'test-mails' ); const {stdin} = process; async function getStdin() { let result = ''; if (stdin.isTTY) { return result; } stdin.setEncoding('utf8'); for await (const chunk of stdin) { result += chunk; } return result; } if( !fs.existsSync( mailDir ) ){ fs.mkdirSync( mailDir ); } function parseMail( rawStr ){ str = rawStr.split('\n\n'); const headers = str[ 0 ]; const body = str.slice(1).join('\n\n'); const headersObj = {}; headers.split('\n').forEach(function(v){ v = v.split(': '); headersObj[ v[ 0 ] ] = v[ 1 ]; }); headersObj.body = body; fs.writeFileSync( path.join( mailDir, headersObj[ 'Message-ID' ] ) + '.eml', rawStr ); return headersObj; } function main(){ getStdin() .then( parseMail ) .then( function( mailJson ){ // fs.writeFileSync( path.join( mailDir, mailJson[ 'Message-ID' ] ) + '.json', JSON.stringify(mailJson, null, 2) ); }); } -
harish2704 created this gist
Jun 30, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ #!/usr/bin/env node const getStdin = require('get-stdin'); const fs = require('fs'); const path = require('path'); const mailDir = path.join( __dirname, '..', '..', 'test-mails' ); if( !fs.existsSync( mailDir ) ){ fs.mkdirSync( mailDir ); } function parseMail( str ){ str = str.split('\n\n'); const headers = str[ 0 ]; const body = str[ 1 ]; const headersObj = {}; headers.split('\n').forEach(function(v){ v = v.split(': '); headersObj[ v[ 0 ] ] = v[ 1 ]; }); headersObj.body = body; return headersObj; } function main(){ getStdin() .then( parseMail ) .then( function( mailJson ){ fs.writeFileSync( path.join( mailDir, mailJson[ 'Message-ID' ] ) + '.json', JSON.stringify(mailJson, null, 2) ); }); } if( require.main === module ){ main(); }