Send email from Go over SMTP

Go's net/smtp client can STARTTLS to Unitpost on port 587. Username is the literal `unitpost`; password is an API key.

What are the SMTP settings?

Hostsmtp.unitpost.com
Usernameunitpost(always this literal value)
PasswordYour Unitpost API key(must carry emails:send)
Port (STARTTLS)587(or 2587 if blocked)
Port (implicit TLS)465(or 2465 if blocked)
From addressAny address on a verified domain

How do I set it up?

  1. Send with net/smtp

    Go
    package main
    
    import (
        "crypto/tls"
        "net/smtp"
        "os"
    )
    
    func main() {
        host := "smtp.unitpost.com"
        auth := smtp.PlainAuth("", "unitpost", os.Getenv("UNITPOST_API_KEY"), host)
        to := []string{"customer@example.com"}
        msg := []byte("From: you@yourdomain.com\r\n" +
            "To: customer@example.com\r\n" +
            "Subject: Hello from Go\r\n" +
            "\r\n" +
            "Sent via SMTP.\r\n")
    
        c, err := smtp.Dial(host + ":587")
        if err != nil {
            panic(err)
        }
        defer c.Close()
        if err = c.StartTLS(&tls.Config{ServerName: host}); err != nil {
            panic(err)
        }
        if err = c.Auth(auth); err != nil {
            panic(err)
        }
        if err = c.Mail("you@yourdomain.com"); err != nil {
            panic(err)
        }
        if err = c.Rcpt(to[0]); err != nil {
            panic(err)
        }
        w, err := c.Data()
        if err != nil {
            panic(err)
        }
        if _, err = w.Write(msg); err != nil {
            panic(err)
        }
        if err = w.Close(); err != nil {
            panic(err)
        }
        _ = c.Quit()
    }

    Port 587 needs an explicit STARTTLS before AUTH (PLAIN and LOGIN are both accepted). From must be on a verified domain.

  2. Verify delivery

    The message appears in Activity.