UNPKG

nodemailer-mailchimp-transactional

Version:

Mailchimp transactional transporter for nodemailer

165 lines (161 loc) 5.79 kB
"use strict"; 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 name2 in all) __defProp(target, name2, { get: all[name2], 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); // index.ts var index_exports = {}; __export(index_exports, { default: () => index_default }); module.exports = __toCommonJS(index_exports); var import_mailchimp_transactional = __toESM(require("@mailchimp/mailchimp_transactional")); // package.json var name = "nodemailer-mailchimp-transactional"; var version = "1.0.6"; // index.ts var MailchimpTransport = class { name; version; client; options; constructor(options) { if (!options.apiKey) { throw new Error("Mailchimp Transactional API key is required"); } this.options = options; this.name = name; this.version = version; this.client = (0, import_mailchimp_transactional.default)(options.apiKey); } send(mail, callback) { mail.normalize((err, source) => { if (source.template) { this.sendWithTemplate(source, callback); } else { return this.sendStandardEmail(source, callback); } }); } sendWithTemplate(source, callback) { const message = this.buildMailchimpMessage(source); const mailchimpTemplateMsg = { template_name: source.template?.template_name || "", template_content: source.template?.template_content || [], message, ip_pool: source.ip_pool, async: source.async, send_at: source.send_at, outputFormat: source.outputFormat }; this.client.messages.sendTemplate(mailchimpTemplateMsg).then((response) => { if (response instanceof Error) { callback(response); } else { callback(null, response); } }); } sendStandardEmail(source, callback) { const message = this.buildMailchimpMessage(source); const mailchimpMsg = { message, ip_pool: source.ip_pool, async: source.async, send_at: source.send_at, outputFormat: source.outputFormat }; this.client.messages.send(mailchimpMsg).then((response) => { if (response instanceof Error) { callback(response); } else { callback(null, response); } }); } buildMailchimpMessage(source) { const msg = { from_email: this.options.senderMail ?? typeof source.from === "string" ? source.from : source.from?.address ?? "", from_name: this.options.senderName ?? typeof source.from === "string" ? source.from : source.from?.address ?? "", subject: source.subject ?? "", text: source.text ?? void 0, html: source.html ?? void 0, to: [].concat(source.to ?? []).map((entry) => ({ email: entry.address, name: entry.name ?? "", type: "to" })), headers: source.headers ?? {}, important: source.priority, track_opens: source.track_opens, track_clicks: source.track_clicks, auto_text: source.auto_text, auto_html: source.auto_html, inline_css: source.inline_css, url_strip_qs: source.url_strip_qs, preserve_recipients: source.preserve_recipients, view_content_link: source.view_content_link, tracking_domain: source.tracking_domain, signing_domain: source.signing_domain, return_path_domain: source.return_path_domain, merge: source.merge ?? !!(source.merge_vars || source.global_merge_vars), merge_language: source.merge_language ?? "mailchimp", tags: source.tags, subaccount: source.subaccount, metadata: source.metadata ?? {}, recipient_metadata: source.recipient_metadata ?? [], google_analytics_domains: source.google_analytics_domains ?? [], google_analytics_campaign: source.google_analytics_campaign ?? void 0 }; if (source.cc) { const ccRecipients = [].concat(source.cc).map((entry) => ({ email: entry.address, name: entry.name ?? "", type: "cc" })); msg.to = msg.to.concat(ccRecipients); } if (source.bcc) { const bccRecipients = [].concat(source.bcc).map((entry) => ({ email: entry.address, name: entry.name ?? "", type: "bcc" })); msg.to = msg.to.concat(bccRecipients); } if (source.merge_vars) { msg.merge_vars = source.merge_vars; } if (source.global_merge_vars) { msg.global_merge_vars = source.global_merge_vars; } return msg; } }; var MailchimpTransactionalTransport = (options) => { return new MailchimpTransport(options); }; var index_default = MailchimpTransactionalTransport;