-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathsendEmail.ts
More file actions
62 lines (56 loc) · 1.57 KB
/
sendEmail.ts
File metadata and controls
62 lines (56 loc) · 1.57 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
52
53
54
55
56
57
58
59
60
61
62
import nodemailer from "nodemailer";
import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2";
import { z } from "zod";
const hasAccessKeys = process.env.ACCESS_KEY && process.env.SECRET_KEY;
const sesClient = new SESv2Client({
region: "eu-west-1",
...(hasAccessKeys
? {
credentials: {
accessKeyId: process.env.ACCESS_KEY || "",
secretAccessKey: process.env.SECRET_KEY || "",
},
}
: {}),
});
// create Nodemailer SES transporter
export const nodemailerSesTransporter = nodemailer.createTransport({
SES: { sesClient, SendEmailCommand },
});
interface MailConfig {
recipient: string;
subject: string;
htmlMessage: string;
}
const sendEmail = async (config: MailConfig) => {
const { recipient, htmlMessage, subject } = config;
if (!htmlMessage.length || !subject.length)
throw new Error(`"htmlMessage" & "subject" required.`);
const emailSchema = z.string().email();
const to = emailSchema.parse(recipient);
// send some mail
return new Promise((resolve, reject) => {
nodemailerSesTransporter.sendMail(
{
from: "[email protected]",
to,
subject,
html: htmlMessage,
},
(err, info) => {
if (err) {
console.log("Error sending mail:", err);
reject(`Error sending mail: ${err}`);
} else {
console.log(info.envelope);
console.log(info.messageId);
resolve({
envelope: info.envelope,
messageId: info.messageId,
});
}
},
);
});
};
export default sendEmail;