Templates & Topics
Relevant source files
The following files were used as context for generating this wiki page:
Templates and Topics provide structural and organizational layers for email communication within the Resend Rust SDK. Templates allow you to create reusable email layouts with dynamic variables, while Topics enable audience segmentation by categorizing interest groups for subscription management.
These modules interact with the core Resend client to handle resource lifecycle management, including creation, retrieval, updates, and deletion. They also integrate with Emails for message delivery and Contacts for subscription tracking.
Sources: src/templates.rs:17, src/topics.rs:18, src/lib.rs:114-118
Email Templates
The TemplateSvc manages email layouts stored on Resend servers. Templates support versioning through a draft/published status system and utilize variables for dynamic content injection during email transmission.
Sources: src/templates.rs:18-20, src/templates.rs:250-254
Template Lifecycle and Operations
The following diagram illustrates the lifecycle of a template from creation to publication or duplication.
The SDK provides methods to move templates through these states via the /templates endpoints.
Sources: src/templates.rs:24-87
Template Data Structure
A Template object contains metadata and the actual content (HTML/Text) used for rendering emails.
| Field | Type | Description |
|---|---|---|
id | TemplateId | Unique identifier for the template. |
name | String | The display name of the template. |
status | TemplateEvent | Current state: draft or published. |
html | Option<String> | The HTML content of the template. |
variables | Vec<Variable> | List of dynamic placeholders. |
from | Option<String> | Default sender address. |
subject | Option<String> | Default email subject line. |
Sources: src/templates.rs:222-237
Dynamic Variables
Templates use Variable structures to define placeholders. Each variable has a key, a type (String or Number), and an optional fallback_value.
Sources: src/templates.rs:114-120, src/templates.rs:188-193
Subscription Topics
The TopicsSvc manages categories used to segment audiences. Topics allow contacts to express preferences for specific types of communication, such as newsletters or product updates.
Sources: src/topics.rs:19-21
Topic Visibility and Subscription Types
Topics define how contacts interact with them on unsubscribe pages.
- Visibility: Can be
Public(visible to everyone) orPrivate(visible only to opted-in contacts). - Default Subscription: Determines if new contacts are
OptInorOptOutby default.
Sources: src/topics.rs:109-114, src/topics.rs:141-147
Topic Architecture
The relationship between Topics, Contacts, and Emails ensures that users only receive relevant content.
Integration Details
Using Templates in Emails
When sending an email, you reference a template by its ID or alias. You provide a HashMap<String, serde_json::Value> to satisfy the variables defined in the template.
Sources: src/emails.rs:373-376, src/batch.rs:219-232
Managing Contact Topics
Contacts are linked to topics via UpdateContactTopicOptions. You can retrieve a list of topic subscriptions for a specific contact or update their status (e.g., from OptIn to OptOut).
Sources: src/contacts.rs:105-132, src/contacts.rs:500-503
Event Processing
System events track changes to these entities. For example, topic.created, topic.updated, and topic.deleted events are emitted when topic resources change. Similarly, contact.topics.updated tracks subscription changes for specific users.
Sources: src/events.rs:260-272, src/events.rs:445-455
Summary
The Templates and Topics modules allow developers to separate content design and audience preference management from core delivery logic. Templates provide a flexible way to manage dynamic content, while Topics provide the necessary infrastructure for compliant and relevant audience segmentation.
Sources: src/templates.rs, src/topics.rs