-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnode-send.js
More file actions
51 lines (45 loc) · 1.49 KB
/
Copy pathnode-send.js
File metadata and controls
51 lines (45 loc) · 1.49 KB
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
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { compileEmailTemplate } from '../../dist/index.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, '..', '..');
const template = readFileSync(join(rootDir, 'examples', 'auth', 'welcome-verify.html'), 'utf8');
const css = readFileSync(join(rootDir, 'examples', 'auth', 'shared.css'), 'utf8');
async function main() {
const welcomeVerify = await compileEmailTemplate({
subject: 'Verify your ACME email, {{firstName}}',
html: template,
css,
text: 'Hi {{firstName}}, verify your email: {{verifyUrl}}'
});
const email = welcomeVerify.render({
data: {
appUrl: 'https://app.acme.example',
brandUrl: 'https://acme.example',
expiresIn: '30 minutes',
firstName: 'Dan',
verifyUrl: 'https://app.acme.example/auth/verify?token=welcome-token',
welcomeImageUrl: 'https://images.acme.example/welcome.png?messageId=msg_123&recipient=dan'
}
});
await sendEmail({
to: 'dan@example.com',
subject: email.subject,
html: email.html,
text: email.text
});
}
async function sendEmail(message) {
console.log('Ready to send email:');
console.log(JSON.stringify({
to: message.to,
subject: message.subject,
htmlBytes: Buffer.byteLength(message.html),
text: message.text
}, null, 2));
}
main().catch((error) => {
console.error(error);
process.exit(1);
});