First Commit
This commit is contained in:
113
src/index.ts
Normal file
113
src/index.ts
Normal file
@ -0,0 +1,113 @@
|
||||
process.env.LOGGING_NO_DEFAULT = "true";
|
||||
import { LoggingBase, ConsoleAdapter } from "@hibas123/logging";
|
||||
|
||||
import * as yargs from "yargs";
|
||||
|
||||
const options = yargs
|
||||
.option("verbose", {
|
||||
alias: "v",
|
||||
type: "boolean",
|
||||
description: "Print debug informations",
|
||||
required: false,
|
||||
})
|
||||
.option("config", {
|
||||
alias: "c",
|
||||
type: "string",
|
||||
default: "/etc/issc.ini",
|
||||
description: "Config file location",
|
||||
})
|
||||
.option("to", {
|
||||
alias: "t",
|
||||
description: "The receiver of the email",
|
||||
type: "string",
|
||||
required: true,
|
||||
})
|
||||
.option("subject", {
|
||||
alias: "s",
|
||||
description: "Subject of the mail",
|
||||
type: "string",
|
||||
required: false,
|
||||
}).argv;
|
||||
|
||||
const Logging = new LoggingBase({
|
||||
console: false,
|
||||
});
|
||||
|
||||
if (options.verbose) {
|
||||
Logging.addAdapter(new ConsoleAdapter(true));
|
||||
}
|
||||
|
||||
Logging.log("Starting INSANLY SIMPLE SMTP CLIENT");
|
||||
|
||||
const EXAMPLE_CONFIG = `
|
||||
[smtp]
|
||||
server=
|
||||
from=
|
||||
username=
|
||||
password=
|
||||
port=
|
||||
`;
|
||||
|
||||
import * as nm from "nodemailer";
|
||||
|
||||
import * as ini from "ini";
|
||||
import { readFileSync, fstat, existsSync } from "fs";
|
||||
|
||||
Logging.log("Loading configuration");
|
||||
|
||||
if (!existsSync(options.config)) {
|
||||
console.log(
|
||||
"Add configuration to /etc/issc.ini or set your location by using --config <path>. \n\n Example config: \n\n" +
|
||||
EXAMPLE_CONFIG
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const config = ini.parse(readFileSync(options.config, "utf-8"));
|
||||
Logging.log("Creating Transport");
|
||||
const transport = nm.createTransport({
|
||||
host: config.smtp.server,
|
||||
port: Number(config.smtp.port || 587),
|
||||
secure: false,
|
||||
auth: {
|
||||
user: config.smtp.username,
|
||||
pass: config.smtp.password,
|
||||
method: "PLAIN",
|
||||
},
|
||||
logger: {
|
||||
fatal: Logging.errorMessage,
|
||||
trace: Logging.log,
|
||||
level: () => undefined,
|
||||
debug: Logging.debug,
|
||||
error: Logging.errorMessage,
|
||||
info: Logging.log,
|
||||
warn: Logging.warn,
|
||||
},
|
||||
});
|
||||
Logging.log("Verifying transport");
|
||||
|
||||
import getstdin = require("get-stdin");
|
||||
|
||||
transport
|
||||
.verify()
|
||||
.then(async () => {
|
||||
Logging.log("Reading STDIN");
|
||||
const text = await getstdin();
|
||||
Logging.log("Sending mail");
|
||||
const res = await transport.sendMail({
|
||||
from: config.smtp.from,
|
||||
to: options.to,
|
||||
subject: options.subject,
|
||||
text,
|
||||
});
|
||||
|
||||
console.log("Message sent");
|
||||
})
|
||||
.catch((err) => {
|
||||
Logging.error(err);
|
||||
console.log("Some error occured!", err.message);
|
||||
})
|
||||
.finally(() => {
|
||||
transport.close();
|
||||
process.exit();
|
||||
});
|
Reference in New Issue
Block a user