Skip to content

Commit

Permalink
feat: send mail helper
Browse files Browse the repository at this point in the history
  • Loading branch information
masb0ymas committed Nov 23, 2020
1 parent 5c0f486 commit ecf02e7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/helpers/SendEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import path from 'path'
import handlebars from 'handlebars'
import { readHTMLFile } from 'helpers/Common'
import EmailProvider from 'config/email'
import ResponseError from 'modules/Response/ResponseError'
import { BASE_URL_CLIENT } from 'config/baseClient'
import { EmailAttributes, UserAttributes } from 'models/user'

class SendMail {
/**
*
* @param formData
* @param token
*/
public static AccountRegister(formData: UserAttributes, token: string) {
const { email, fullName }: EmailAttributes = formData
const pathTemplate = path.resolve(
__dirname,
`../../public/templates/emails/register.html`
)
const subject = 'Verifikasi Email'
const urlToken = `${BASE_URL_CLIENT}/email/verify?token=${token}`
const dataTemplate = { fullName, urlToken }
const Email = new EmailProvider()

readHTMLFile(pathTemplate, (error: Error, html: any) => {
if (error) {
throw new ResponseError.NotFound('email template not found')
}

const template = handlebars.compile(html)
const htmlToSend = template(dataTemplate)
Email.send(email, subject, htmlToSend)
})
}
}

export default SendMail

0 comments on commit ecf02e7

Please sign in to comment.