From 98f86657ebf6bf8a9fdccbfe14cfb1852d151c08 Mon Sep 17 00:00:00 2001 From: GP Date: Fri, 5 Feb 2021 11:26:57 +1300 Subject: [PATCH] Emailjs (#346) * switch to emailjs --- config/app_TEMPLATE.js | 3 ++- emailUtil.ts | 31 ++++++++++++------------------- package-lock.json | 18 +++++------------- package.json | 3 +-- report-service-errors.ts | 1 - 5 files changed, 20 insertions(+), 36 deletions(-) diff --git a/config/app_TEMPLATE.js b/config/app_TEMPLATE.js index 8c560dac..a2592e92 100644 --- a/config/app_TEMPLATE.js +++ b/config/app_TEMPLATE.js @@ -33,8 +33,9 @@ const s3 = { }; const smtpDetails = { - from: "localhost", + host: "localhost", port: 25, + tls: true, from_name: "Cacophony Reporting" // // service: "gmail", diff --git a/emailUtil.ts b/emailUtil.ts index 9cae0de9..128618db 100644 --- a/emailUtil.ts +++ b/emailUtil.ts @@ -3,9 +3,7 @@ import { Recording } from "./models/Recording"; import { TrackTag } from "./models/TrackTag"; import log from "./logging"; import moment, { Moment } from "moment"; -import nodemailer from "nodemailer"; -import Mail from "nodemailer/lib/mailer"; -import { SentMessageInfo } from "nodemailer/lib/smtp-connection"; +import { SMTPClient, Message } from "emailjs"; function alertBody( recording: Recording, @@ -29,26 +27,21 @@ async function sendEmail( to: string, subject: string ): Promise { - const transporter: Mail = nodemailer.createTransport(config.smtpDetails); + const client = new SMTPClient(config.smtpDetails); - var mailOptions: Mail.Options = { - from: config.smtpDetails.from_name, - to: to, - subject: subject, - html: html, - text: text - }; try { - const info: SentMessageInfo = await transporter.sendMail(mailOptions); - if (info.rejected.length > 0) { - log.error(info); - return false; - } - } catch (error) { - log.error(error); + const message = new Message({ + text: text, + from: config.smtpDetails.from_name, + to: to, + subject: subject, + attachment: [{ data: text, alternative: true }] + }); + await client.sendAsync(message); + } catch (err) { + log.error(err); return false; } - return true; } diff --git a/package-lock.json b/package-lock.json index 7236719b..0ee2adf0 100755 --- a/package-lock.json +++ b/package-lock.json @@ -117,14 +117,6 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-12.0.10.tgz", "integrity": "sha512-LcsGbPomWsad6wmMNv7nBLw7YYYyfdYcz6xryKYQhx89c3XXan+8Q6AJ43G5XDIaklaVkK3mE4fCb0SBvMiPSQ==" }, - "@types/nodemailer": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.4.0.tgz", - "integrity": "sha512-KY7bFWB0MahRZvVW4CuW83qcCDny59pJJ0MQ5ifvfcjNwPlIT0vW4uARO4u1gtkYnWdhSvURegecY/tzcukJcA==", - "requires": { - "@types/node": "*" - } - }, "@types/qs": { "version": "6.9.1", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.1.tgz", @@ -943,6 +935,11 @@ "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" }, + "emailjs": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/emailjs/-/emailjs-3.4.0.tgz", + "integrity": "sha512-GYzxHth2mOtfjGvQQYoS7747lrGj/7qohZiY1zQe4CtzVhDuRJ/WNKlhII1tZ0YAUC6190eA7paMuB7AJj4d3w==" + }, "emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", @@ -2572,11 +2569,6 @@ "tar": "^4" } }, - "nodemailer": { - "version": "6.4.16", - "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.4.16.tgz", - "integrity": "sha512-68K0LgZ6hmZ7PVmwL78gzNdjpj5viqBdFqKrTtr9bZbJYj6BRj5W6WGkxXrEnUl3Co3CBXi3CZBUlpV/foGnOQ==" - }, "nopt": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.1.tgz", diff --git a/package.json b/package.json index 2cbf9c18..71b12df1 100755 --- a/package.json +++ b/package.json @@ -11,12 +11,12 @@ "@types/express-validator": "^3.0.0", "@types/jsonwebtoken": "^8.3.8", "@types/moment-timezone": "^0.5.13", - "@types/nodemailer": "^6.4.0", "apidoc": "^0.17.7", "aws-sdk": "^2.653.0", "bcrypt": "^3.0.8", "body-parser": "^1.19.0", "commander": "^2.20.3", + "emailjs": "^3.4.0", "express": "^4.17.1", "express-validator": "^5.3.1", "express-winston": "^2.5.0", @@ -36,7 +36,6 @@ "node-cmd": "^1.1.1", "node-gyp": "^4.0.0", "node-pre-gyp": "^0.10.2", - "nodemailer": "^6.4.16", "passport": "^0.3.2", "passport-anonymous": "^1.0.1", "passport-jwt": "^4.0.0", diff --git a/report-service-errors.ts b/report-service-errors.ts index d2bc1f1f..27d62c45 100644 --- a/report-service-errors.ts +++ b/report-service-errors.ts @@ -3,7 +3,6 @@ const winston = require("winston"); import eventUtil from "./api/V1/eventUtil"; import moment, { Moment } from "moment"; import { ServiceErrorMap } from "./api/V1/systemError"; -import nodemailer from "nodemailer"; import { sendEmail } from "./emailUtil"; async function main() {