emailer-kit
Version:
emailer-kit is a versatile and easy-to-use Node.js utility that simplifies email sending using Nodemailer. With a streamlined interface, it provides a set of functions to effortlessly send HTML emails, making it an ideal toolkit for integrating email func
86 lines (84 loc) • 3.08 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
emailer: () => emailer
});
module.exports = __toCommonJS(src_exports);
var import_nodemailer = __toESM(require("nodemailer"));
var validateEnvVariables = () => {
const requiredEnvVariables = ["NODEMAIL_SERVICE", "NODEMAIL_EMAIL", "NODEMAIL_PASSWORD"];
for (const variable of requiredEnvVariables) {
if (!process.env[variable]) {
throw new Error(`Missing required environment variable: ${variable}`);
}
}
};
var validateEmailAddress = (email) => {
const emailRegex = /\S+@\S+\.\S+/;
if (!emailRegex.test(email)) {
throw new Error("Invalid email address");
}
};
var createTransporter = () => {
validateEnvVariables();
const config = {
service: process.env.NODEMAIL_SERVICE,
auth: {
user: process.env.NODEMAIL_EMAIL,
pass: process.env.NODEMAIL_PASSWORD
}
};
return import_nodemailer.default.createTransport(config);
};
var emailer = async (emailerOptions) => {
const { email, subject, htmlContent, file } = emailerOptions;
validateEmailAddress(email);
try {
const transporterInstance = createTransporter();
const mailOptions = {
from: process.env.NODEMAIL_EMAIL || "",
to: email,
subject,
html: htmlContent,
attachments: file ? [{ path: file.path, filename: file.name }] : void 0
};
const info = await transporterInstance.sendMail(mailOptions);
console.log("Email sent:", info.response);
return info;
} catch (error) {
console.error("Error sending email:", error);
throw error;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
emailer
});