Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #25: Implement basic email sender #93

Merged
merged 11 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=
RyanWils marked this conversation as resolved.
Show resolved Hide resolved
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); // Send promise.resolve if an error occurs
RyanWils marked this conversation as resolved.
Show resolved Hide resolved
}
});
});
};