-
Notifications
You must be signed in to change notification settings - Fork 0
/
postMark.js
56 lines (48 loc) · 1.38 KB
/
postMark.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 m = require("moment")
const { makeFetchRequest } = require("../helpers/network/fetch")
const { Services } = require("../helpers/constants")
const endpoint = "https://api.postmarkapp.com/email"
const apiKey = process.env.KEY_API_POSTMARK
exports.pmSendEmail = async ({ to, to_name, from, from_name, subject, body, isHTML = false }) => {
let fetchBody = {
From: `${from_name} ${from}`,
To: `${to_name} ${to}`,
Subject: subject,
TrackOpens: true,
TrackLinks: isHTML ? "HtmlAndText" : "TextOnly"
}
if (isHTML) {
fetchBody.HtmlBody = body
} else {
fetchBody.TextBody = body
}
let headers = {
"X-Postmark-Server-Token": apiKey,
Accept: "application/json"
}
await makeFetchRequest(endpoint, fetchBody, headers).catch((e) => {
throw Error(e.message)
})
}
/**
* Process the event notification from the Postmark webhook.
* @param { {} } o POST data object (Postmark POSTs data for each event)
* @returns { {} } The processed event
*/
exports.pmProcessWebhook = (o) => {
let eventType = o.RecordType
if (eventType === "Open" || eventType === "Click") {
let usefulInfo = {
email: o.Recipient,
timestamp: m(o.ReceivedAt, "YYYY-MM-DDTHH:mm:ss.SSSSSSSZ").format(),
event: eventType.toLowerCase(),
useragent: o.UserAgent,
service: Services.POSTMARK
}
if (eventType === "Click") {
usefulInfo.link = o.OriginalLink
}
return usefulInfo
}
return null
}