Skip to content

Instantly share code, notes, and snippets.

@jhinrichsen
Created April 12, 2018 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhinrichsen/d23c1641253a93e0b3404b5a97b1a1c8 to your computer and use it in GitHub Desktop.
Save jhinrichsen/d23c1641253a93e0b3404b5a97b1a1c8 to your computer and use it in GitHub Desktop.
Minimal email commandline client
package main
import (
"bytes"
"fmt"
"log"
"net/smtp"
"os"
"os/user"
)
func die(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
user, err := user.Current()
die(err)
hostname, err := os.Hostname()
die(err)
// Connect to the remote SMTP server.
c, err := smtp.Dial("<your SMTP server in here>:25")
die(err)
defer c.Close()
// Set the sender and recipient.
recipient := fmt.Sprintf("%s@%s", user.Username, hostname)
if err := c.Mail(recipient); err != nil {
log.Fatal(err)
}
if err := c.Rcpt("<recipient in here>"); err != nil {
log.Fatal(err)
}
// Send the email body.
wc, err := c.Data()
die(err)
defer wc.Close()
buf := bytes.NewBufferString("This is the email body.")
if _, err = buf.WriteTo(wc); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment