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:

The Go source is as follows:

// Copyright 2013 - by Jim Lawless
// License: MIT / X11
// See:
// Bear with me ... I'm a Go noob.

package main

import (

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")

   fmt.Printf("GSend v 1.01 by Jim Lawless\n")


   if flag.NFlag() != 5 {

   body := "To: " + *to + "\r\nSubject: " +
      *subject + "\r\n\r\n" + *msg
   auth := smtp.PlainAuth("",*from,*pwd,"")
   err := smtp.SendMail("",auth,*from,
   if err != nil {

GSend accepts five mandatory flags:

GSend v 1.01 by Jim Lawless
        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 -f -p yourpassword -s "Testing!" -m "This is a test"

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


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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s