Sending GMail with Go (Golang)

I needed a command-line e-mail sender utility that would send e-mail via my GMail account. I wanted the solution to work on Windows and OS/X for the time being.

My solution is a utility named GSend.

GitHub repo: https://github.com/jimlawless/gsend/

The Go source is as follows:

// Copyright 2013 - by Jim Lawless
// License: MIT / X11
// See: http://www.mailsend-online.com/license2013.php
//
// Bear with me ... I'm a Go noob.
 
package main
 
import (
   "log"
   "flag"
   "net/smtp"
   "fmt"
)
 
func main() {
   to := flag.String("t","","destination Internet mail address")
   from := flag.String("f","","the sender's GMail address")
   pwd := flag.String("p","","the sender's password")
   subject := flag.String("s","","subject line of email")
   msg := flag.String("m","","a one-line email message")
   flag.Usage=func() {
      fmt.Printf("Syntax:\n\tGSend [flags]\nwhere flags are:\n")
      flag.PrintDefaults()
   }
 
   fmt.Printf("GSend v 1.01 by Jim Lawless\n")
 
   flag.Parse()
 
   if flag.NFlag() != 5 {
      flag.Usage()
      return
   }
 
   body := "To: " + *to + "\r\nSubject: " +
      *subject + "\r\n\r\n" + *msg
   auth := smtp.PlainAuth("",*from,*pwd,"smtp.gmail.com")
   err := smtp.SendMail("smtp.gmail.com:587",auth,*from,
      []string{*to},[]byte(body))
   if err != nil {
      log.Fatal(err)
   }
}

GSend accepts five mandatory flags:

GSend v 1.01 by Jim Lawless
Syntax:
        GSend [flags]
where flags are:
  -f="": the sender's GMail address
  -m="": a one-line email message
  -p="": the sender's password
  -s="": subject line of email
  -t="": destination Internet mail address

To send an email with GSend, use a command-line similar to the following (all on a single line ):

gsend -t to@some.place -f you@gmail.com -p yourpassword -s "Testing!" -m "This is a test"

The recipient should see an email with your specified subject and one line of text.

Update 24 September 2022 I have been thinking about fleshing this utility out with more robust email options. Stay tuned.