-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-email.js
41 lines (37 loc) · 986 Bytes
/
send-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
/**
* Run this script from the command line with the
* path to the HTML file you want to send.
*
* `node send-email.js ./dist/index.html`
*/
import sgMail from '@sendgrid/mail';
import * as dotenv from 'dotenv';
import { readFileSync } from 'fs';
dotenv.config();
const htmlFile = process.argv[2];
let htmlContent = null;
// Get the HTML file contents
try {
htmlContent = readFileSync(htmlFile, 'utf8');
} catch (err) {
console.error(err);
}
// Send the email if we have the htmlContent
if (htmlContent) {
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: process.env.SEND_EMAIL_TO, // Change to your recipient
from: process.env.SEND_EMAIL_FROM, // Change to your verified sender
subject: 'Testing HTML Email',
html: htmlContent,
};
sgMail
.send(msg)
.then((response) => {
console.log(response[0].statusCode);
console.log(response[0].headers);
})
.catch((error) => {
console.error(error);
});
}