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.
Psudo sendmail script for testing
#!/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) );
});
}
if( require.main === module ){
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment