Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
File-Sharing-Website/modules/mailer.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
61 lines (50 sloc)
1.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Sources: | |
https://www.w3schools.com/nodejs/nodejs_email.asp | |
https://www.youtube.com/watch?v=FbDRGpB2eJ8 | |
*/ | |
'use strict' | |
const nodemailer = require('nodemailer') | |
const crypto = require('crypto') | |
const createHash = function(userString) { | |
if (userString === '' || userString === undefined) throw new Error('String cannot be empty.') | |
const hash = crypto.createHash('md5').update(userString).digest('hex') | |
return hash | |
} | |
// eslint-disable-next-line max-lines-per-function | |
const sendMail = function(toAddress, link, userMessage) { | |
// eslint-disable-next-line max-len | |
const mailText = `A user decided to share a file with you! Here is the link:\n ${link}\n\nUser message: ${userMessage}` | |
if (!toAddress.includes('@')) throw new Error('Email invalid') | |
/* istanbul ignore next */ | |
if (!link.includes('/localhost:8080/')) { | |
throw new Error('Wrong download link') | |
} | |
/* istanbul ignore next */ | |
const transporter = nodemailer.createTransport({ | |
service: 'gmail', | |
auth: { | |
user: 'filesharingproject2@gmail.com', | |
//TODO: store pass in seperate file | |
pass: 'passpasspass' | |
} | |
}) /* istanbul ignore next */ | |
const mailOptions = { | |
from: 'filesharingproject2@gmail.com', | |
to: toAddress, | |
subject: 'Sharing a download link', | |
text: mailText | |
} /* istanbul ignore next */ | |
transporter.sendMail(mailOptions, (error, info) => { | |
if (error) { | |
console.log(error) | |
throw new Error('Email not sent') | |
} else { | |
console.log(`Email sent: ${info.response}`) | |
} | |
}) | |
} | |
module.exports = { | |
sendMail, | |
createHash | |
} |