diff --git a/.sample-env b/.sample-env new file mode 100644 index 0000000..2d11661 --- /dev/null +++ b/.sample-env @@ -0,0 +1,5 @@ +CHIPCHOC_EMAIL_HOST=mail.example.com +CHIPCHOC_EMAIL_PORT=587 +CHIPCHOC_EMAIL_SECURE=false +CHIPCHOC_EMAIL_USER=user@example.com +CHIPCHOC_EMAIL_PASSWORD=badexample diff --git a/package.json b/package.json index f8ad5f2..67b22bc 100644 --- a/package.json +++ b/package.json @@ -19,10 +19,11 @@ }, "dependencies": { "cors": "^2.8.5", - "dotenv": "^8.2.0", + "dotenv": "^10.0.0", "express": "^4.17.1", "express-ws": "^4.0.0", "importabular": "^0.2.9", + "nodemailer": "^6.6.1", "showdown": "^1.9.1", "sirv-cli": "^1.0.0" } diff --git a/server.js b/server.js index 69a2b74..4c4f6e7 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,34 @@ const express = require('express') +const nodemailer = require('nodemailer') require('dotenv').config() +let DEBUG + +if (process.env.DEBUG || process.env.CHIPCHOC_DEBUG) { + DEBUG = true +} else { + DEBUG = false +} + +if (DEBUG) console.log('Starting ChipChoc in debug mode') + +// set up nodemailer +const perumal = nodemailer.createTransport({ + // (that's the name of badri's postmaster, in case you were wondering ;) + host: process.env.CHIPCHOC_EMAIL_HOST, + port: process.env.CHIPCHOC_EMAIL_PORT || 587, + secure: process.env.CHIPCHOC_EMAIL_SECURE == 'false' ? false : true, + auth: { + user: process.env.CHIPCHOC_EMAIL_USER, + pass: process.env.CHIPCHOC_EMAIL_PASSWORD, + }, +}) + const app = express() var expressWs = require('express-ws')(app) -const port = process.env.port || 5000 +const port = process.env.CHIPCHOC_PORT || 5000 const cors = require('cors') const path = require('path') @@ -20,10 +43,11 @@ app.get('/air', (req, res) => { }) }) + app.ws('/hit-send', (ws, request) => { console.log('Hitting send on some emails!') - ws.on('message', (message) => { + ws.on('message', async (message) => { try { message = JSON.parse(message) } catch(err) { @@ -34,11 +58,23 @@ app.ws('/hit-send', (ws, request) => { return } - console.log(`\n\n-------------------------- BEGIN EMAIL --------------------------\n`) - console.log(`From: ${message.from}\nTo:${message.to}\n\n${message.text}`) - console.log(`\n--------------------------- END EMAIL ---------------------------`) + if (DEBUG) { + console.log(`\n\n-------------------------- BEGIN EMAIL --------------------------\n`) + console.log(`From: ${message.from}\nTo:${message.to}\n\n${message.text}`) + console.log(`\n--------------------------- END EMAIL ---------------------------`) + } else { + console.log(`Sending email: ${message.from} -> ${message.to}`) - // TODO: actually send the email + // actually send the email + let receipt = await perumal.sendMail({ + from: message.from || process.env.CHIPCHOC_DEFAULT_SENDER, + to: message.to, + bcc: message.from || process.env.CHIPCHOC_DEFAULT_SENDER, + text: message.text, + html: message.html, + }) + + } setTimeout(() => { ws.send(JSON.stringify({ diff --git a/yarn.lock b/yarn.lock index 7c5a684..7708283 100644 --- a/yarn.lock +++ b/yarn.lock @@ -313,10 +313,10 @@ destroy@~1.0.4: resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80" integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA= -dotenv@^8.2.0: - version "8.2.0" - resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a" - integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw== +dotenv@^10.0.0: + version "10.0.0" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81" + integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q== ee-first@1.1.1: version "1.1.1" @@ -732,6 +732,11 @@ negotiator@0.6.2: resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb" integrity sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw== +nodemailer@^6.6.1: + version "6.6.1" + resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.6.1.tgz#2a05fbf205b897d71bf43884167b5d4d3bd01b99" + integrity sha512-1xzFN3gqv+/qJ6YRyxBxfTYstLNt0FCtZaFRvf4Sg9wxNGWbwFmGXVpfSi6ThGK6aRxAo+KjHtYSW8NvCsNSAg== + normalize-path@^3.0.0, normalize-path@~3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"