Use provider-native features
Send a Postmark template, turn on SendGrid sandbox mode, or pick an SES configuration set per send without leaving the unified client.
The unified message covers what every provider shares. For features one provider has and others don’t, such as stored templates, tracking toggles, unsubscribe groups, or test modes, pass native on the send. Its keys are adapter names:
import { createEmailClient } from "@opencoredev/email-sdk";
import { postmark } from "@opencoredev/email-sdk/postmark";
import { resend } from "@opencoredev/email-sdk/resend";
const email = createEmailClient({
adapters: [
postmark({ serverToken: process.env.POSTMARK_SERVER_TOKEN! }),
resend({ apiKey: process.env.RESEND_API_KEY! }),
],
fallback: { adapters: ["resend"] },
});
await email.send(
{
from: "Acme <hello@acme.com>",
to: "ada@example.com",
subject: "Welcome to Acme",
text: "Thanks for signing up.",
},
{
native: {
postmark: {
template: {
alias: "welcome",
model: { name: "Ada" },
},
trackOpens: true,
},
},
},
);
Postmark sends its stored welcome template. If the route falls back to Resend (when that happens), Resend sends the inline subject and text, because there’s no resend entry.
How routing treats native options
- Each adapter that is tried receives only its own entry. Postmark options never reach Resend, and Resend options never reach Postmark.
- An adapter with no entry sends exactly what it would send without
native. - TypeScript infers the accepted keys from the adapters you passed to
createEmailClient. An unknown key, a key for an adapter without native options (such as SMTP), or a field from another provider fails type checking. - That check needs a literal name for every adapter, the same as
adapterandfallbackroute names. An unnamedsmtp({ host })written inside theadaptersarray gets the name typestring, and the client then accepts any key. Passname: "smtp"to keep the check. - The SDK doesn’t check the shape of native options at runtime. If a JavaScript caller passes the wrong shape, the provider’s own API returns the error. The only runtime checks are that the
nativemap and each entry in it are built from plain objects and arrays (a class instance throws anEmailValidationError), and the two rules below for thebody,fields, andheadersescape hatches. - The SDK copies
nativewhen the send starts, and each adapter attempt gets its own copy of its entry. Changing an entry in middleware, a hook, or an adapter never changes your object, a retry, or the next adapter in the route. - Existing code annotated with
EmailSendOptions<Name>keeps compiling. That type has nonativefield; to pass native options through your own helper, type the parameter asParameters<typeof email.send>[1].
Templates
The message still needs a subject and html or text. When you set a native template, the adapter sends the template in place of the inline body, and the inline fields stay the content for other adapters in the route.
- Resend:
template: { id, variables, useTemplateSubject }replaceshtmlandtext. The messagesubjectoverrides the template’s default subject; setuseTemplateSubject: trueto keep the template’s. - Postmark:
template: { id }or{ alias }, plusmodelandinlineCss, sends to/email/withTemplatewithoutSubject,HtmlBody, orTextBody. - SendGrid:
templateIdanddynamicTemplateDatareplacecontent. Dynamic templates take their subject from the template. - SES:
template: { name }or{ arn }, plusdata, replacesContent.Simple. Headers and attachments are still sent. - Mailgun:
template: { name, version, variables, renderText }replaceshtmlandtext. Mailgun still uses the message subject. - Brevo:
templateIdandparamsreplacehtmlContentandtextContent. The messagesubjectoverrides the template’s subject; setuseTemplateSubject: trueto keep the template’s. - Loops: sends are always template-based.
transactionalIdoverrides the adapter’stransactionalIdfor this send, anddataVariablesadds data variables.
With sendPersonalized, a SendGrid dynamic template gets each recipient’s variables merged over dynamicTemplateData in that recipient’s personalization.
Other options by provider
Each adapter module exports its options type, for example PostmarkNativeOptions from @opencoredev/email-sdk/postmark.
- Resend:
topicId - Postmark:
messageStream(overrides the adapter option),trackOpens,trackLinks,tag - SendGrid:
asm(groupId,groupsToDisplay),ipPoolName,batchId,sandboxMode,tracking(clicks,opens,subscription) - SES:
configurationSetName(overrides the adapter option),listManagement(contactListName,topicName),feedbackForwardingEmailAddress - Mailgun:
tracking,trackingClicks,trackingOpens,testMode,requireTls,skipVerification,ipPool - Brevo:
batchId - Loops:
addToAudience
Sandbox and test modes (sendgrid.sandboxMode, mailgun.testMode) make the provider validate the request without delivering it. The send still resolves successfully. sendPersonalized builds its accepted list from the recipients it submitted, so with either mode on that list names people who were validated, not sent to. Telemetry doesn’t count these sends as provider volume. Both modes apply to that adapter alone: if the request fails and the route falls back, the next adapter sends for real, so leave fallback off for sandbox sends.
Fields the SDK doesn’t model
Resend, Postmark, SendGrid, SES, Brevo, and Loops accept two escape hatches in their entry, and so do Iterable, Mailchimp Transactional, MailerSend, MailPace, Mailtrap, Plunk, Scaleway, Sequenzy, SparkPost, Unosend, and ZeptoMail:
bodyis shallow-merged over the request body the adapter builds. Its top-level keys replace the SDK’s, sobody.mail_settingsreplaces the whole object thatsandboxModewould set.headersadds HTTP headers to the provider API request. These are not email headers; usemessage.headersfor those. Native headers only add names. The send throws anEmailValidationErrorbefore calling the provider when a native header names, in any letter case, a header the adapter sets itself: its credentials,Content-Type, or a name from the adapter’s ownheadersoption. Resend also rejectsIdempotency-Keyon every send, even one without anidempotencyKey, because that option owns the header; passidempotencyKeyinstead, which also gives eachsendPersonalizedrecipient its own key. The same goes forContent-Length,Transfer-Encoding, andHost, which frame the request body. Mailgun’sContent-Typecarries the multipart boundary that fetch generates, and SES signs each request with SigV4, so SES also rejectsX-Amz-Date,X-Amz-Security-Token, andX-Amz-Content-Sha256.
import { createEmailClient } from "@opencoredev/email-sdk";
import { mailersend } from "@opencoredev/email-sdk/mailersend";
const email = createEmailClient({
adapters: [mailersend({ apiKey: process.env.MAILERSEND_API_KEY! })],
});
await email.send(
{
from: "Acme <hello@acme.com>",
to: "ada@example.com",
subject: "Welcome to Acme",
text: "Thanks for signing up.",
},
{
native: {
mailersend: {
body: { template_id: "zr6ke4n6kzon4eqy" },
headers: { "X-Request-Id": crypto.randomUUID() },
},
},
},
);
Mailgun sends multipart form data, so its escape hatch is fields instead of body. Each field replaces the SDK’s value for that name, and an array value appends one field per item: fields: { "o:dkim": "no" }.
sendPersonalized rejects body and fields with an EmailValidationError before anything is sent. Either one can replace the recipients, such as SendGrid’s personalizations or Mailgun’s to, and the result’s accepted list would then name people the provider never received. Typed options and headers still work in personalized sends. To use body or fields, call send() for each recipient.
A send() with body or fields doesn’t count the message’s recipients as submitted in telemetry, since the passthrough may have replaced them.
SMTP, Cloudflare, Microsoft Graph, Helo, Sendheron, JetEmail, Primitive, Lettermint, and Lettr don’t take native options yet.
Batches
sendMany takes native both on the batch options and on each item. An item’s entry for an adapter replaces the batch entry for that adapter. Entries for other adapters still come from the batch.
await email.sendMany(
[
{ message: first },
{ message: second, options: { native: { postmark: { tag: "vip" } } } },
],
{ native: { postmark: { messageStream: "broadcast" } } },
);
The second item’s Postmark entry is { tag: "vip" } only, so it uses the adapter’s default stream.
Native options on a custom adapter
Declare the options type as the fourth type argument of EmailAdapter, and read it from context.native. Use a type alias, not an interface, so it fits the JSON-compatible constraint.
import type { EmailAdapter } from "@opencoredev/email-sdk";
type AcmeNativeOptions = { priority?: "high" | "low" };
export const acme: EmailAdapter<"acme", unknown, unknown, AcmeNativeOptions> = {
name: "acme",
capabilities: {
repeatedHeaders: true,
idempotency: "none",
scheduling: false,
personalized: "expanded",
},
async send(message, context) {
const priority = context.native?.priority ?? "low";
// Call the Acme API with `priority`.
return { adapter: "acme" };
},
};
Fallbacks and retries
How a route moves from one adapter to the next.
Adapter contract
The full EmailAdapter type and context.