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.
Kevin Ohashi
Latest posts by Kevin Ohashi (see all)
- Analyzing Digital Ocean’s First Major Move with Cloudways - February 28, 2023
- Removing old companies - June 28, 2021
- WordPress & WooCommerce Hosting Performance Benchmarks 2021 - May 27, 2021
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!
Nice catch! I’ve fixed that little typo 🙂
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
Thanks for this – huge timesaver after unsuccessfully trying a bunch of other SO answers!
Glad I could help 🙂
Thanks a bunch!
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
That’s bizarre. It’s only calling it once in my script. Are you running it in a bigger script that perhaps loops?
To add to recipients multiple emails you should be doing this
curl -d “to[]=${SGTO1}&to[]=${SGTO2}&to[]=${SGTO3}&toname1[]=${SGTONAME1}&toname2[]=${SGTONAME3}&subject=${SGSUBJECT}&text=${SGTEXT}&from=${SGFROM}&api_user=${SGUSER}&api_key=${SGPASS}” https://api.sendgrid.com/api/mail.send.json
How to include attachment with the same curl command?