Sending Emails
Relevant source files
The following files were used as context for generating this wiki page:
Sending emails is the primary functionality of the Resend Go SDK. The system allows you to send individual messages, batch multiple requests, schedule delivery for a future time, and utilize dynamic templates with variable substitution.
The following sequence diagram illustrates the basic flow of sending an email through the SDK.
Core Email Parameters
The SendEmailRequest structure defines the content and behavior of an email. You must specify the sender and at least one recipient.
| Field | Type | Description |
|---|---|---|
From | string | The sender's email address. |
To | []string | Primary recipient addresses. |
Subject | string | The email subject line. |
Text | string | Plain text version of the message. |
Html | string | HTML version of the message. |
Cc | []string | Carbon copy recipient addresses. |
Bcc | []string | Blind carbon copy recipient addresses. |
ReplyTo | string | The address for recipient replies. |
ScheduledAt | string | ISO 8601 timestamp for future delivery. |
TopicId | string | ID to target specific contact subscriptions. |
Sources: examples/send_email.go:19-30, examples/schedule_email.go:21-27, emails_test.go:424-441
Batch Sending
Batching allows you to send up to 100 emails in a single API call. This improves performance by reducing HTTP overhead. You can choose between two validation modes for batches.
Batch Validation Modes
- Strict Mode (Default): The entire batch fails if any individual email request is invalid.
- Permissive Mode: Valid emails in the batch are sent even if others contain errors. The response includes a list of successful IDs and specific errors for failed indices.
Email Templates
Templates enable the separation of content from logic. You create templates with placeholders using triple curly braces (e.g., {{{userName}}}).
- Variables: You must define variables in the template before use.
- Identification: Use either the
TemplateIdor aTemplateAliasto reference a template in a send request. - Overrides: You can override template defaults (like
FromorSubject) directly in theSendEmailRequest.
Sources: examples/send_email_with_template.go:21-72, emails_test.go:221-253
Attachments
The SDK supports multiple methods for including attachments. You must provide a filename and content type for each attachment.
- Local Files: Read files from the local filesystem and pass the byte content.
- Remote Paths: Provide a URL string to a file hosted elsewhere.
- Inline Attachments: Use
ContentIdto reference images or files directly within the HTML body.
Sources: examples/with_attachments.go:23-45, emails_test.go:111-140
Idempotency and Reliability
To prevent duplicate emails during network retries, use the IdempotencyKey. If you repeat a request with the same key within 24 hours, the API returns the original response without resending the message.
options := &resend.SendEmailOptions{
IdempotencyKey: "unique-key-per-request",
}
sent, err = client.Emails.SendWithOptions(ctx, params, options)
Sources: examples/send_email.go:39-47, emails_test.go:195-218
Scheduling and Cancellation
You can schedule emails for future delivery by providing an ISO 8601 timestamp in the ScheduledAt field.
- Schedule: Set
ScheduledAtin the initialSendEmailRequest. - Update: Change the delivery time for a pending email using
Emails.Update. - Cancel: Stop a scheduled email before it is sent using
Emails.Cancelwith the email ID.
Sources: examples/schedule_email.go:21-48, emails_test.go:41-59
Conclusion
Sending emails via the Resend Go SDK provides flexible options ranging from simple text messages to complex, templated batch operations. By utilizing features like idempotency keys and scheduled delivery, developers can build robust communication workflows while maintaining control over message delivery and formatting.