Use Go's SMTP packages to send email through the Lettermint SMTP relay.
This guide covers SMTP integration. For full configuration options and available headers, see the SMTP Introduction .
Basic configuration
Use environment variables for credentials:
import " os "
const (
SMTPHost = "smtp.lettermint.co"
Username = "lettermint"
)
var APIToken = os. Getenv ( "LETTERMINT_PROJECT_TOKEN" )
Advanced features
Multiple recipients
Send to multiple recipients with CC and BCC:
func sendEmailMultipleRecipients ( to , cc , bcc [] string , subject , body string ) error {
from := "sender@yourdomain.com"
apiToken := os. Getenv ( "LETTERMINT_PROJECT_TOKEN" )
// Build recipient list for headers
toHeader := strings. Join (to, ", " )
ccHeader := strings. Join (cc, ", " )
headers := fmt. Sprintf (
"From: %s\r\n " +
"To: %s\r\n " +
"Cc: %s\r\n " +
"Subject: %s\r\n " +
"Content-Type: text/html; charset=UTF-8 \r\n " +
" \r\n " ,
from, toHeader, ccHeader, subject,
)
msg := [] byte (headers + body + " \r\n " )
auth := smtp. PlainAuth ( "" , Username, apiToken, SMTPHost)
// All recipients (to + cc + bcc) receive the email
allRecipients := append ( append (to, cc ... ), bcc ... )
return smtp. SendMail (
SMTPHost + ":587" ,
auth,
from,
allRecipients,
msg,
)
}
// Usage
sendEmailMultipleRecipients (
[] string { "user1@example.com" , "user2@example.com" },
[] string { "manager@yourdomain.com" },
[] string { "archive@yourdomain.com" },
"Monthly Newsletter" ,
"<h1>This Month's Updates</h1>" ,
)
Attachments
Send emails with file attachments:
import (
" encoding/base64 "
" fmt "
" io/ioutil "
" mime "
" net/smtp "
" os "
" path/filepath "
" strings "
)
func sendEmailWithAttachment ( to , subject , body , filePath string ) error {
from := "sender@yourdomain.com"
apiToken := os. Getenv ( "LETTERMINT_PROJECT_TOKEN" )
// Read file
fileContent, err := ioutil. ReadFile (filePath)
if err != nil {
return err
}
fileName := filepath. Base (filePath)
mimeType := mime. TypeByExtension (filepath. Ext (filePath))
if mimeType == "" {
mimeType = "application/octet-stream"
}
boundary := "boundary123"
var msg strings . Builder
msg. WriteString (fmt. Sprintf ( "From: %s\r\n " , from))
msg. WriteString (fmt. Sprintf ( "To: %s\r\n " , to))
msg. WriteString (fmt. Sprintf ( "Subject: %s\r\n " , subject))
msg. WriteString (fmt. Sprintf ( "MIME-Version: 1.0 \r\n " ))
msg. WriteString (fmt. Sprintf ( "Content-Type: multipart/mixed; boundary= %s\r\n " , boundary))
msg. WriteString ( " \r\n " )
// HTML body
msg. WriteString (fmt. Sprintf ( "-- %s\r\n " , boundary))
msg. WriteString ( "Content-Type: text/html; charset=UTF-8 \r\n " )
msg. WriteString ( " \r\n " )
msg. WriteString (body)
msg. WriteString ( " \r\n " )
// Attachment
msg. WriteString (fmt. Sprintf ( "-- %s\r\n " , boundary))
msg. WriteString (fmt. Sprintf ( "Content-Type: %s\r\n " , mimeType))
msg. WriteString ( "Content-Transfer-Encoding: base64 \r\n " )
msg. WriteString (fmt. Sprintf ( "Content-Disposition: attachment; filename= \"%s\"\r\n " , fileName))
msg. WriteString ( " \r\n " )
msg. WriteString (base64.StdEncoding. EncodeToString (fileContent))
msg. WriteString ( " \r\n " )
msg. WriteString (fmt. Sprintf ( "-- %s -- \r\n " , boundary))
auth := smtp. PlainAuth ( "" , Username, apiToken, SMTPHost)
return smtp. SendMail (
SMTPHost + ":587" ,
auth,
from,
[] string {to},
[] byte (msg. String ()),
)
}
Add Lettermint-specific headers for tags, metadata, and routing:
func sendEmailWithHeaders ( to , subject , body string ) error {
from := "sender@yourdomain.com"
apiToken := os. Getenv ( "LETTERMINT_PROJECT_TOKEN" )
msg := [] byte (fmt. Sprintf (
"From: %s\r\n " +
"To: %s\r\n " +
"Subject: %s\r\n " +
"Content-Type: text/html; charset=UTF-8 \r\n " +
"X-LM-Tag: order-confirmation \r\n " +
"X-LM-Tags: campaign=orders, environment=production \r\n " +
"X-LM-Tags: variant=B \r\n " +
"X-LM-Metadata-order_id: 12345 \r\n " +
"X-LM-Metadata-customer_id: cust_789 \r\n " +
"X-Lettermint-Route: transactional \r\n " +
" \r\n " +
" %s\r\n " ,
from, to, subject, body,
))
auth := smtp. PlainAuth ( "" , Username, apiToken, SMTPHost)
return smtp. SendMail (
SMTPHost + ":587" ,
auth,
from,
[] string {to},
msg,
)
}
X-LM-Tag is the singular tag. X-LM-Tags contains structured pairs. A raw message can contain more than one X-LM-Tags field.
Metadata headers are extracted by Lettermint and included in webhook payloads, but not added to the actual email sent to recipients.
See the SMTP Introduction for full details on available headers.
Using third-party libraries
With gomail
For a simpler API, use the gomail library:
go get gopkg.in/gomail.v2
package main
import (
" os "
" gopkg.in/gomail.v2 "
)
func sendEmailWithGomail () error {
m := gomail. NewMessage ()
m. SetHeader ( "From" , "sender@yourdomain.com" )
m. SetHeader ( "To" , "recipient@example.com" )
m. SetHeader ( "Subject" , "Test Email" )
m. SetBody ( "text/html" , "<h1>Hello!</h1><p>This is a test email.</p>" )
// Add Lettermint headers
m. SetHeader ( "X-LM-Tag" , "test-email" )
m. SetHeader ( "X-LM-Metadata-campaign" , "test" )
// Add attachments
m. Attach ( "/path/to/document.pdf" )
d := gomail. NewDialer ( "smtp.lettermint.co" , 587 , "lettermint" , os. Getenv ( "LETTERMINT_PROJECT_TOKEN" ))
return d. DialAndSend (m)
}