Skip to content

Mailgun function sends confirmation email with html#25

Closed
paulvalderama wants to merge 19 commits into
garageScript:masterfrom
paulvalderama:mailgun
Closed

Mailgun function sends confirmation email with html#25
paulvalderama wants to merge 19 commits into
garageScript:masterfrom
paulvalderama:mailgun

Conversation

@paulvalderama
Copy link
Copy Markdown

@paulvalderama paulvalderama commented Apr 16, 2020

The function receives the email input for who to send the email to, configures the mail to send including html, sends confirmation email to user. Function is stored in mailgunModule and exported to be used in server file.

Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailgun.js Outdated
Comment thread services/mailer.js Outdated

const mailgunModule = {}

mailgunModule.sendEmail = async (req, res) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mailer.sendConfirmationEmail = async ( { recipient, confirmationToken }) => {

Comment thread services/mailer.test.js Outdated
Comment thread services/mailer.test.js Outdated
Comment thread services/mailer.js Outdated
html: '<h1>Congratulations! Confirm your E-Mail with C0D3</h1><button>Confirm</button>'
}, (error) => {
if (error) {
console.log(`error sending email ${error}`)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use logger

Comment thread services/mailer.js Outdated
}
})
} catch (error) {
console.log(`mailgun did not send email successful ${error}`)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use logger

Comment thread services/mailgun.js Outdated
Comment thread package.json Outdated
},
"dependencies": {
"pg": "^8.0.0"
"pg": "^8.0.2"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mailgun should appear in dependencies, add $ npm i --save mailgun-js

Copy link
Copy Markdown

@allopez7 allopez7 Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you don't have the latest devDependencies and dependencies. You need to go to master and $ git pull then on your branch $ git pull origin master so you can get all the latest changes.

Comment thread services/mailer.js Outdated
Comment thread services/mailer.js Outdated
require('dotenv').config()

const sendConfirmationEmail = () => {
const mailgun = require('mailgun-js');
Copy link
Copy Markdown

@allopez7 allopez7 Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

place mailgun on the first line. Typically we place our modules at the top.

Comment thread services/mailer.js Outdated
Comment thread services/mailer.js Outdated
if(error){
console.log(error)
}
console.log(body)
Copy link
Copy Markdown

@allopez7 allopez7 Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inside of logger we also have logger.info which is equivalent to console.log. Use logger.info('email successfully sent')

Comment thread services/mailer.js Outdated
Comment thread services/mailer.js Outdated
to: process.env.RECEIVER_EMAIL,
subject: 'Congratulations!',
text: 'Welcome to C0D3',
html: "<h1>Confirm your E-mail</h1><button>Confirm</button>"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

html: .... <a href="https://learndb.dev/emailConfirmation/ token from parameter>Click to confirm</a>

Copy link
Copy Markdown
Contributor

@songz songz Apr 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const link = `https://learndatabases.dev/emailConfirmation/${token}`
html: `
<h1> Confirm your Email </h1>
<p>
  <a href="${link}">Click Here</a>
</p>
<p> Or visit this link: <a href="${link}">${link}</a></p>
`

Comment thread services/mailer.test.js
Comment thread services/mailer.js Outdated
};
mg.messages().send(data, function(error, body){
if(error){
logger.error('email failed to send')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't want to be calling both error and info. So you need return.

Comment thread services/mailer.js Outdated
text: 'Welcome to C0D3',
html: "<h1>Confirm your E-mail</h1><button>Confirm</button>"
};
mg.messages().send(data, function(error, body){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no way for caller to see whether email was successful or not. You should return a promise that resolves or rejects. From the documentation, it seems like the library returns promises: https://www.npmjs.com/package/mailgun-js#promises

Comment thread services/mailer.js Outdated

const mg = mailgun({apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN});

const sendConfirmationEmail = () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

takes in 2 arguments. receiver, token

Comment thread services/mailer.js Outdated
logger.error('error')
})
}
sendConfirmationEmail()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you running your own function.

Comment thread services/mailer.js Outdated
}
sendConfirmationEmail()

module.exports = sendConfirmationEmail No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may be sending other types of email in the future, so please export an object.

module.exports = {
  sendConfirmationEmail
}

Comment thread services/mailer.test.js
Comment thread package.json Outdated
"dependencies": {
"mailgun-js": "^0.22.0",
"pg": "^8.0.2",
"promisify-call": "^2.0.4",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this module necessary? I don't see you using it anywhere. if not, please remove it.

Comment thread services/mailer.js Outdated
};

return mg.messages().send(data).then((returnedData) => {
logger.info('results')
Copy link
Copy Markdown
Contributor

@songz songz Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.info('Confirmation Email successfully sent', returnedData)

Comment thread services/mailer.js Outdated
return mg.messages().send(data).then((returnedData) => {
logger.info('results')
}).catch((error) => {
logger.error('error')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.info('Confirmation Email Error:', error)

Comment thread services/mailer.js Outdated
return mg.messages().send(data).then((returnedData) => {
logger.info('Confirmation Email successfully sent', returnedData)
}).catch((error) => {
logger.info('Confirmation Email Error:', error)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.error

Comment thread services/mailer.test.js Outdated
Comment thread services/mailer.js Outdated
mgModule.sendConfirmationEmail = (receiver, token) => {
const link = `https://learndatabases.dev/emailConfirmation/${token}`
const data = {
from: process.env.SENDER_EMAIL,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

admin@learndatabases.dev

Comment thread services/mailer.test.js
log: jest.fn()
}

logGen.mockReturnValue(logger)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the logger or remove logGen

Copy link
Copy Markdown
Contributor

@songz songz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Handle logger functions when send function resolves or throws.

@songz
Copy link
Copy Markdown
Contributor

songz commented May 7, 2020

replaced: #41 (review)

@songz songz closed this May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants