Skip to content

Commit

Permalink
Fix email
Browse files Browse the repository at this point in the history
  • Loading branch information
andyschul committed Jul 20, 2022
1 parent b6ed7ec commit 7e59632
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
23 changes: 11 additions & 12 deletions scheduler/email.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const nodemailer = require("nodemailer");
const sgMail = require('@sendgrid/mail')
const User = require('./models/user');
sgMail.setApiKey(process.env.SENDGRID_API_KEY)

const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS
}
});

async function sendEmail(mailOptions) {
if (mailOptions.to === 'all') {
const users = await User.find({});
const users = await User.find({active: true});
mailOptions.to = users.map(user => user.email).join(',');
}
transporter.sendMail(mailOptions, function(error, info){
error ? console.log(error) : console.log('Email sent: ' + info.response);
});
sgMail
.send(mailOptions)
.then(() => {
console.log('Email sent')
})
.catch((error) => {
console.error(error)
})
}

module.exports = { sendEmail };
46 changes: 46 additions & 0 deletions scheduler/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scheduler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"author": "",
"license": "ISC",
"dependencies": {
"@sendgrid/mail": "^7.7.0",
"axios": "^0.18.0",
"dotenv": "^7.0.0",
"luxon": "^1.12.1",
"mongoose": "^5.5.4",
"node-schedule": "^1.3.2",
"nodemailer": "^6.1.0",
"redis": "^2.8.0"
}
}
6 changes: 6 additions & 0 deletions scheduler/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ async function createSchedulers(year) {
let teeTimeSchedule = schedule.scheduleJob({ start: teeTimesStartDate, end: tournamentStartDate, rule: teeTimeRule, tz: tz }, async function(){
let teeTimes = await api.getTeeTimes(tournament.id);
if (teeTimes.length) {
email.sendEmail({
to: 'abschultz20@gmail.com',
from: 'thegolfpoolnoreply@gmail.com',
subject: `${tournament.name} tee times are out!`,
html: '<strong>Visit https://thegolfpool.herokuapp.com to make your selections!</strong>',
});
this.cancel();
}
});
Expand Down

0 comments on commit 7e59632

Please sign in to comment.