Add mail functions
parent
539f207071
commit
7207a1b735
|
@ -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
|
||||
});
|
|
@ -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 <b>" + submitter + "</b><br><br>" +
|
||||
"<b>Sport:</b><br>" + sport + " - " + gender + " - " + division + "<br><br>" +
|
||||
"<b>Home team:</b><br>" + home + " - " + homeScore + "<br><br>" +
|
||||
"<b>Visiting team:</b><br>" + visiting + " - " + visitingScore + "<br><br>");
|
||||
};
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
|
Reference in New Issue