commit 7e3eb693dba6a4f356ce84835279bcbc8031e1b4 Author: David Kebler Date: Thu Jul 16 12:49:29 2020 -0700 working initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ccbe46 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules/ diff --git a/email.js b/email.js new file mode 100644 index 0000000..c515eb5 --- /dev/null +++ b/email.js @@ -0,0 +1,45 @@ + +import gmail from 'gmail-send' +import ses from 'node-ses' +import {stringify as nice} from 'yaml' + +const create = { gmail:gmail,ses:ses } + +class Email { + constructor(opts) { + this._pkg = { + subject: opts.subject || 'notification', + to: opts.to, + label: opts.label + } + Object.assign(this._pkg,opts.credentials) + this.smtp = opts.smtp || 'gmail' + this._send = create[this.smtp](this.pkg) + } + + async send (msg='', opts={}) { + const pkg = Object.assign({},this._pkg,opts) + if (typeof msg === 'string') { pkg.subject = msg; pkg.text = msg } + else { + pkg.subject = msg.msg || msg.message || msg.subject + pkg.text = msg.text || nice(msg) + pkg.html = msg.html + pkg.files = msg.files + } + if (!pkg.to) return {error:'no recipients'} + try { + let res = await this._send(pkg) + return res + } catch (err) { + return {error:err} + } + } + + setOpts (opts={}) { + Object.assign(this.pkg,opts) + } + +} + +export default Email +export { Email as Service } diff --git a/package.json b/package.json new file mode 100644 index 0000000..e26b023 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "@uci-utils/notify-email-plugin", + "version": "0.1.5", + "description": "email uci notify plugin", + "main": "email.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "notification", + "email" + ], + "author": "codecoots", + "license": "MIT", + "dependencies": { + "gmail-send": "^1.8.10", + "node-ses": "^3.0.2", + "yaml": "^1.10.0" + } +}