Permalink
Browse files

Added command line options

  • Loading branch information...
1 parent 1cdf3f6 commit eeb5e4e33536688745f4cac643275ffa4130f971 Andris Reinman committed Sep 4, 2012
Showing with 208 additions and 3 deletions.
  1. +8 −0 README.md
  2. +193 −0 bin/nodemailer
  3. +7 −3 package.json
View
@@ -26,6 +26,7 @@ in a more structured way (with TOC).
* Use objects as header values for **SendGrid** SMTP API
* **XOAUTH** authentication support and token generation (3-legged OAuth) - useful with Gmail
* **DKIM** signing
+ * Send e-mail from **command line**
## Support Nodemailer development
@@ -594,6 +595,13 @@ nodemailer.sendMail(mailOptions, function(error, responseStatus){
});
```
+## Command line usage
+
+If installed globally with -g, ndoemailer command enables to send mail from the command line. Run nodemailer --help for usage instructions.
+
+ npm install -g nodemailer
+ nodemailer --help
+
## Tests
Run the tests with npm in Nodemailer's directory
View
@@ -0,0 +1,193 @@
+#!/usr/bin/env node
+var nodemailer = require("../lib/nodemailer"),
+ argv = require('optimist').argv;
+
+if(argv.help){
+
+ console.log("Send mail with Nodemailer");
+ console.log("http://github.com/andris9/Nodemailer");
+ console.log("");
+ console.log("Syntax:");
+ console.log("nodemailer [options]");
+ console.log("");
+ console.log(" --help show this message");
+ console.log(" --transport=[transport] \"smtp\", \"ses\", \"sendmail\"");
+ console.log(" --debug=true show debug log");
+ console.log("");
+ console.log("SMTP options");
+ console.log(" --host=[hostname] SMTP server hostname");
+ console.log(" --port=[port] SMTP server port");
+ console.log(" --secure=[true:false] Use SSL when connecting");
+ console.log("");
+ console.log("Amazon SES options");
+ console.log(" --key=[AWSKey] Amazon SES key");
+ console.log(" --secret=[AWSSecret] Amazon SES secret");
+ console.log("");
+ console.log("Mail options");
+ console.log(" --from=[address] Sender of the mail");
+ console.log(" --to=[addresslist] Comma separated list of To: addresses");
+ console.log(" --cc=[addresslist] Comma separated list of Cc: addresses");
+ console.log(" --bcc=[addresslist] Comma separated list of Bcc: addresses");
+ console.log(" --subject=[subject] Message subject");
+ console.log(" --text=[plaintext] Plain text mail body");
+ console.log(" --html=[html] HTML mail body");
+ console.log(" --textFile=[path] Plain text mail body from a file");
+ console.log(" --htmlFile=[path] HTML mail body from a file");
+ console.log(" --stdin=[target] Read \"text\" or \"html\" from stdin");
+ console.log(" --textFile=[path] Plain text mail body from a file");
+ console.log(" --attachments=[patlist] Comma separated list of file paths");
+ console.log("");
+ console.log("Example:")
+ console.log("nodemailer --host=smtp.gmail.com --port=465 --secure=true --user=\"myuser@gmail.com\" --pass=\"mypass\" --from=\"myuser@gmail.com\" --to=\"target@example.com\" --subject=\"test\" --text=\"hello world!\"");
+ process.exit(0);
+}
+
+var configFile = argv.config || argv.c || process.env.NODEMAILER_CONFIG;
+
+if(configFile){
+ configFile = require(configFile);
+}else{
+ configFile = {}
+}
+
+var transportType = (argv.transport || configFile.transport || process.env.NODEMAILER_TRANSPORT || "").trim(),
+ service = argv.service || configFile.service || process.env.NODEMAILER_SERVICE,
+ user = argv.user || argv.u || configFile.user || process.env.NODEMAILER_USER,
+ pass = argv.pass || argv.p || configFile.pass || process.env.NODEMAILER_PASS,
+ host = argv.host || argv.h || configFile.host || process.env.NODEMAILER_HOST,
+ port = argv.port || configFile.port || process.env.NODEMAILER_PORT,
+ secure = (argv.secure || configFile.secure || process.env.NODEMAILER_SECURE || "").toString().toLowerCase().trim(),
+ key = argv.key || argv.k || configFile.key || process.env.NODEMAILER_KEY,
+ secret = argv.secret || configFile.secret || process.env.NODEMAILER_SECRET;
+
+if(!transportType){
+ transportType = host?"smtp":"sendmail";
+}
+
+secure = ["false", "null", "0", "undefined"].indexOf(secure)>=0 ? false : !!secure;
+
+var transportOptions = {debug: !!argv.debug || !!argv.d};
+
+switch(transportType.toUpperCase().trim()){
+ case "SMTP":
+ if(service){
+ transportOptions.service = service;
+ }else{
+ transportOptions.host = host;
+ transportOptions.port = port;
+ transportOptions.secureConnection = !!secure;
+ }
+ if(user){
+ transportOptions.auth = {
+ user: user,
+ pass: pass
+ }
+ }
+ break;
+ case "SES":
+ transportOptions.auth = {
+ AWSAccessKeyID: key,
+ AWSSecretKey: secret
+ }
+}
+
+var transport = nodemailer.createTransport(transportType, transportOptions);
+
+
+var from = argv.from || argv.f || configFile.from || process.env.NODEMAILER_FROM,
+ to = argv.to || argv.t,
+ cc = argv.cc,
+ bcc = argv.bcc,
+ subject = argv.subject || argv.s,
+ text = argv.text,
+ html = argv.html,
+ headers = argv.headers,
+ attachments = (argv.attachment || argv.a || "").toString().trim();
+
+if(attachments.length){
+ attachments = attachments.split(",").map(function(a){return {filePath: a.trim()}})
+}else{
+ attachments = [];
+}
+
+if(argv.textFile){
+ argv.text = require("fs").readFileSync(argv.textFile, "utf-8");
+}
+
+if(argv.htmlFile){
+ argv.html = require("fs").readFileSync(argv.htmlFile, "utf-8");
+}
+
+if(argv.stdin){
+ process.stdin.resume();
+ process.stdin.setEncoding('utf8');
+
+ var stdin = "";
+
+ process.stdin.on('data', function (chunk) {
+ stdin += chunk;
+ });
+
+ process.stdin.on('end', function () {
+ if(argv.stdin == "text"){
+ text = stdin;
+ }else{
+ html = stdin;
+ }
+ });
+}else{
+ sendMessage();
+}
+
+
+function sendMessage(){
+ var message = {};
+ if(from){
+ message.from = from;
+ }
+ if(to){
+ message.to = to;
+ }
+ if(cc){
+ message.cc = cc;
+ }
+ if(bcc){
+ message.bcc = bcc;
+ }
+ if(subject){
+ message.subject = subject;
+ }
+ if(text){
+ message.text = text;
+ }
+ if(html){
+ message.html = html;
+ }
+ if(html && !text){
+ message.generateTextFromHTML = true;
+ }
+ if(headers){
+ message.headers = JSON.parse(headers);
+ }
+ if(attachments && attachments.length){
+ message.attachments = attachments;
+ }
+
+ transport.sendMail(message, function(err, resp){
+ if(err){
+ console.log("Error");
+ console.log(err);
+ console.log(resp);
+ process.exit(1);
+ return;
+ }
+ if(resp.failedRecipients && resp.failedRecipients.length){
+ console.log("Warning: Failed to send mail to " + resp.failedRecipients);
+ }
+ console.log(resp.message);
+ process.exit(0);
+ });
+}
+
+// smtp://user:pass@domain:port
+// smtps://user:pass@domain:port
View
@@ -1,7 +1,7 @@
{
"name": "nodemailer",
"description": "Easy to use module to send e-mails, supports unicode and SSL/TLS",
- "version": "0.3.22",
+ "version": "0.3.23",
"author" : "Andris Reinman",
"maintainers":[
{
@@ -26,13 +26,17 @@
],
"dependencies": {
"mailcomposer": ">= 0.1.15",
- "simplesmtp": ">= 0.1.19"
+ "simplesmtp": ">= 0.1.19",
+ "optimist": "*"
},
"devDependencies": {
"nodeunit": "*"
},
"engine": {
- "node": ">=0.4"
+ "node": ">=0.6"
+ },
+ "bin": {
+ "nodemailer" : "./bin/nodemailer"
},
"keywords": ["e-mail", "mime", "email", "mail", "sendmail", "ses"]
}

0 comments on commit eeb5e4e

Please sign in to comment.