Add nodemailer and sendMail function

This commit is contained in:
mmcwilliams 2023-12-14 15:31:36 -05:00
parent 5a755114c6
commit d748706a22
11 changed files with 123 additions and 13 deletions

View File

@ -2,3 +2,6 @@ YOLO_WEB_URL=http://localhost:3333
DATASETS=./datasets
JOB_DELAY=0
APP_NAME=yolo_web
MAIL_FROM=""
MAIL_SMTP=""
MAIL_SMTP_PORT=25

1
dist/index.js vendored
View File

@ -26,6 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv/config");
const express_1 = __importDefault(require("express"));
const promises_1 = __importDefault(require("fs/promises"));
const fs_1 = require("fs");

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

48
dist/mail.js vendored Normal file
View File

@ -0,0 +1,48 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendMail = void 0;
const nodemailer_1 = __importDefault(require("nodemailer"));
const log_1 = require("./log");
const from = process.env['MAIL_FROM'];
const smtp = process.env['MAIL_SMTP'];
const port = typeof process.env['MAIL_SMTP_PORT'] !== 'undefined' ? 25 : parseInt(process.env['MAIL_SMTP_PORT'], 10);
let transporter = null;
const log = (0, log_1.createLog)('mail');
if (typeof from !== 'undefined' && from !== null && from !== '' &&
typeof smtp !== 'undefined' && smtp !== null && smtp !== '') {
transporter = nodemailer_1.default.createTransport({
host: smtp,
port,
secure: false,
tls: { rejectUnauthorized: false }
});
}
async function sendMail(to, subject, body) {
if (transporter !== null) {
try {
await transporter.sendMail({
from,
to,
subject,
html: body
});
log.info(`Sent email "${subject}"`);
}
catch (err) {
log.error(`Error sending email "${subject}"`);
log.error(err);
return false;
}
}
else {
log.warn(`Email not configured, not sending "${subject}"`);
return false;
}
return true;
}
exports.sendMail = sendMail;
module.exports = { sendMail };
//# sourceMappingURL=mail.js.map

1
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V,GAAG,CAAC,KAAK,CAAC,wBAAwB,OAAO,GAAG,CAAC,CAAC;YAC9C,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACf,OAAO,KAAK,CAAC;SAChB;KACJ;SAAM;QACH,GAAG,CAAC,IAAI,CAAC,sCAAsC,OAAO,GAAG,CAAC,CAAC;QAC3D,OAAO,KAAK,CAAC;KAChB;IACD,OAAO,IAAI,CAAC;AAChB,CAAC;AApBD,4BAoBC;AAED,MAAM,CAAC,OAAO,GAAG,EAAE,QAAQ,EAAE,CAAC"}

19
package-lock.json generated
View File

@ -17,6 +17,7 @@
"mime": "^3.0.0",
"multer": "^1.4.5-lts.1",
"node-fetch": "^2.7.0",
"nodemailer": "^6.9.7",
"sqlite3": "^5.1.6",
"triple-beam": "^1.4.1",
"uuid": "^9.0.0",
@ -28,6 +29,7 @@
"@types/mime": "^3.0.1",
"@types/multer": "^1.4.7",
"@types/node-fetch": "^2.6.9",
"@types/nodemailer": "^6.4.14",
"@types/sqlite3": "^3.1.8",
"@types/uuid": "^9.0.2",
"@types/winston": "^2.4.4",
@ -200,6 +202,15 @@
"form-data": "^4.0.0"
}
},
"node_modules/@types/nodemailer": {
"version": "6.4.14",
"resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.4.14.tgz",
"integrity": "sha512-fUWthHO9k9DSdPCSPRqcu6TWhYyxTBg382vlNIttSe9M7XfsT06y0f24KHXtbnijPGGRIcVvdKHTNikOI6qiHA==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/qs": {
"version": "6.9.7",
"resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz",
@ -1688,6 +1699,14 @@
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
}
},
"node_modules/nodemailer": {
"version": "6.9.7",
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.7.tgz",
"integrity": "sha512-rUtR77ksqex/eZRLmQ21LKVH5nAAsVicAtAYudK7JgwenEDZ0UIQ1adUGqErz7sMkWYxWTTU1aeP2Jga6WQyJw==",
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/nopt": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz",

View File

@ -16,6 +16,7 @@
"@types/mime": "^3.0.1",
"@types/multer": "^1.4.7",
"@types/node-fetch": "^2.6.9",
"@types/nodemailer": "^6.4.14",
"@types/sqlite3": "^3.1.8",
"@types/uuid": "^9.0.2",
"@types/winston": "^2.4.4",
@ -30,6 +31,7 @@
"mime": "^3.0.0",
"multer": "^1.4.5-lts.1",
"node-fetch": "^2.7.0",
"nodemailer": "^6.9.7",
"sqlite3": "^5.1.6",
"triple-beam": "^1.4.1",
"uuid": "^9.0.0",

View File

@ -21,12 +21,6 @@ if [[ "${JSON}" != *"{"* ]]; then
exit 1
fi
if [ -f ./scripts/claim_mail.sh ]; then
bash ./scripts/claim_mail.sh "${JSON}"
else
echo "No email script, not sending."
fi
ID=$(echo $JSON | jq -r '.id')
bash ./scripts/train.sh "${JSON}"

View File

@ -103,8 +103,3 @@ rm "${ZIPPATH}"
deactivate
cd ..
if [ -f ./scripts/completed_mail.sh ]; then
bash ./scripts/completed_mail.sh "${JSON}"
else
echo "No email script, not sending"
fi

View File

@ -1,3 +1,4 @@
import 'dotenv/config'
import express from 'express';
import { Express, Request, Response, NextFunction } from 'express'
import fs from 'fs/promises';
@ -11,6 +12,7 @@ import multer from 'multer';
import { v4 as uuid } from 'uuid';
import { getType } from 'mime';
import { createLog } from './log'
import { sendMail } from './mail';
import type { Logger } from 'winston';
import * as Handlebars from 'handlebars';

45
src/mail.ts Normal file
View File

@ -0,0 +1,45 @@
import nodemailer from 'nodemailer';
import { createLog } from './log';
import type { Logger } from 'winston';
const from : string = process.env['MAIL_FROM'];
const smtp : string = process.env['MAIL_SMTP'];
const port : number = typeof process.env['MAIL_SMTP_PORT'] !== 'undefined' ? 25 : parseInt(process.env['MAIL_SMTP_PORT'], 10);
let transporter : nodemailer.Transporter = null;
const log : Logger = createLog('mail');
if (typeof from !== 'undefined' && from !== null && from !== '' &&
typeof smtp !== 'undefined' && smtp !== null && smtp !== '') {
transporter = nodemailer.createTransport({
host: smtp,
port,
secure : false,
tls: { rejectUnauthorized: false }
});
}
export async function sendMail (to : string, subject : string, body : string) : Promise<boolean>{
if (transporter !== null) {
try {
await transporter.sendMail({
from,
to,
subject,
html: body
});
log.info(`Sent email "${subject}"`);
} catch (err) {
log.error(`Error sending email "${subject}"`);
log.error(err);
return false;
}
} else {
log.warn(`Email not configured, not sending "${subject}"`);
return false;
}
return true;
}
module.exports = { sendMail };