Skip to content

Instantly share code, notes, and snippets.

@harish2704
Last active May 14, 2022 10:55
Show Gist options
  • Save harish2704/273f910610d28e2960ca6ec89ef93b04 to your computer and use it in GitHub Desktop.
Save harish2704/273f910610d28e2960ca6ec89ef93b04 to your computer and use it in GitHub Desktop.

Revisions

  1. harish2704 revised this gist May 14, 2022. 1 changed file with 25 additions and 5 deletions.
    30 changes: 25 additions & 5 deletions sendmail.js
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,52 @@
    #!/usr/bin/env node
    const getStdin = require('get-stdin');
    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( str ){
    str = str.split('\n\n');
    function parseMail( rawStr ){
    str = rawStr.split('\n\n');
    const headers = str[ 0 ];
    const body = str[ 1 ];
    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) );
    // fs.writeFileSync( path.join( mailDir, mailJson[ 'Message-ID' ] ) + '.json', JSON.stringify(mailJson, null, 2) );
    });
    }

  2. harish2704 created this gist Jun 30, 2017.
    35 changes: 35 additions & 0 deletions sendmail.js
    Original 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();
    }