forked from nodemailer/nodemailer
/
nodemailer
executable file
·194 lines (171 loc) · 6.13 KB
/
nodemailer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/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(" --user=[username] SMTP username");
console.log(" --pass=[password] SMTP password");
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(" --attachments=[pathlist] 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": (key?"ses":"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){
text = require("fs").readFileSync(argv.textFile, "utf-8");
}
if(argv.htmlFile){
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