-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.js
56 lines (48 loc) · 1.51 KB
/
email.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const nodemailer = require('nodemailer');
const htmlToText = require('html-to-text');
const fs = require('fs');
const path = require('path');
const Handlebars = require('handlebars');
var postmark = require('postmark');
var client = new postmark.ServerClient('9277babd-4736-42f6-b8c5-fcbc211c1152');
module.exports = class Email {
constructor(user, url) {
this.to = user.email;
// this.firstName = user.name.split(' ')[0];
this.url = url;
this.from = `Sam Singer <${process.env.EMAIL_FROM}>`;
}
// Send the actual email
async send(template, subject) {
const emailTemplateString = fs.readFileSync(
`${path.resolve('emailViews')}/template.hbs`,
'utf-8'
);
const hbsTemplate = Handlebars.compile(emailTemplateString);
const compiledHbsTemplate = hbsTemplate({
firstName: this.firstName,
url: this.url
});
console.log(hbsTemplate);
await client.sendEmail({
From: '[email protected]',
To: '[email protected]',
Subject: 'Hello from Postmark',
HtmlBody: compiledHbsTemplate,
MessageStream: 'outbound'
});
// 1) Render HTML based on a pug template
// 2) Define email options
// 3) Create a transport and send email
// await this.newTransport().sendMail(mailOptions);
}
async sendWelcome() {
await this.send('welcome', 'Welcome to the Natours Family!');
}
async sendPasswordReset() {
await this.send(
'passwordReset',
'Your password reset token (valid for only 10 minutes)'
);
}
};