Skip to content

Instantly share code, notes, and snippets.

@stagas
Created December 21, 2010 18:53
Show Gist options
  • Save stagas/750365 to your computer and use it in GitHub Desktop.
Save stagas/750365 to your computer and use it in GitHub Desktop.
simple sendmail for node.js
//
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