nodemailer-mailchimp-transactional
Version:
Mailchimp transactional transporter for nodemailer
134 lines (131 loc) • 4.26 kB
JavaScript
// index.ts
import MailchimpTransactional from "@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 = MailchimpTransactional(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;
export {
index_default as default
};