diff --git a/mail/mail.js b/mail/mail.js new file mode 100644 index 0000000..caac4d4 --- /dev/null +++ b/mail/mail.js @@ -0,0 +1,33 @@ +const app = require('../app'); +const nodemailer = require('nodemailer'); + +if (process.env.NODE_ENV !== 'production') { + require('dotenv').config(); +} + +module.exports = { + send: function (recipient, subject, message) { + send(recipient, subject, message); + } + }; + + +var send = function (recipient, subject, message) { + transporter.sendMail({ + to: recipient, // list of receivers + subject: subject, // Subject line + html: message, // html body + }); +} + +let transporter = nodemailer.createTransport({ + host: process.env.MAIL_HOST, + port: process.env.MAIL_PORT, + secure: process.env.MAIL_SECURE, + auth: { + user: process.env.MAIL_USER, + pass: process.env.MAIL_PASS, + }, +}, { + from: process.env.MAIL_FROM +}); \ No newline at end of file diff --git a/routes/submit.js b/routes/submit.js index bdd7012..30901be 100644 --- a/routes/submit.js +++ b/routes/submit.js @@ -1,4 +1,5 @@ var express = require('express'); +var mail = require('../mail/mail'); var router = express.Router(); /* GET submit page. */ @@ -8,7 +9,30 @@ router.get('/', function(req, res, next) { /* POST submit page. */ router.post('/', function(req, res, next) { + let sport = req.body.sport; + let gender = req.body.gender; + let division = req.body.division; + let home = req.body['home-team']; + let homeScore = req.body['home-team-score']; + let visiting = req.body['visiting-team']; + let visitingScore = req.body['visiting-team-score']; + let submitter = req.body['submitter']; + let recipient = req.body['email']; + + let message = prepMailBody(sport, gender, division, home, homeScore, visiting, visitingScore, submitter); + + mail.send(recipient, "Score Report", message); + res.send('Score sent'); }); +var prepMailBody = function(sport, gender, division, home, homeScore, visiting, visitingScore, submitter) { + return( + "Score report from " + submitter + "

" + + "Sport:
" + sport + " - " + gender + " - " + division + "

" + + "Home team:
" + home + " - " + homeScore + "

" + + "Visiting team:
" + visiting + " - " + visitingScore + "

"); +}; + + module.exports = router;