How to send email from the command line with SendGrid

Sending email should be simple. It's something I really don't like thinking about.

I ran into an issue where I wanted to send email from the command line in a bash script that helps power Review Signal. A notification when something goes wrong.

The only problem, I don't have any mailing program installed. I also don't really want to send email from my servers because making sure they get through isn't something I want to spend a lot of time thinking about.

The first solution that jumped to mind was SendGrid, who I had the pleasure of trying out their API at PayPal BattleHack DC. It was dead simple. I had integrated it into PHP though. I didn't know if it worked from the command line.

I checked the docs and found they had a rest API.

curl -d ' Subject&text=testingtextbody&'

If you want to clean it up with variables:

SGTONAME='Some Name'
SGSUBJECT='Email Subject'
SGTEXT='Email Text'
curl -d "to=${SGTO}&toname=${SGTONAME}&subject=${SGSUBJECT}&text=${SGTEXT}&from=${SGFROM}&api_user=${SGUSER}&api_key=${SGPASS}"

Voila! Sending emails from my bash script is now simple.

The following two tabs change content below.
Kevin Ohashi is the geek-in-charge at Review Signal. He is passionate about making data meaningful for consumers. Kevin is based in Washington, DC.

10 thoughts on “How to send email from the command line with SendGrid

  1. avatarMike Hapner

    Hi – thanks for saving me sifting through the API again… one small thing — in your sample with variables, it should be &text=${SGTEXT} — not &text=${SGSUBJECT} 🙂

    Thanks again!

  2. avatarWill Smidlein

    Hi there!

    I just came across this post (albeit a few months after it was published) and wanted to thank you for sharing! We love hearing that SendGrid is the first thing that comes to peoples’ minds when they think of email! 🙂

    I’d love to send some swag your way if you’re interested! Just drop me an email that I’m commenting with.

    Thanks again, and feel free to drop me a note if you ever need any help!

    Will Smidlein
    Developer Relations, SendGrid

  3. avatarJoe

    Hello there, thanks for the script, I was able to send email via bash. But turns out sendgrid was sending arround 400 emails at one time. It’s like looping email send when i use this script. Do you know about this issue ? Thanks


Leave a Reply

Your email address will not be published. Required fields are marked *

Current day month ye@r *


Interested in seeing which web hosting companies people love (and hate!)? Click here and find out how your web host stacks up.