Email Templates Management
Relevant source files
The following files were used as context for generating this wiki page:
- src/main/java/com/resend/services/templates/Templates.java
- src/main/java/com/resend/services/templates/model/CreateTemplateOptions.java
- src/main/java/com/resend/services/templates/model/UpdateTemplateOptions.java
- src/main/java/com/resend/services/templates/model/GetTemplateResponseSuccess.java
- src/test/java/com/resend/services/templates/TemplatesTest.java
- src/main/java/com/resend/services/emails/model/Template.java
- src/test/java/com/resend/services/util/TemplatesUtil.java
Email Templates Management provides the functionality to create, retrieve, update, and organize reusable email content within the Resend Java SDK. It allows you to define dynamic content using variables, manage template versions through publication, and utilize these templates when sending emails.
The system centers around the Templates service, which interacts with the Resend API to manage template lifecycles. Users can maintain templates in draft or published statuses and reference them by unique identifiers or human-readable aliases.
Sources: src/main/java/com/resend/services/templates/Templates.java, src/main/java/com/resend/services/templates/model/GetTemplateResponseSuccess.java:54-72
Core Service Architecture
The Templates class serves as the primary entry point for all template-related operations. It extends BaseService and performs synchronous HTTP requests to the Resend API.
Service Methods
The service implements the following operations:
- Create: Initializes a new template with specific HTML/text content and metadata.
- Retrieve: Fetches a single template by ID/Alias or lists all available templates.
- Update: Modifies the content or configuration of an existing template.
- Delete: Removes a template from the system.
- Lifecycle Management: Specific actions to duplicate existing templates or publish draft versions.
Sources: src/main/java/com/resend/services/templates/Templates.java:16-121
Template Operation Flow
This diagram illustrates the typical lifecycle of a template from creation to usage in an email.
The flow shows that a template typically starts in a draft state and must be published before it can be effectively utilized for delivery. Sources: src/main/java/com/resend/services/templates/Templates.java:118-121, src/test/java/com/resend/services/templates/TemplatesTest.java:160-172
Data Models and Configuration
Template management utilizes specific option classes to ensure type-safe interactions with the API.
Template Definition Fields
The system uses CreateTemplateOptions and UpdateTemplateOptions to define template properties.
| Field | Type | Description |
|---|---|---|
name | String | A descriptive name for the template. |
alias | String | A unique human-readable identifier. |
from | String | Default sender address. |
subject | String | Default email subject line. |
html | String | The HTML content, supporting variable placeholders. |
text | String | The plain text version of the content. |
variables | List | Definitions of dynamic placeholders and fallback values. |
Sources: src/main/java/com/resend/services/templates/model/CreateTemplateOptions.java:15-38, src/main/java/com/resend/services/templates/model/UpdateTemplateOptions.java:15-38
Variables and Dynamic Content
Templates support variables that are replaced at sending time. Each Variable consists of a key, a type (e.g., STRING, NUMBER), and an optional fallback value used when no data is provided during the email send request.
Sources: src/test/java/com/resend/services/util/TemplatesUtil.java:18-36
Integration with Email Sending
When sending an email, you can reference a published template instead of providing raw HTML or text. This is handled by the com.resend.services.emails.model.Template class.
Sending Logic
- Reference the template by its unique
id. - Provide a map of
variablesto populate the placeholders defined in the template. - If a template is provided in
CreateEmailOptions, the SDK prohibits includinghtml,text, orreactin the same payload.
Sources: src/main/java/com/resend/services/emails/model/CreateEmailOptions.java:557-567, src/main/java/com/resend/services/emails/model/Template.java:13-36
Template Class Relationship
The following class diagram shows the relationship between template management models and the email sending component.
Lifecycle Operations
Publication and Duplication
Templates remain in a draft status until the publish method is called. Once published, the published_at timestamp is populated. The duplicate operation creates a new template based on an existing one's content, which is useful for versioning or creating variations.
Sources: src/main/java/com/resend/services/templates/Templates.java:108-121, src/main/java/com/resend/services/templates/model/GetTemplateResponseSuccess.java:82-90
Retrieval and Filtering
The list method supports pagination using ListParams. You can retrieve a specific template using its UUID or the alias assigned during creation.
Sources: src/main/java/com/resend/services/templates/Templates.java:54-81
Conclusion
Email Templates Management in the Resend Java SDK provides a robust framework for decoupling email content from application logic. By utilizing the Templates service and its associated models, developers can manage complex dynamic content, handle template versioning through publication, and streamline email delivery through reusable template identifiers and variable injection.