Files
sign/packages/lib/mail/sendMail.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-02-21 17:42:30 +01:00
import { ReadStream } from "fs";
2023-01-26 16:20:48 +01:00
import nodemailer from "nodemailer";
import nodemailerSendgrid from "nodemailer-sendgrid";
2023-02-21 17:42:30 +01:00
export const sendMail = async (
to: string,
subject: string,
body: string,
attachments: {
filename: string;
content: string | Buffer;
}[] = []
) => {
2023-04-04 14:20:36 +02:00
let transport;
if (process.env.SENDGRID_API_KEY)
transport = nodemailer.createTransport(
nodemailerSendgrid({
apiKey: process.env.SENDGRID_API_KEY || "",
})
);
if (process.env.SMTP_MAIL_HOST)
transport = nodemailer.createTransport({
host: process.env.SMTP_MAIL_HOST || "",
port: Number(process.env.SMTP_MAIL_PORT) || 587,
auth: {
user: process.env.SMTP_MAIL_USER || "",
pass: process.env.SMTP_MAIL_PASSWORD || "",
},
});
if (!transport)
throw new Error(
"No valid transport for NodeMailer found. Probably Sendgrid API Key nor SMTP Mail host was set."
);
2023-01-26 16:20:48 +01:00
await transport
.sendMail({
from: process.env.MAIL_FROM,
to: to,
subject: subject,
html: body,
2023-02-21 17:42:30 +01:00
attachments: attachments,
})
.catch((err) => {
throw err;
});
2023-01-26 16:20:48 +01:00
};