-
Notifications
You must be signed in to change notification settings - Fork 11
Mailgun function sends confirmation email with html #25
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
Conversation
services/mailer.js
Outdated
|
|
||
| const mailgunModule = {} | ||
|
|
||
| mailgunModule.sendEmail = async (req, res) => { |
There was a problem hiding this comment.
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 }) => {
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}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use logger
services/mailer.js
Outdated
| } | ||
| }) | ||
| } catch (error) { | ||
| console.log(`mailgun did not send email successful ${error}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use logger
package.json
Outdated
| }, | ||
| "dependencies": { | ||
| "pg": "^8.0.0" | ||
| "pg": "^8.0.2" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
services/mailer.js
Outdated
| require('dotenv').config() | ||
|
|
||
| const sendConfirmationEmail = () => { | ||
| const mailgun = require('mailgun-js'); |
There was a problem hiding this comment.
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.
services/mailer.js
Outdated
| if(error){ | ||
| console.log(error) | ||
| } | ||
| console.log(body) |
There was a problem hiding this comment.
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')
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>" |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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>
`
services/mailer.js
Outdated
| }; | ||
| mg.messages().send(data, function(error, body){ | ||
| if(error){ | ||
| logger.error('email failed to send') |
There was a problem hiding this comment.
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.
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){ |
There was a problem hiding this comment.
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
services/mailer.js
Outdated
|
|
||
| const mg = mailgun({apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN}); | ||
|
|
||
| const sendConfirmationEmail = () => { |
There was a problem hiding this comment.
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
services/mailer.js
Outdated
| logger.error('error') | ||
| }) | ||
| } | ||
| sendConfirmationEmail() |
There was a problem hiding this comment.
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.
services/mailer.js
Outdated
| } | ||
| sendConfirmationEmail() | ||
|
|
||
| module.exports = sendConfirmationEmail No newline at end of file |
There was a problem hiding this comment.
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
}
package.json
Outdated
| "dependencies": { | ||
| "mailgun-js": "^0.22.0", | ||
| "pg": "^8.0.2", | ||
| "promisify-call": "^2.0.4", |
There was a problem hiding this comment.
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.
services/mailer.js
Outdated
| }; | ||
|
|
||
| return mg.messages().send(data).then((returnedData) => { | ||
| logger.info('results') |
There was a problem hiding this comment.
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)
services/mailer.js
Outdated
| return mg.messages().send(data).then((returnedData) => { | ||
| logger.info('results') | ||
| }).catch((error) => { | ||
| logger.error('error') |
There was a problem hiding this comment.
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)
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger.error
services/mailer.js
Outdated
| mgModule.sendConfirmationEmail = (receiver, token) => { | ||
| const link = `https://learndatabases.dev/emailConfirmation/${token}` | ||
| const data = { | ||
| from: process.env.SENDER_EMAIL, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
admin@learndatabases.dev
| log: jest.fn() | ||
| } | ||
|
|
||
| logGen.mockReturnValue(logger) |
There was a problem hiding this comment.
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
songz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Handle logger functions when
sendfunction resolves or throws.
|
replaced: #41 (review) |
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.