-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendmail.js
More file actions
44 lines (42 loc) · 1.05 KB
/
Copy pathsendmail.js
File metadata and controls
44 lines (42 loc) · 1.05 KB
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
#!/usr/bin/env node
var nodemailer = require("nodemailer");
const simpleParser = require("mailparser").simpleParser;
const path = require("path");
const os = require("os");
const config = require(path.join(os.homedir(), "sendmail-config.json"));
function strMapToObj(strMap) {
let obj = Object.create(null);
for (let [k, v] of strMap) {
obj[k] = v;
}
return obj;
}
console.log("using config", config);
const transporter = nodemailer.createTransport(config);
console.log("reading email from stdin");
const source = process.stdin;
simpleParser(source)
.then(({ headers, to, subject, text, html, textAsHtml }) => {
from = config.auth.user;
to = to.text;
headers = strMapToObj(headers);
const email = {
headers,
from,
to,
subject,
text,
html,
textAsHtml
};
console.log("sending email", { from, to, subject, headers });
return transporter.sendMail(email);
})
.then(res => {
console.log(res);
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1);
});