Mailgun function sends confirmation email with html#25
Conversation
|
|
||
| const mailgunModule = {} | ||
|
|
||
| mailgunModule.sendEmail = async (req, res) => { |
There was a problem hiding this comment.
mailer.sendConfirmationEmail = async ( { recipient, confirmationToken }) => {
| html: '<h1>Congratulations! Confirm your E-Mail with C0D3</h1><button>Confirm</button>' | ||
| }, (error) => { | ||
| if (error) { | ||
| console.log(`error sending email ${error}`) |
| } | ||
| }) | ||
| } catch (error) { | ||
| console.log(`mailgun did not send email successful ${error}`) |
| }, | ||
| "dependencies": { | ||
| "pg": "^8.0.0" | ||
| "pg": "^8.0.2" |
There was a problem hiding this comment.
mailgun should appear in dependencies, add $ npm i --save mailgun-js
There was a problem hiding this comment.
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.
| require('dotenv').config() | ||
|
|
||
| const sendConfirmationEmail = () => { | ||
| const mailgun = require('mailgun-js'); |
There was a problem hiding this comment.
place mailgun on the first line. Typically we place our modules at the top.
| if(error){ | ||
| console.log(error) | ||
| } | ||
| console.log(body) |
There was a problem hiding this comment.
inside of logger we also have logger.info which is equivalent to console.log. Use logger.info('email successfully sent')
| 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.
html: .... <a href="https://learndb.dev/emailConfirmation/ token from parameter>Click to confirm</a>
There was a problem hiding this comment.
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>
`
| }; | ||
| mg.messages().send(data, function(error, body){ | ||
| if(error){ | ||
| logger.error('email failed to send') |
There was a problem hiding this comment.
you don't want to be calling both error and info. So you need return.
| 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.
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
|
|
||
| const mg = mailgun({apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN}); | ||
|
|
||
| const sendConfirmationEmail = () => { |
There was a problem hiding this comment.
takes in 2 arguments. receiver, token
| logger.error('error') | ||
| }) | ||
| } | ||
| sendConfirmationEmail() |
There was a problem hiding this comment.
why are you running your own function.
| } | ||
| sendConfirmationEmail() | ||
|
|
||
| module.exports = sendConfirmationEmail No newline at end of file |
There was a problem hiding this comment.
you may be sending other types of email in the future, so please export an object.
module.exports = {
sendConfirmationEmail
}
| "dependencies": { | ||
| "mailgun-js": "^0.22.0", | ||
| "pg": "^8.0.2", | ||
| "promisify-call": "^2.0.4", |
There was a problem hiding this comment.
is this module necessary? I don't see you using it anywhere. if not, please remove it.
| }; | ||
|
|
||
| return mg.messages().send(data).then((returnedData) => { | ||
| logger.info('results') |
There was a problem hiding this comment.
logger.info('Confirmation Email successfully sent', returnedData)
| return mg.messages().send(data).then((returnedData) => { | ||
| logger.info('results') | ||
| }).catch((error) => { | ||
| logger.error('error') |
There was a problem hiding this comment.
logger.info('Confirmation Email Error:', error)
| return mg.messages().send(data).then((returnedData) => { | ||
| logger.info('Confirmation Email successfully sent', returnedData) | ||
| }).catch((error) => { | ||
| logger.info('Confirmation Email Error:', error) |
| mgModule.sendConfirmationEmail = (receiver, token) => { | ||
| const link = `https://learndatabases.dev/emailConfirmation/${token}` | ||
| const data = { | ||
| from: process.env.SENDER_EMAIL, |
| log: jest.fn() | ||
| } | ||
|
|
||
| logGen.mockReturnValue(logger) |
There was a problem hiding this comment.
Test the logger or remove logGen
songz
left a comment
There was a problem hiding this comment.
- 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.