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 'to=destination@example.com&toname=Destination&subject=Example Subject&text=testingtextbody&from=info@domain.com&api_user=your_sendgrid_username&api_key=your_sendgrid_password' https://api.sendgrid.com/api/mail.send.json

If you want to clean it up with variables:

#!/bin/sh
SGTO=receiver@example.com
SGTONAME='Some Name'
SGSUBJECT='Email Subject'
SGFROM=from@example.com
SGTEXT='Email Text'
SGUSER=user
SGPASS=password
curl -d "to=${SGTO}&toname=${SGTONAME}&subject=${SGSUBJECT}&text=${SGTEXT}&from=${SGFROM}&api_user=${SGUSER}&api_key=${SGPASS}" https://api.sendgrid.com/api/mail.send.json

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

The following two tabs change content below.
avatar
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!

    Reply
  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

    Reply
  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

    Reply

Leave a Reply to Ron Lobo Cancel reply

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

Current day month ye@r *

Loading...

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