Skip to content

Commit

Permalink
Merge pull request #93 from RyanWils/issue-25
Browse files Browse the repository at this point in the history
Fix #25: Implement basic email sender
  • Loading branch information
RyanWils committed Nov 14, 2019
2 parents 93233dc + d0dedf9 commit 70c1290
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ PORT=8080

# REDIS_URL specifies Redis server info
REDIS_URL=redis://127.0.0.1:6379

# NODEMAILER_USERNAME is sender's username credential
NODEMAILER_USERNAME=

# NODEMAILER_PASSWORD is sender's password credential
NODEMAILER_PASSWORD=

# NODEMAILER_SERVER is sender's server that they are using (Example: smtp.example.com)
NODEMAILER_SERVER=
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"feedparser-promised": "^2.0.1",
"node-summarizer": "^1.0.7",
"jsdom": "^15.2.1",
"nodemailer": "^6.3.1",
"sentiment": "^5.0.2"
},
"devDependencies": {
Expand Down
58 changes: 58 additions & 0 deletions src/email-sender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const nodemailer = require('nodemailer');

/*
HOW TO USE
Import this file - const sendEmail = require('./email-sender);
send email - sendEmail.sendMessage('emailName@hotmail.com','Seneca had an error',
'<p>An Error had occured</p>');
Parameters examples
receipiants - 'emailName@hotmail.com,emailName2@hotmail.com'
subjectMessage - 'Seneca Telescope had an error'
message - '<p>An Error has occurred <br/>Error Message:</p>'
*/

exports.sendMessage = async function (receipiants, subjectMessage, message) {
return new Promise((resolve, reject) => {
const transporter = nodemailer.createTransport('SMTP', {
// Email Server (.env variable must be used refer to the Contribution.md)
host: process.env.NODEMAILER_SERVER,
port: 25,
secure: false,
auth: {
// Email Name (.env variables must be used refer to the Contribution.md)
user: process.env.NODEMAILER_USERNAME,
// Email Pass (.env variables must be used refer to the Contribution.md)
pass: process.env.NODEMAILER_PASSWORD,
},
});

// verify connection configuration
transporter.verify((error, success) => {
// If error then print to console
if (error) {
console.log(error);
// else print a ready message
} else if (success) {
console.log('Server is running properly');
}
});

// Email Content
const mailOptions = {
from: process.env.NODEMAILER_USERNAME, // Email Name
to: receipiants, // People to send to
subject: subjectMessage, // Subject Line
html: message, // Message Body
};

// Send the email with the email content
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
reject(err); // Send promise.reject if an error occurs
} else {
resolve(info.accepted); // Send promise.resolve if an error occurs
}
});
});
};

0 comments on commit 70c1290

Please sign in to comment.