Video icon 64
Learn to Code. Start your free trial today.
Advertisement

Send Email Newsletters via Command Line With Postman

by
Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →

If you're wanting to start a new email newsletter—or find a better way to send out your existing newsletter—there's quite a few tools for you. There's MailChimp and many other online email services, as well as tools built into Office apps like Word and Outlook's Mail Merge and a similar feature with new Google Docs add-ons. But if you're used to coding your own sites and using the terminal, there's a better option: the terminal app Postman.

In this tutorial, I'll show you how to effectively use Postman to send your email newsletters via any SMTP server: your own server or email account, or services like Amazon SES or Mandrill. We’ll cover creating a basic recipient list, creating a basic email template, different mail sending options, and finally what a full example looks like. With those building block examples under your belt, you'll be able to send any email newsletter the way you want.

Getting Started With Postman

This tutorial is going to assume you are comfortable installing support for the go programming language in your operating system environment and setting a couple path related environment variables.

I will briefly demonstrate how to get this up and running on your Mac, but please reference the documentation I linked to above before moving on—and note that you can get it running using similar steps on any Linux computer, and with a bit more effort on a Windows PC.

Installation on OS X

One prerequisite for installation on OS X is homebrew. Please read the documentation and setup homebrew before moving forward.

Now that you have homebrew all setup and up to date, run the brew command to install go. I’ll show all the output you should see below.

[chadhs@mac ~]$ brew install go
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/go-1.2.2.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring go-1.2.2.mavericks.bottle.tar.gz
==> Caveats
As of go 1.2, a valid GOPATH is required to use the `go get` command:
  http://golang.org/doc/code.html#GOPATH

`go vet` and `go doc` are now part of the go.tools sub repo:
  http://golang.org/doc/go1.2#go_tools_godoc

To get `go vet` and `go doc` run:
  go get code.google.com/p/go.tools/cmd/godoc
  go get code.google.com/p/go.tools/cmd/vet

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completion has been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/go/1.2.2: 3981 files, 115M

Now that you have go support installed, lets setup our environment.

[chadhs@mac ~]$ mkdir $HOME/go
[chadhs@mac ~]$ export GOPATH=$HOME/go
[chadhs@mac ~]$ export PATH=$PATH:$GOPATH/bin

To continue using postman or other utilities requiring go, we need to make these environment settings persistent. You’ll need to add them to your shell’s rc or profile file that gets loaded when launching a new terminal session. I’ve yet to join the ranks of the zsh folks full-time, so here’s what I put into my .bashrc after PATH is defined.

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Now let’s install postman using go.

[chadhs@mac ~]$ go get github.com/zachlatta/postman

Run the postman command after installation completes, to verify you’ve done everything correctly. You should see the following output.

[chadhs@mac ~]$ postman
Postman is a utility for sending batch emails.

Usage:

  postman [flags]

Flags:

  -attach      attach a list of comma separated files
  -c           number of concurrent requests to have
  -csv         path to csv of contact list
  -debug       print emails to stdout instead of sending
  -html        html template path
  -password    smtp password
  -port        port of smtp server
  -sender      email to send from
  -server      url of smtp server
  -subject     subject of email
  -text        text template path
  -user        smtp username

Creating a basic recipient list

The next thing you need is a list of contacts to send your email to. For our example you’ll need to create a blank text file named recipients.csv. In that text file you’ll enter “Email, Name, Type” on the first line, and then all of your contacts following that format, one per line. Be sure to save the file once you’re done checking that everything has been entered correctly.

Email,Name,Type
chadhs@example.com,Chad Stovern,Human
info@techcraft.example.com,Techcraft Workshop,Robot
cstovern@work.example.com,Chad Wokerson,Human

Creating basic html & text email templates

Now that you have your list of email recipients created, let’s make two email templates that will hold our message. One template will be an html version and the other will be a text version to make plain text geeks like me happy. Feel free to deviate from copying and pasting my exact examples below. Play around with the wording and where you include the special “{{.Name}}” and “{{.Type}} entries. Those entries are ”variables" that will fill in the name and type from your recipients.csv contact list.

Here is about what your html version should look like:

<h1>Hello, {{.Name}} the {{.Type}}!</h1>

<h2>
We'd like to take this opportunity to congratulate you on being
featured as one of our top 25 {{.Type}} customers!
</h2>

<h3>
Click here to claim your <a href="http://rewards.example.com/freeoffer">FREE Reward</a>
</h3>

<p>
<strong>
Sincerely,<br>
Fake Marketing Robot
</strong>
</p>

Here is approximately what your plain text version should look like:

Hello, {{.Name}} the {{.Type}}!

We'd like to take this opportunity to congratulate you on being
featured as one of our top 25 {{.Type}} customers!

Click here to claim your FREE Reward: http://rewards.example.com/freeoffer

Sincerely,
Fake Marketing Robot

Sending options to consider

Now that you have all the required pieces assembled, there’s one more piece to consider before we start sending mail. For tutorial purposes, using your gmail or other personal account to send mail is just fine. When you put this into practice “in the real world” you’ll want to weigh some options.

At a minimum, be sure you’re sending mail from the account you want people to see when the recieve your message. I could think of far worse advice than having a special marketing@example.com or customerservice@example.com address to send such messages from. The other thing to consider is email limits imposed by your provider. There may be limits on how many different external email addresses you can send to in any one particular 24 hour period. If your list is 500 or less you’re likely to be fine using your current email service to send out a weekly email campaign. If you want to do much more than this using postman as a tool, I would seriously look into a service such as sendgridAmazon SES or Mandrill to send your emails. Just signup for an account with any of the respective services and use their server settings with postman.

Gathering a Few Technical Details

The last piece of the puzzle is gathering some information about the mail account you’re going to use to send your email. You’ll need to know the following items:

  • email address
  • server name (such as: smtp.example.com)
  • server port (such as: 587)
  • user name (usually your email address)
  • password

You will need these values handy before moving on and sending email.

Putting it All Together

Now let’s take all our work and do a live test. Run the same example I have below, substituting in your own information in place of mine. Be sure to pick a solid sounding subject line that matches the content of your message, but doesn’t send off sirens in a users head to reach straight for the delete button. You’re looking for the magic words “Emailed recipient X of X…” to appear, confirming your success. If postman just hangs indefinitely or you see another error, be sure to check your email server settings (user, password, server, port) again for any errors and then run postman again.

[chadhs@mac postman]$ postman -html template.html -text template.txt -csv recipients.csv -sender "Chad Stovern <chadhs@example.com>" -subject "A FREE gift for being our customer!" -server smtp.example.com -port 587 -user chadhs@example.com -password "nice-try-not-my-password"
Emailed recipient 3 of 3...

If everything worked on the command-line, wait for the emails to come in. You’ll know you followed our example correctly if you get the same email content, but the name and type fields were filled in appropriately with the values from your recipient list.


Example Email 1
Example email generated via postman

And another example, using the same email customized for another recipient:

Example Email 2
Example email generated via postman

Conclusion

With those building block examples under your belt, the possibilities are limitless. You can now spend time building some great reusable templates that you own and control, outside of any hosted marketing service. I highly recommend you stroll on over to Tuts+ web design for some great ideas on how to start building out and beautifying those templates.

If you have any further questions on using postman or are stuck and at your whit’s end; let me know in the comments below. As always, thanks for reading!

Advertisement