Tutorial

Send Email with Go (net/smtp) via SMTP

The standard library is enough — no third-party dependency required.

By the Sendcorex team·Updated Sep 21, 2026·7 min read

Option 1: net/smtp

Go's standard library ships everything you need. HTML mail needs a MIME content-type header written into the message body manually:

package main

import (
    "net/smtp"
    "os"
)

func main() {
    apiKey := os.Getenv("SENDCOREX_API_KEY")
    auth := smtp.PlainAuth("", apiKey, apiKey, "smtp.sendcorex.com")

    to := []string{"customer@example.com"}
    msg := []byte("To: customer@example.com\r\n" +
        "From: Your App <hello@yourdomain.com>\r\n" +
        "Subject: Welcome!\r\n" +
        "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
        "<h1>You are in.</h1>")

    err := smtp.SendMail("smtp.sendcorex.com:587", auth, "hello@yourdomain.com", to, msg)
    if err != nil {
        panic(err)
    }
}

Option 2: REST API with net/http

body := []byte(`{"to":"customer@example.com","from":"hello@yourdomain.com","subject":"Welcome!","html":"<h1>You are in.</h1>"}`)

req, _ := http.NewRequest("POST", "https://graph.sendcorex.com/v3.0/emails", bytes.NewBuffer(body))
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)

Common errors

SymptomFix
535 Authentication failedConfirm the API key is correct and used for both fields in PlainAuth.
Message renders as plain textThe Content-Type: text/html header must be part of the raw message, before the blank line.

Send your first email in minutes

Create a free account, verify your domain and paste in the code above.

No credit card required