Skip to content

Go

Emails can be sent from Go applications using the net/smtp package

main.go
package main

import (
    "fmt"
    "net/smtp"
)

func main() {
    // Sender
    from := "[email protected]"

    // Recipient
    to := []string{"[email protected]"}

    // Message
    subject := "Test Email"
    body := "This is a test email."
    message := []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s",
        from, to[0], subject, body))

    // Authentication
    auth := smtp.PlainAuth("", "[mail credential user]", "[mail credential password]", smtpHost)

    // Send email
    err := smtp.SendMail("in.smtp.sendamatic.net:587", auth, from, to, message)
    if err != nil {
        fmt.Printf("Failed to send email: %v\n", err)
        return
    }

    fmt.Println("Email sent successfully!")
}

Third-party clients

Oliver Jakoubek maintains a Sendamatic Go client which supports a fluent message builder interface, file attachments, HTML and plain text emails, and many other useful features.

https://code.beautifulmachines.dev/jakoubek/sendamatic