mirror of https://github.com/eleith/emailjs.git
houston, we now have tests
This commit is contained in:
parent
044e48966c
commit
613090f67f
|
@ -0,0 +1,2 @@
|
|||
node_modules
|
||||
test/config.js
|
34
package.json
34
package.json
|
@ -1,15 +1,23 @@
|
|||
{
|
||||
"name": "emailjs",
|
||||
"description": "send text/html emails and attachments from node.js to any smtp server",
|
||||
"version": "0.1.16",
|
||||
"author": "eleith",
|
||||
"contributors": ["izuzak"],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "http://github.com/eleith/emailjs.git"
|
||||
},
|
||||
"dependencies": {},
|
||||
"engine": ["node >= 0.3.8"],
|
||||
"main": "email"
|
||||
"name": "emailjs",
|
||||
"description": "send text/html emails and attachments from node.js to any smtp server",
|
||||
"version": "0.1.17",
|
||||
"author": "eleith",
|
||||
"contributors":["izuzak"],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "http://github.com/eleith/emailjs.git"
|
||||
},
|
||||
"devDependencies":
|
||||
{
|
||||
"prompt": ">= 0.1.10"
|
||||
},
|
||||
"dependencies":{},
|
||||
"engine": ["node >= 0.3.8"],
|
||||
"main": "email",
|
||||
"scripts":
|
||||
{
|
||||
"test": "node test/run.js"
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,16 @@
|
|||
var config =
|
||||
{
|
||||
host: 'localhost',
|
||||
type: 'ssl', // tls or ssl, delete if you want neither
|
||||
// port: '25',
|
||||
|
||||
username: 'username',
|
||||
|
||||
// if your smtp requires authentication
|
||||
// password: null,
|
||||
|
||||
email: null // where you want test emails going to
|
||||
};
|
||||
|
||||
for(each in config)
|
||||
exports[each] = config[each];
|
|
@ -0,0 +1,130 @@
|
|||
var prompt = require('prompt');
|
||||
var os = require('os');
|
||||
var path = require('path');
|
||||
var tests = require('./tests');
|
||||
var email = require('../email');
|
||||
|
||||
var run =
|
||||
{
|
||||
tests: function(config)
|
||||
{
|
||||
var server = email.server.connect(
|
||||
{
|
||||
user: config.username,
|
||||
password: config.password,
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
tls: config.type == 'tls',
|
||||
ssl: config.type == 'ssl'
|
||||
});
|
||||
|
||||
for(var test in tests)
|
||||
{
|
||||
tests[test](email, server, config);
|
||||
}
|
||||
},
|
||||
|
||||
prompts: function(config)
|
||||
{
|
||||
var prompts =
|
||||
{
|
||||
host:
|
||||
{
|
||||
name: 'host',
|
||||
message: 'smtp hostname or IP',
|
||||
'default': 'localhost'
|
||||
},
|
||||
|
||||
type:
|
||||
{
|
||||
name: 'type',
|
||||
message: 'ssl, tls or none',
|
||||
validator: /^(ssl|tls|none)$/,
|
||||
warning: "connection type must be 'ssl', 'tls', 'none'",
|
||||
'default': 'tls'
|
||||
},
|
||||
|
||||
username:
|
||||
{
|
||||
name: 'username',
|
||||
message: "smtp username",
|
||||
'default': ''
|
||||
},
|
||||
|
||||
password:
|
||||
{
|
||||
name: 'password',
|
||||
message: "smtp password",
|
||||
hidden: true,
|
||||
'default': ''
|
||||
}
|
||||
};
|
||||
|
||||
var ask = [];
|
||||
|
||||
for(var each in prompts)
|
||||
{
|
||||
if(!config[each])
|
||||
ask.push(prompts[each]);
|
||||
}
|
||||
|
||||
if(ask.length)
|
||||
{
|
||||
prompt.start();
|
||||
prompt.message = "";
|
||||
prompt.delimiter = ">";
|
||||
prompt.addProperties(config, ask, function(err) { run.prompts2(config); });
|
||||
}
|
||||
else
|
||||
run.prompts2(config);
|
||||
},
|
||||
|
||||
prompts2: function(config)
|
||||
{
|
||||
var ask = [];
|
||||
|
||||
if(!config.port)
|
||||
{
|
||||
ask.push(
|
||||
{
|
||||
name: 'port',
|
||||
message: 'smtp port',
|
||||
'default': config.type == 'tls' ? 587 : config.type == 'ssl' ? 465 : 25
|
||||
});
|
||||
}
|
||||
|
||||
if(!config.email)
|
||||
{
|
||||
ask.push(
|
||||
{
|
||||
name: 'email',
|
||||
message: 'your email address',
|
||||
empty: false,
|
||||
validator: /.+@.+/,
|
||||
warning: 'not a valid email address'
|
||||
});
|
||||
}
|
||||
|
||||
if(ask.length)
|
||||
{
|
||||
if(!prompt.started)
|
||||
prompt.start();
|
||||
|
||||
prompt.message = "";
|
||||
prompt.delimiter = ">";
|
||||
prompt.addProperties(config, ask, function(err) { run.tests(config); });
|
||||
}
|
||||
else
|
||||
run.tests(config);
|
||||
}
|
||||
};
|
||||
|
||||
path.exists(path.join(__dirname, 'config.js'), function(exists)
|
||||
{
|
||||
var config = {};
|
||||
|
||||
if(exists)
|
||||
config = require(path.join(__dirname, 'config.js'));
|
||||
|
||||
run.prompts(config);
|
||||
});
|
|
@ -0,0 +1,60 @@
|
|||
var tests =
|
||||
{
|
||||
text: function(email, server, config)
|
||||
{
|
||||
var message = email.message.create(
|
||||
{
|
||||
subject: "this is a test TEXT message from emailjs",
|
||||
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
|
||||
to: config.email,
|
||||
text: "hello friend, i hope this message finds you well."
|
||||
});
|
||||
|
||||
server.send(message, function(err, message)
|
||||
{
|
||||
console.log(err ? err.message : "text only email successfully sent");
|
||||
});
|
||||
},
|
||||
|
||||
html: function(email, server, config)
|
||||
{
|
||||
var message = email.message.create(
|
||||
{
|
||||
subject: "this is a test TEXT+HTML message from emailjs",
|
||||
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
|
||||
to: config.email,
|
||||
text: "hello friend, i hope this message finds you well."
|
||||
});
|
||||
|
||||
// attach an alternative html email for those with advanced email clients
|
||||
message.attach_alternative("<html><body> hello <i>friend</i> i hope <b>this</b> <a href='http://github.com/eleith/emailjs'>message</a> finds <b>you</b> well.</body></html>");
|
||||
|
||||
server.send(message, function(err, message)
|
||||
{
|
||||
console.log(err ? err.message : "text+html email successfully sent");
|
||||
});
|
||||
},
|
||||
|
||||
attachment: function(email, server, config)
|
||||
{
|
||||
var path = require('path');
|
||||
var message = email.message.create(
|
||||
{
|
||||
subject: "this is a test TEXT+ATTACHMENT message from emailjs",
|
||||
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
|
||||
to: config.email,
|
||||
text: "hello friend, i hope this message and pdf finds you well."
|
||||
});
|
||||
|
||||
message.attach(path.join(__dirname, "attachments/The Last Question - Isaac Asimov.pdf"), "application/pdf", "the_last_question.pdf");
|
||||
|
||||
// attach an alternative html email for those with advanced email clients
|
||||
server.send(message, function(err, message)
|
||||
{
|
||||
console.log(err ? err.message : "text+attachment email successfully sent");
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
for(test in tests)
|
||||
exports[test] = tests[test];
|
Loading…
Reference in New Issue