SendEmail
The SendEmail method sends a transactional email to a user.
Usage
err := client.SendEmail(ctx, payload)
Parameters
| Name | Type | Description |
|---|---|---|
ctx | context.Context | Context for the request. |
payload | cdp.EmailPayload | Struct containing email details. |
EmailPayload Struct
| Field | Type | Required | Description |
|---|---|---|---|
To | string | ✅ | Recipient email address. |
Identifiers | cdp.Identifiers | ✅ | User identifiers (exactly one of ID, Email, or CioID). |
TransactionalMessageID | string | ID of the template (required if not sending raw). | |
MessageData | map[string]interface{} | Data to populate template variables. | |
Body | string | Raw HTML body (required if no template). | |
Subject | string | Email subject (required if no template). | |
From | string | Sender email address (required if no template). | |
ReplyTo | string | Reply-to email address. | |
BCC | []string | List of BCC email addresses. | |
CC | []string | List of CC email addresses. | |
Attachments | map[string]string | Filename to base64 content. Max 5 files, 2 MB decoded in total. |
Validation Rules
Tois required and must be a valid emailIdentifiersmust contain exactly one of:ID,Email, orCioID- When not using a template:
Body,Subject, andFromare all required - All email fields (
From,BCC,CC,ReplyTo) are validated for proper format
Attachments
Attach and AttachFile base64-encode the content for you:
payload := cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{ID: "user-123"},
TransactionalMessageID: "INVOICE_EMAIL",
}
if err := payload.AttachFile("./invoice.pdf"); err != nil { // named "invoice.pdf"
return err
}
payload.Attach("notes.txt", []byte("Plain text content"))
err := client.SendEmail(ctx, payload)
Content that is already base64 can be set on payload.Attachments directly. SendEmail validates attachments before sending: at most 5 files, at most 2 MB decoded in total, filenames without /, \ or .., and non-empty base64 content. The content type is inferred from the file extension.
Unsupported Fields Warning
The SDK accepts the following fields for future compatibility, but they are not yet processed by the backend:
SendAt,Tracked,HeadersDisableMessageRetention,SendToUnsubscribed,QueueDraftDisableCSSPreprocessing,FakeBCC,ReplyTo,Preheader
Using these fields will trigger a warning log.
Dual-Write Behavior
Email NOT Forwarded to Customer.io
Emails are not sent to Customer.io even when dual-write is enabled. This prevents duplicate emails.
Examples
Using a Template
payload := cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{ID: "user_123"},
TransactionalMessageID: "welcome_email",
MessageData: map[string]interface{}{
"firstName": "Alice",
"loginLink": "https://example.com/login",
},
}
if err := client.SendEmail(context.Background(), payload); err != nil {
log.Println("Error sending email:", err)
}
Raw HTML Email
payload := cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{ID: "user_123"},
Subject: "Hello!",
Body: "<html><body><h1>Hello Alice</h1></body></html>",
From: "[email protected]",
}
if err := client.SendEmail(context.Background(), payload); err != nil {
log.Println("Error sending email:", err)
}