Adds email-templates and emails collections to Payload for authoring rich-text templates and tracking delivery status. Emails are sent through a registered process-email job task with retries, scheduling and priorities, using the configured Payload email adapter. Requires Payload 3.37 or newer and the Lexical editor package; the collections deny all access until you grant it.
Template-based email system with scheduling and job processing for PayloadCMS
Installation
npm i @xtr-dev/payload-mailing1
Stars
0
Forks
0
Open Issues
1 weeks ago
Last Updated
613
Weekly Downloads
v0.9.0
npm Version
234.3 KB
Unpacked Size
0
Dependencies
10 since Sep 6
| Date | Stars |
|---|---|
| Dec 5, 2026 | 1 |
| Dec 4, 2026 | 1 |
| Dec 3, 2026 | 1 |
| Dec 2, 2026 | 1 |
| Dec 1, 2026 | 1 |
| Nov 30, 2026 | 1 |
| Nov 29, 2026 | 1 |
| Nov 28, 2026 | 1 |
| Nov 27, 2026 | 1 |
| Nov 26, 2026 | 1 |
| Nov 25, 2026 | 1 |
| Nov 24, 2026 | 1 |
| Nov 23, 2026 | 1 |
| Nov 22, 2026 | 1 |
| Nov 21, 2026 | 1 |
| Nov 20, 2026 | 1 |
| Nov 19, 2026 | 1 |
| Nov 18, 2026 | 1 |
| Nov 17, 2026 | 1 |
| Nov 16, 2026 | 1 |
| Nov 15, 2026 | 1 |
| Nov 14, 2026 | 1 |
| Nov 13, 2026 | 1 |
| Nov 12, 2026 | 1 |
| Nov 11, 2026 | 1 |
| Nov 10, 2026 | 1 |
| Nov 9, 2026 | 1 |
| Nov 8, 2026 | 1 |
| Nov 7, 2026 | 1 |
| Nov 6, 2026 | 1 |
| Nov 5, 2026 | 1 |
| Nov 4, 2026 | 1 |
| Nov 3, 2026 | 1 |
| Nov 2, 2026 | 1 |
| Nov 1, 2026 | 1 |
| Oct 31, 2026 | 1 |
| Oct 30, 2026 | 1 |
| Oct 29, 2026 | 1 |
| Oct 28, 2026 | 1 |
| Oct 27, 2026 | 1 |
| Oct 26, 2026 | 1 |
| Oct 25, 2026 | 1 |
| Oct 24, 2026 | 1 |
| Oct 23, 2026 | 1 |
| Oct 22, 2026 | 1 |
| Oct 21, 2026 | 1 |
| Oct 20, 2026 | 1 |
| Oct 19, 2026 | 1 |
| Oct 18, 2026 | 1 |
| Oct 17, 2026 | 1 |
| Oct 16, 2026 | 1 |
| Oct 15, 2026 | 1 |
| Oct 14, 2026 | 1 |
| Oct 13, 2026 | 1 |
| Oct 12, 2026 | 1 |
| Oct 11, 2026 | 1 |
| Oct 10, 2026 | 1 |
| Oct 9, 2026 | 1 |
| Oct 8, 2026 | 1 |
| Oct 7, 2026 | 1 |
| Oct 6, 2026 | 1 |
| Oct 5, 2026 | 1 |
| Oct 4, 2026 | 1 |
| Oct 3, 2026 | 1 |
| Oct 2, 2026 | 1 |
| Oct 1, 2026 | 1 |
| Sep 30, 2026 | 1 |
| Sep 29, 2026 | 1 |
| Sep 28, 2026 | 1 |
| Sep 27, 2026 | 1 |
| Sep 26, 2026 | 1 |
| Sep 25, 2026 | 1 |
| Sep 24, 2026 | 1 |
| Sep 23, 2026 | 1 |
| Sep 22, 2026 | 1 |
| Sep 21, 2026 | 1 |
| Sep 20, 2026 | 1 |
| Sep 19, 2026 | 1 |
| Sep 18, 2026 | 1 |
| Sep 17, 2026 | 1 |
| Sep 16, 2026 | 1 |
| Sep 15, 2026 | 1 |
| Sep 14, 2026 | 1 |
| Sep 13, 2026 | 1 |
| Sep 12, 2026 | 1 |
| Sep 11, 2026 | 1 |
| Sep 10, 2026 | 1 |
| Sep 9, 2026 | 1 |
| Sep 8, 2026 | 1 |
| Sep 7, 2026 | 1 |
| Sep 6, 2026 | 1 |
781+753 since Sep 6
| Date | Weekly downloads |
|---|---|
| Dec 5, 2026 | 781 |
| Dec 4, 2026 | 802 |
| Dec 3, 2026 | 799 |
| Dec 2, 2026 | 759 |
| Dec 1, 2026 | 850 |
| Nov 30, 2026 | 895 |
| Nov 29, 2026 | 667 |
| Nov 28, 2026 | 540 |
| Nov 27, 2026 | 362 |
| Nov 26, 2026 | 393 |
| Nov 25, 2026 | 423 |
| Nov 24, 2026 | 322 |
| Nov 23, 2026 | 246 |
| Nov 22, 2026 | 149 |
| Nov 21, 2026 | 99 |
| Nov 20, 2026 | 101 |
| Nov 19, 2026 | 97 |
| Nov 18, 2026 | 138 |
| Nov 17, 2026 | 161 |
| Nov 16, 2026 | 208 |
| Nov 15, 2026 | 212 |
| Nov 14, 2026 | 214 |
| Nov 13, 2026 | 258 |
| Nov 12, 2026 | 291 |
| Nov 11, 2026 | 253 |
| Nov 10, 2026 | 237 |
| Nov 9, 2026 | 189 |
| Nov 8, 2026 | 183 |
| Nov 7, 2026 | 191 |
| Nov 6, 2026 | 168 |
| Nov 5, 2026 | 104 |
| Nov 4, 2026 | 71 |
| Nov 3, 2026 | 56 |
| Nov 2, 2026 | 55 |
| Nov 1, 2026 | 51 |
| Oct 31, 2026 | 34 |
| Oct 30, 2026 | 10 |
| Oct 29, 2026 | 9 |
| Oct 28, 2026 | 6 |
| Oct 27, 2026 | 5 |
| Oct 26, 2026 | 17 |
| Oct 25, 2026 | 47 |
| Oct 24, 2026 | 99 |
| Oct 23, 2026 | 99 |
| Oct 22, 2026 | 100 |
| Oct 21, 2026 | 103 |
| Oct 20, 2026 | 106 |
| Oct 19, 2026 | 95 |
| Oct 18, 2026 | 62 |
| Oct 17, 2026 | 11 |
| Oct 16, 2026 | 11 |
| Oct 15, 2026 | 69 |
| Oct 14, 2026 | 110 |
| Oct 13, 2026 | 138 |
| Oct 12, 2026 | 153 |
| Oct 11, 2026 | 168 |
| Oct 10, 2026 | 167 |
| Oct 9, 2026 | 169 |
| Oct 8, 2026 | 113 |
| Oct 7, 2026 | 72 |
| Oct 6, 2026 | 42 |
| Oct 5, 2026 | 26 |
| Oct 4, 2026 | 12 |
| Oct 3, 2026 | 24 |
| Oct 2, 2026 | 28 |
| Oct 1, 2026 | 25 |
| Sep 30, 2026 | 29 |
| Sep 29, 2026 | 37 |
| Sep 28, 2026 | 38 |
| Sep 27, 2026 | 37 |
| Sep 26, 2026 | 27 |
| Sep 25, 2026 | 21 |
| Sep 24, 2026 | 72 |
| Sep 23, 2026 | 66 |
| Sep 22, 2026 | 58 |
| Sep 21, 2026 | 109 |
| Sep 20, 2026 | 111 |
| Sep 19, 2026 | 108 |
| Sep 18, 2026 | 110 |
| Sep 17, 2026 | 61 |
| Sep 16, 2026 | 62 |
| Sep 15, 2026 | 61 |
| Sep 14, 2026 | 11 |
| Sep 13, 2026 | 9 |
| Sep 12, 2026 | 13 |
| Sep 11, 2026 | 17 |
| Sep 10, 2026 | 16 |
| Sep 9, 2026 | 18 |
| Sep 8, 2026 | 20 |
| Sep 7, 2026 | 21 |
| Sep 6, 2026 | 28 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1055702159-root)<a href="https://payloaddirectory.dev/plugins/1055702159-root"><img src="https://payloaddirectory.dev/badge/1055702159-root.svg" alt="Payload Directory"></a>README
Template-based email for PayloadCMS 3.x — templates, layouts, scheduling, and job-queue processing, all through Payload collections you already know.
v0.x — minor versions may contain breaking changes until 1.0.
pnpm add @xtr-dev/payload-mailing @payloadcms/richtext-lexical
@payloadcms/richtext-lexical is a required peer dependency: the templates
collection uses it as the default rich-text editor. If your app already uses
the Lexical editor (Payload's default), you have it installed already.
Add the plugin plus an email adapter:
import { buildConfig } from 'payload'
import { mailingPlugin } from '@xtr-dev/payload-mailing'
import { nodemailerAdapter } from '@payloadcms/email-nodemailer'
export default buildConfig({
email: nodemailerAdapter({ defaultFromAddress: '[email protected]', transport: { /* SMTP */ } }),
plugins: [
mailingPlugin({
defaultFrom: '[email protected]',
collections: {
// Required — collections deny all access by default. See "Access Control".
// Grant staff only: sent emails contain sign-in links and other per-recipient secrets.
emails: { access: { read: ({ req: { user } }) => user?.role === 'admin' } },
templates: { access: { read: ({ req: { user } }) => user?.role === 'admin' } },
},
}),
],
})
This adds two collections under a Mailing admin group: email-templates
(author templates) and emails (delivery + status tracking).
The mailing collections deny every operation by default. Payload's built-in default grants access to any authenticated user, which in an app with front-end/non-admin users would expose email content, recipients, and templates. Until you grant access explicitly, no one can read or manage these collections via the REST/GraphQL API or admin panel — but sending still works, since the plugin sends through Payload's Local API (which bypasses access control).
Grant access via the collection overrides. Your functions are merged on top of the deny-all default, so any operation you don't set stays denied:
mailingPlugin({
collections: {
emails: {
access: {
read: ({ req: { user } }) => user?.role === 'admin',
create: ({ req: { user } }) => user?.role === 'admin',
update: ({ req: { user } }) => user?.role === 'admin',
delete: ({ req: { user } }) => user?.role === 'admin',
},
},
// templates: { access: { … } }
},
})
Keep emails staff-only. Each sent email stores its variables and rendered
body, which often hold sign-in links, confirmation links and one-time codes, so
granting read to every signed-in user lets any account take over another.
Granting create lets any account send mail from your domain. Your app's own
code sends through sendEmail (Local API) and needs no access rule.
access is the standard Payload access control
API. The same override object also accepts custom fields, hooks, admin, etc.
import { sendEmail } from '@xtr-dev/payload-mailing'
// From a template
await sendEmail(payload, {
template: { slug: 'welcome-email', variables: { firstName: 'John' } },
data: {
to: '[email protected]',
scheduledAt: new Date(Date.now() + 3600_000), // optional: send later
priority: 1, // optional: 1 = highest
},
})
// Or with your own content
await sendEmail(payload, {
data: { to: '[email protected]', subject: 'Hi', html: '<h1>Hi</h1>' },
})
Emails are queued and sent in the background (see Jobs). Pass
processImmediately: true to send synchronously, or queue to route a single
send's job onto a named queue (e.g. 'high') instead of the plugin's queue
option.
With processImmediately, sendEmail resolves (with the sent email) only
when the email actually went out. If the email was created and queued but not
sent inline (the delivery failed, e.g. your SMTP server is down, or the job
could not be run), sendEmail throws an ImmediateProcessingError. The email is
not lost: it is back to pending and is retried by your job runner after
retryDelay, like any failed send (see Jobs). The error's email
(with its current status and error) and jobId tell you which email it was.
If "queued is fine" for your caller, catch the error and treat the send as
queued instead of failed:
import { ImmediateProcessingError, sendEmail } from '@xtr-dev/payload-mailing'
try {
await sendEmail(payload, { template: { slug: 'invite' }, data: { to }, processImmediately: true })
} catch (error) {
if (!(error instanceof ImmediateProcessingError)) throw error
// Created and queued, not lost: error.email.id will be retried by the queue.
}
Any other error (validation, template, database) means no email was created.
If that inline try was the email's last allowed attempt (retryAttempts: 1),
error.email.status is failed and it is not retried. An email with a future
scheduledAt is not sent inline and does not throw; it stays pending until
it is due.
Author templates in the admin (Mailing → Email Templates): a slug,
subject, rich-text content, and optional declared variables. Reference
data with {{ }}, e.g. Hello {{ user.name }}! or
{{ createdAt | formatDate: "long" }}.
templateEngine to liquidjs (default), mustache, or
simple, or supply templateRenderer: (tpl, vars) => string for your own.{{ x | raw }} / Mustache {{{ x }}}); subject and text are verbatim.
LiquidJS {% echo x %} is escaped the same way as {{ x }}, literals
included; write {% echo x | raw %} for intentional HTML. Use | raw only
on values you trust. This needs liquidjs 10.30.1 or newer, which the
plugin installs; older versions wrote echo output unescaped.perPart({ html, text }) to give
the HTML body one value and the plain-text body (and subject) another, e.g. a
link button as markup in HTML and as Log in: https://… in text. The HTML
value is still escaped unless you use | raw / {{{ }}}.{{siteUrl}}/account
or {{resetUrl}}. Leave out spaces inside the braces (the link field rejects
URLs with spaces). The value is HTML-escaped but its scheme is not checked, so
pass only trusted URLs.formatDate ("short", "long" or default),
formatCurrency and capitalize. Set locale (e.g. 'nl-NL'), currency
(e.g. 'EUR') and timeZone (e.g. 'Europe/Amsterdam') in the plugin
config, or override per use: {{ d | formatDate: "long", "nl-NL" }},
{{ total | formatCurrency: "EUR", "nl-NL" }}.user.name are
checked as nested paths. Opt-in; declare nothing to skip checks.Render without sending: renderTemplate(payload, slug, vars) →
{ html, text, subject }.
Define reusable wrappers once and inject a template's body at {{ content }}:
mailingPlugin({
layouts: {
branded: { html: `<html><body><main>{{ content }}</main></body></html>` },
},
defaultLayout: 'branded', // applied to templates that don't pick their own
})
Templates then get a Layout select (with Use default / None). Layout
variables are always HTML-escaped; content is injected without double-encoding.
Fully opt-in — with no layouts, templates render exactly as before.
The plugin registers its process-email task automatically. Sending (or
creating an emails doc) queues one job per email on the plugin's queue,
delayed until scheduledAt when set. You need a job runner: Payload's
jobs.autoRun cron for that queue, or your own payload.jobs.run() calls.
Emails move through pending → processing → sent / failed. A send error puts
the email back to pending and queues a new process-email job (on the same
queue) that runs once retryDelay has passed, so your job runner retries it
without extra code. After retryAttempts tries (default 3) the email is
marked failed and is not sent again unless you ask for it with
retryFailedEmails(payload).
processEmails(payload) sends up to 50 due pending emails per call (highest
priority, then oldest first, skipping emails whose last failed try is more
recent than retryDelay); call it from a scheduled task if you don't run a job
runner, or as a safety net. An email is only ever sent by the run that claims
it, so a retry job and processEmails never both send it. An email left
processing by a crashed process is released by processEmails /
retryFailedEmails once processingTimeout has passed, as a failed attempt.
processAllEmails(payload) is the one call to put on a schedule: it runs
processEmails, then retries failed emails that are below retryAttempts
again (which only happens after you raise that option). Emails that used up
their attempts stay failed and are skipped on every later run.
To queue an email from a Payload job or workflow, register the
schedule-email task from the /tasks subpath:
import { ScheduleEmailTask } from '@xtr-dev/payload-mailing/tasks'
export default buildConfig({ jobs: { tasks: [ScheduleEmailTask] } })
await payload.jobs.queue({ task: 'schedule-email', input: { to: '[email protected]', templateSlug: 'welcome' } })
| Option | Description |
|---|---|
defaultFrom / defaultFromName | Default sender for emails that don't set one. |
retryAttempts | Maximum send attempts per email (default 3). The attempt that reaches it marks the email failed; automatic retries and processAllEmails then leave it alone. |
retryDelay | Wait (ms) after a failed send before it is retried (default 300000). |
processingTimeout | Time (ms) after which an email stuck in processing (its process crashed mid-send) is released for a retry (default 900000). Keep it above your slowest send: a send still running past it may go out twice. |
queue | Job queue name (default 'default'). A single sendEmail call can override it with its own queue option. |
templateEngine | 'liquidjs' | 'mustache' | 'simple'. |
templateRenderer | Custom (tpl, vars) => string | Promise<string>. |
strictTemplates | true makes a template parse/render error throw a TemplateRenderError (so sendEmail rejects) instead of falling back to plain {{ name }} substitution. Default false. |
layouts / defaultLayout | Named layout wrappers and the default one. |
locale / currency / timeZone | Defaults for the formatDate / formatCurrency filters (otherwise en-US / USD / server time zone). |
adminPreview | Live in-admin render preview; true by default. Set false to skip it (and the payload generate:importmap step). |
collections | Rename ('slug') or override ({ access, fields, … }) the emails/templates collections. |
beforeSend | (options, email) => options hook to mutate the send just before delivery. |
resolveInternalLink | (fields) => url for rich-text links to other documents (fields.doc.relationTo / fields.doc.value). Without it, internal links render as plain text. |
richTextEditor | Custom Lexical editor config for the templates collection's content field, passed through as RichTextField['editor']. |
initOrder | 'before' (default) sets up payload.mailing before the host app's own onInit runs; 'after' runs the host's onInit first. |
| Export | Purpose |
|---|---|
sendEmail(payload, options) | Queue (or immediately send) a template/direct email. |
renderTemplate(payload, slug, vars) | Render { html, text, subject } without sending. |
processEmails(payload) | Send up to 50 due pending emails. |
processAllEmails(payload) | processEmails, then retryFailedEmails limited to emails with attempts left. Never retries an email that used up retryAttempts, so it is safe to schedule. |
retryFailedEmails(payload, options?) | Manual retry: give up to 20 failed emails one more try each, oldest first, once retryDelay has passed since their last attempt, including emails that used up retryAttempts. If that try fails too, the email stays failed. Don't schedule it as is (it would retry forever); pass { onlyWithAttemptsLeft: true } to skip emails that reached retryAttempts. |
getMailing(payload) | Get the mailing context (service, config, slugs). |
escapeHtml(str) / safeUrl(url) | Escape text for HTML / allow only http(s), mailto and relative URLs. |
perPart({ html, text }) | Give a variable separate HTML and plain-text values. |
ImmediateProcessingError / TemplateRenderError | Thrown by processImmediately sends that were queued but not sent inline / by strictTemplates render errors. |
sendEmail<Email>(…) is generic over your generated Email type for type-safe
custom fields.
PayloadCMS ^3.37.0 · Node.js ^18.20.2 || >=20.9.0
@payloadcms/ui and react are optional peers, used only by the admin
preview (adminPreview); a Payload app with the admin panel already has them.
Issues and PRs welcome at the repository; see DEVELOPMENT.md for local setup. MIT licensed.
Topics
plugin emails
Transactional emails defined in code, with copy editors can change, preview and test-send from the admin.
7029/wk10v3
payload usesend
Payload email adapter that sends messages through the useSend API, formerly Unsend
82621/wk8v3 v4
payload unsend
Email adapter that sends Payload CMS emails through the Unsend REST API
062/wk3v3
groundmail payload
Payload email adapter that sends mail through the Groundmail HTTP API
78405/wk0v?
payload order emails
Transactional emails on Payload order creation and status changes, with overridable templates
7333/wk1v3
eusend payload
Payload email adapter that sends transactional email through the eusend REST API from the EU
72230/wk1v3 v4