Tutorial

Kirim email dengan Go (net/smtp) lewat SMTP

Library standar sudah cukup — tidak perlu dependensi pihak ketiga.

Oleh tim Sendcorex·Diperbarui 21 Sep 2026·7 menit baca

Opsi 1: net/smtp

Library standar Go sudah menyediakan semua yang Anda butuhkan. Email HTML memerlukan header MIME content-type yang ditulis manual di isi pesan:

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)
    }
}

Opsi 2: REST API dengan 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)

Error umum

GejalaSolusi
535 Authentication failedPastikan API key benar dan dipakai di kedua field PlainAuth.
Pesan tampil sebagai teks biasaHeader Content-Type: text/html harus menjadi bagian dari pesan mentah, sebelum baris kosong.

Kirim email pertama Anda dalam hitungan menit

Buat akun gratis, verifikasi domain Anda, dan tempel kode di atas.

Tanpa kartu kredit