Created
December 21, 2010 18:53
-
-
Save stagas/750365 to your computer and use it in GitHub Desktop.
simple sendmail for node.js
This file contains 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 characters
// | |
var net = require('net') | |
, config = { | |
type: 'text/plain' | |
} | |
exports.send = function(options, cb) { | |
var stream = net.createConnection(options.port, options.host) | |
var from = options.from.match(/(<)(.*)(>)/) | |
? options.from.match(/(<)(.*)(>)/)[2] | |
: options.from | |
var to = options.to.match(/(<)(.*)(>)/) | |
? options.to.match(/(<)(.*)(>)/)[2] | |
: options.to | |
stream.on('connect', function() { | |
var out = [ | |
'helo ' + (options.domain || from.split('@')[1]) | |
, 'auth login' | |
, options.username | |
, options.password | |
, 'mail from:<' + from + '>' | |
, 'rcpt to:<' + to + '>' | |
, 'data' | |
, 'From: ' + options.from | |
, 'To: ' + options.to | |
, 'Date: ' + new Date().toUTCString() | |
, 'Subject: ' + options.subject | |
, 'Content-Type: ' + (options.type || config.type) | |
, '' | |
, options.body | |
.split(/\r\n|\n|\r/) | |
.join('\r\n') | |
.replace('\r\n.\r\n', '\r\n. \r\n') | |
, '.' | |
, 'quit' | |
] | |
stream.end(out.join('\r\n')) | |
}) | |
stream.on('error', function(err) { | |
cb && cb(err) | |
}) | |
stream.on('end', function() { | |
cb && cb() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment