setup STMP from .env

This commit is contained in:
Timur Ercan
2023-04-04 14:20:36 +02:00
parent 4031faec46
commit 0c3305b11d
3 changed files with 50 additions and 23 deletions

View File

@ -11,14 +11,29 @@ export const sendMail = async (
content: string | Buffer;
}[] = []
) => {
if (!process.env.SENDGRID_API_KEY)
throw new Error("Sendgrid API Key not set.");
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."
);
const transport = await nodemailer.createTransport(
nodemailerSendgrid({
apiKey: process.env.SENDGRID_API_KEY || "",
})
);
await transport
.sendMail({
from: process.env.MAIL_FROM,