JavaScript jobs and emails
Schedule JavaScript jobs and send custom email through PocketBase mail delivery.
Use this guide to run recurring JavaScript work and send custom messages from PocketBase hooks, routes, or middleware. You will register a job, confirm where to inspect it, configure mail delivery, and send a message with the configured sender.
Before you begin
You need a PocketBase application with JavaScript extensions enabled and access to Dashboard > Settings > Mail settings. A job runs in the process started by the serve command, so keep that process running while you test it. Mail delivery uses either the sendmail command or an SMTP client, depending on the configured mail settings.
Schedule a job
Add cronAdd to a JavaScript extension file that PocketBase loads:
cronAdd("cleanup-demo", "*/2 * * * *", () => {
console.log("cleanup job ran")
})The first argument is the job ID, the second is a cron expression, and the third is the handler. This example runs every two minutes.
Start the application with serve, then open Dashboard > Settings > Crons. The registered job appears there, and the dashboard can preview or trigger app-level cron jobs.
Remove the job by its ID with cronRemove("cleanup-demo"). Use the same ID when you intentionally replace an existing job.
Send a custom email
Configure delivery in Dashboard > Settings > Mail settings, then send a MailerMessage through $app.newMailClient():
onRecordCreateRequest((e) => {
e.next()
const message = new MailerMessage({
from: { address: e.app.settings().meta.senderAddress, name: e.app.settings().meta.senderName },
to: [{ address: e.record.email() }],
subject: "Welcome to Example App",
html: "<p>Your account was created.</p>",
})
e.app.newMailClient().send(message)
}, "users")Messages can also include cc, bcc, and custom headers. The client selects sendmail or SMTP from the mail settings.
The sender values come from the application mail settings and the recipient comes from the created record. Use a test recipient while checking delivery, and leave the e.next() call in the hook so the original request continues.
Troubleshoot delivery
If a message is not delivered, verify Mail settings and the sender address first. For system messages such as password reset or verification, edit templates under Dashboard > Collections > Edit collection > Options, or use the applicable mailer hook.
If the job is missing from Settings > Crons, confirm that the extension file was loaded by the running serve process and that the job ID is exactly cleanup-demo. If a job appears but does not run, keep the serving process running and use the dashboard preview or trigger controls. If mail still does not arrive, confirm whether the configured client uses sendmail or SMTP, then correct that delivery configuration before testing again.
Next steps
Continue with JavaScript event hooks or scheduled jobs administration.