Skip to main content

SendEmail

The SendEmail method sends a transactional email to a user.

Usage​

err := client.SendEmail(ctx, payload)

Parameters​

NameTypeDescription
ctxcontext.ContextContext for the request.
payloadcdp.EmailPayloadStruct containing email details.

EmailPayload Struct​

FieldTypeRequiredDescription
Tostring✅Recipient email address.
Identifierscdp.Identifiers✅User identifiers (exactly one of ID, Email, or CioID).
TransactionalMessageIDstringID of the template (required if not sending raw).
MessageDatamap[string]interface{}Data to populate template variables.
BodystringRaw HTML body (required if no template).
SubjectstringEmail subject (required if no template).
FromstringSender email address (required if no template).
ReplyTostringReply-to email address.
BCC[]stringList of BCC email addresses.
CC[]stringList of CC email addresses.
Attachmentsmap[string]stringFilename to base64 content. Max 5 files, 2 MB decoded in total.

Validation Rules​

  • To is required and must be a valid email
  • Identifiers must contain exactly one of: ID, Email, or CioID
  • When not using a template: Body, Subject, and From are 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, Headers
  • DisableMessageRetention, SendToUnsubscribed, QueueDraft
  • DisableCSSPreprocessing, 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)
}