Skip to content

Commit ecf02e7

Browse files
committed
feat: send mail helper
1 parent 5c0f486 commit ecf02e7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/helpers/SendEmail.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import path from 'path'
2+
import handlebars from 'handlebars'
3+
import { readHTMLFile } from 'helpers/Common'
4+
import EmailProvider from 'config/email'
5+
import ResponseError from 'modules/Response/ResponseError'
6+
import { BASE_URL_CLIENT } from 'config/baseClient'
7+
import { EmailAttributes, UserAttributes } from 'models/user'
8+
9+
class SendMail {
10+
/**
11+
*
12+
* @param formData
13+
* @param token
14+
*/
15+
public static AccountRegister(formData: UserAttributes, token: string) {
16+
const { email, fullName }: EmailAttributes = formData
17+
const pathTemplate = path.resolve(
18+
__dirname,
19+
`../../public/templates/emails/register.html`
20+
)
21+
const subject = 'Verifikasi Email'
22+
const urlToken = `${BASE_URL_CLIENT}/email/verify?token=${token}`
23+
const dataTemplate = { fullName, urlToken }
24+
const Email = new EmailProvider()
25+
26+
readHTMLFile(pathTemplate, (error: Error, html: any) => {
27+
if (error) {
28+
throw new ResponseError.NotFound('email template not found')
29+
}
30+
31+
const template = handlebars.compile(html)
32+
const htmlToSend = template(dataTemplate)
33+
Email.send(email, subject, htmlToSend)
34+
})
35+
}
36+
}
37+
38+
export default SendMail

0 commit comments

Comments
 (0)