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?
| Host | smtp.unitpost.com |
|---|---|
| Username | unitpost(always this literal value) |
| Password | Your Unitpost API key(must carry emails:send) |
| Port (STARTTLS) | 587(or 2587 if blocked) |
| Port (implicit TLS) | 465(or 2465 if blocked) |
| From address | Any address on a verified domain |
How do I set it up?
Send with net/smtp
Gopackage 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.
Verify delivery
The message appears in Activity.