-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.js
executable file
·141 lines (128 loc) · 4.04 KB
/
send.js
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
#!/usr/bin/env node
const readline = require("readline");
require("dotenv").config();
const color = require("cli-color");
const { getDigests, setStatus } = require("./api");
const { filter } = require("./targets");
const { getSender } = require("./template");
const { sendDigest, init } = require("./mailer");
const pause = (time) => {
if (!time) {
const min = 7; max = 42; // wait between 7 seconds and 0.7 min
time = Math.floor(Math.random() * (max - min + 1) + min) *1000;
console.log("waiting",time/1000);
}
return new Promise(resolve => setTimeout(resolve, time));
}
const confirm = async (query = "Press [Y] to continue:") => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) =>
rl.question(query, (answser) => {
const input =answser.trim().toLowerCase();
if (input === "y") {
resolve();
return;
}
if (input !== "n") {
console.log(color.yellow(query));
}
rl.close();
process.exit(1);
})
);
};
const help = (status = 0) => {
console.log(
[
"--help (this command)",
"--dry-run",
"--to substitute recipient to handle the emails",
"--verbose",
"--pause|no-pause (by default wait a min or 3 between sending emails)",
"--target= [email protected] or number of targets to process",
"{campaign_name}",
].join("\n")
);
process.exit(status);
};
const argv = require("minimist")(process.argv.slice(2), {
boolean: ["help", "dry-run", "verbose","pause"],
default: {pause:true},
string: ["to"],
unknown: (d) => {
const allowed = ["target"]; //merge with boolean and string?
if (d[0] !== "-") return true;
if (allowed.includes(d.split("=")[0].slice(2))) return true;
console.log(color.red("unknown param", d));
help(1);
},
});
if (argv._.length !== 1) {
console.log(color.red("only one campaign param allowed"), argv._);
help();
}
const campaign = argv._[0];
if (require.main === module) {
if (argv.help || !campaign) return help();
const main = async () => {
let targets = await getDigests(campaign, "pending");
if (targets.length === 0) {
console.error(color.red("no email to send, run prepare"));
process.exit(1);
}
console.log("targetted", targets.length, "from", campaign);
targets = filter(targets, argv.target);
const sender = getSender(campaign);
// const sender = {email:"[email protected]","name":"restore nature"};
await init({
host: process.env.SMTP_SERVER,
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
});
if (argv["dry-run"]) {
console.log("dry run, you might want to set the 'to' param instead");
process.exit(1);
}
if (!argv.to && !argv['dry-run']) {
await confirm();
}
console.log(color.blue ("start ",new Date().toString()));
for (const i in targets) {
const target = targets[i];
// todo: if template not set, supabase.select email,target_id from digests where campaign=campaign and status='sent' group by email
// if in that list -> template= default, else -> initial
recipient = argv.to ? argv.to : target.email ;
if (argv.to && !argv.target) {
console.log(
color.yellow("set target=n when to is defined, we defaulted to 1 ")
);
argv.target = 1;
}
const info = await sendDigest(
recipient,
target.subject,
target.body,
sender
);
if (info.accepted.length >= 1) {
if (!argv.to && !argv['dry-run']) {
console.log("sent", target.email);
await setStatus(target.id, "sent");
} else {
console.log("sent", target.email, argv.to && "replaced by " + argv.to);
}
}
if (argv.pause) {
await pause();
}
}
console.log(color.blue ("end ",new Date().toString()));
};
main().then ( () => process.exit(0)).catch(console.error);
} else {
//export a bunch
module.exports = {};
}