SMTP quickstart
Open a connection
Use WorkerMailer.connect() when you want to reuse a connection for one or more sends.
import { WorkerMailer } from '@workermailer/smtp';
const mailer = await WorkerMailer.connect({
host: 'smtp.acme.com',
port: 587,
secure: false,
startTls: true,
authType: 'plain',
credentials: {
username: 'alerts@acme.com',
password: 'super-secret'
}
});
Send a message
await mailer.send({
from: { name: 'Acme Alerts', email: 'alerts@acme.com' },
to: { name: 'Alice', email: 'alice@example.com' },
subject: 'Deployment finished',
text: 'Your deployment completed successfully.',
html: '<p>Your deployment completed <strong>successfully</strong>.</p>'
});
Close the connection
After the queued work is done, close the connection explicitly.
await mailer.close();
One-off sends
If you do not need to keep the connection around, use the static helper:
await WorkerMailer.send(
{
host: 'smtp.acme.com',
port: 587,
credentials: {
username: 'alerts@acme.com',
password: 'super-secret'
}
},
{
from: 'alerts@acme.com',
to: 'alice@example.com',
subject: 'Hello',
text: 'This is a one-off send.'
}
);
Recommended Worker pattern
A clean production flow is:
- build mailer options from
env - validate the payload before sending
- use
WorkerMailer.send()for simple one-off jobs - switch to queues when the send should not sit on the request path
Useful next steps
- Add Attachments and inline images
- Wire observability with Hooks and errors
- Move send work to Queues