-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.ts
60 lines (54 loc) · 1.33 KB
/
mailer.ts
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
57
58
59
60
import SibApiV3Sdk from "sib-api-v3-sdk";
import env from "./env";
export type emailType = {
senderEmail: string;
appname: string;
html: string;
subject: string;
receiverEmails: { email: string }[];
plaintext: string;
triggers?: {
beforeEmail?: () => void;
afterEmail?: () => void;
};
};
const SibApiKey = env?.SIB_APIKEY;
const defaultClient = SibApiV3Sdk.ApiClient.instance;
const apiKey = defaultClient.authentications["api-key"];
apiKey.apiKey = SibApiKey;
async function sendEmail(config: emailType) {
if (!SibApiKey) {
throw new Error("Sib Api Key is not defined");
}
const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();
const sendSmtpEmail = {
sender: {
email: config.senderEmail,
name: "Pastekey.io"
},
to: config.receiverEmails,
subject: config.subject,
htmlContent: config.html,
};
try {
if (config.triggers && config.triggers.beforeEmail) {
config.triggers.beforeEmail();
}
const result = await apiInstance.sendTransacEmail(sendSmtpEmail);
if (config.triggers && config.triggers?.afterEmail) {
config.triggers?.afterEmail();
}
return {
msg: "mail has been processed",
data: result?.messageId,
status: "success",
};
} catch (error) {
return {
msg: "mail sending failed",
status: "failed",
data: {},
};
}
}
export default sendEmail;