Email Templates
Using transactional templates is the recommended way to send emails with Conviso. It separates content from code and allows for easier updates.
Sending a Template Email
To send an email using a template, specify the TransactionalMessageID and provide MessageData for any variables defined in your template.
package main
import (
"context"
"log"
"github.com/codematic/opencdp-go/cdp"
)
func sendWelcome(client *cdp.Client) {
payload := cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{
ID: "u_999",
},
// This ID comes from your Conviso dashboard
TransactionalMessageID: "welcome-series-1",
// Variables to replace in the template (e.g., {{firstName}})
MessageData: map[string]interface{}{
"firstName": "John",
"accountType": "Premium",
"supportLink": "https://help.example.com",
},
}
if err := client.SendEmail(context.Background(), payload); err != nil {
log.Printf("Failed to send welcome email: %v", err)
}
}
Sending Raw HTML (Advanced)
If you need to generate HTML programmatically or don't want to use stored templates, you can send raw HTML.
func sendCustomReport(client *cdp.Client, htmlContent string) {
payload := cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{
Email: "[email protected]",
},
Subject: "Weekly Report",
From: "[email protected]",
Body: htmlContent, // Raw HTML string
ReplyTo: "[email protected]",
}
if err := client.SendEmail(context.Background(), payload); err != nil {
log.Printf("Failed to send raw email: %v", err)
}
}
Scheduling Emails
You can schedule emails to be sent in the future using the SendAt field (Unix timestamp).
import "time"
func sendTomorrow(client *cdp.Client) {
tomorrow := time.Now().Add(24 * time.Hour).Unix()
payload := cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{ID: "u_1"},
TransactionalMessageID: "reminder",
SendAt: tomorrow,
}
client.SendEmail(context.Background(), payload)
}