title | description | features | testCoverage | |||||
---|---|---|---|---|---|---|---|---|
Emails (Postmark) |
Simple module to send transaction emails in your backend service |
|
100 |
The Emails (Postmark) Module enables you to send transactional and bulk emails using Postmark. It’s designed for secure, efficient email operations within your backend service. This module doesn’t expose endpoints by default but can easily be adapted to include them as needed.
To add the Emails (Postmark) Module to your project, run:
npx vratix add postmark-email
Add the following environment variable to your .env
file:
- POSTMARK_SERVER_TOKEN: Postmark server token required to authenticate email sending
- Default: None (required)
- Example:
POSTMARK_SERVER_TOKEN=xxxx-xxxxx-xxxx-xxxxx-xxxxxx
The module requires the auth-basic
API module for user management. It will be automatically installed with this module if not already present.
Here is an example of using the sendEmail
controller. If needed, you can pass a custom postmark.ServerClient
instance when creating the emailController
:
import { createEmailController } from "@/controllers/email.js";
import { createUserRepository } from "@/repositories/user.js";
const userRepository = createUserRepository();
const emailController = createEmailController(userRepository);
type ExamplePayload = {
foo: string;
};
emailController.sendEmail<ExamplePayload>({
receiverId: 123,
templateName: "exampleTemplate",
payload: { foo: "bar" },
});
To ensure strongly typed payloads, you can pass a generic type to sendEmail
and sendBulkEmails
:
type SendEmail<T> = {
receiverId: number;
templateName: string;
payload: T;
};
You can also define payload types in /src/controllers/email.ts
like this:
type ExampleTemplatePayload = {
foo: string;
};
type EmailPayload = ExampleTemplatePayload | object;
type SendEmail = {
receiverId: number;
templateName: string;
payload: EmailPayload;
};
For bulk emails with customized content for each recipient, modify the types as follows:
type SendBulkEmails<T> = {
templateName: string;
payload: Omit<SendEmail<T>, "templateName">[];
};
NOTE: Adjust the controller logic to handle the
payload
array if sending unique data to multiple recipients.
Below are common errors with solutions for this module:
Error Code | Name | Solution |
---|---|---|
404 | EmailNotFound | Ensure the user has an email associated with their account |
500 | SendEmailError | An issue occurred with the Postmark SDK. Verify your Postmark server token is correct |
If you have configured DMARK for your domain you can skip this part.
To enhance deliverability and security, follow these steps to configure DMARC for your email domain. DMARC (Domain-based Message Authentication, Reporting, and Conformance) helps protect against email spoofing and ensures that emails sent from your domain are authenticated. Postman's What is DMARC
- SPF (Sender Policy Framework): Define the servers authorized to send emails for your domain.
- Add an SPF record to your DNS settings:
v=spf1 include:spf.postmarkapp.com ~all
- Add an SPF record to your DNS settings:
- DKIM (DomainKeys Identified Mail): Enable DKIM to sign outgoing messages.
- In Postmark, go to your domain settings to find the DKIM keys and add them to your DNS records.
- Create a DMARC TXT record in your DNS settings. Here’s a basic example:
Host: _dmarc.yourdomain.com
Value: "v=DMARC1; p=none; rua=mailto:dmarc-reports@yourdomain.com"
- Tip: Start with
p=none
to monitor your DMARC results before enforcing stricter policies.