Sendcorex
Tutoriel

Envoyer des e-mails avec Go (net/smtp) en SMTP

La bibliothèque standard suffit — aucune dépendance tierce nécessaire.

Par l'équipe Sendcorex·Mis à jour le 21 sept. 2026·7 min de lecture

Option 1 : net/smtp

La bibliothèque standard de Go fournit tout le nécessaire. Un e-mail HTML a besoin d'un en-tête MIME content-type écrit à la main dans le corps du message :

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 : API REST avec 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)

Erreurs fréquentes

SymptômeCorrection
535 Authentication failedVérifiez que la clé API est correcte et utilisée dans les deux champs de PlainAuth.
Le message s'affiche en texte brutL'en-tête Content-Type: text/html doit faire partie du message brut, avant la ligne vide.

Envoyez votre premier e-mail en quelques minutes

Créez un compte gratuit, vérifiez votre domaine et collez le code ci-dessus.

Sans carte bancaire