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.

Advertisements

About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s