{"id":707,"date":"2014-05-06T12:00:48","date_gmt":"2014-05-06T16:00:48","guid":{"rendered":"https:\/\/reviewsignal.com\/blog\/?p=707"},"modified":"2014-07-20T23:35:47","modified_gmt":"2014-07-21T03:35:47","slug":"how-to-send-email-from-the-command-line-with-sendgrid","status":"publish","type":"post","link":"https:\/\/reviewsignal.com\/blog\/2014\/05\/06\/how-to-send-email-from-the-command-line-with-sendgrid\/","title":{"rendered":"How to send email from the command line with SendGrid"},"content":{"rendered":"<p>Sending email should be simple. It's something I really don't like thinking about.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>The first solution that jumped to mind was <a href=\"http:\/\/sendgrid.com\">SendGrid<\/a>, who I had the pleasure of trying out their API at <a href=\"https:\/\/2013.battlehack.org\/washington\/\">PayPal BattleHack DC<\/a>. It was dead simple. I had integrated it into PHP though. I didn't know if it worked from the command line.<\/p>\n<p>I checked the <a href=\"https:\/\/sendgrid.com\/docs\/API_Reference\/Web_API\/mail.html\">docs<\/a> and found they had a rest API.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"bash\" style=\"font-family:monospace;\">curl <span style=\"color: #660033;\">-d<\/span> <span style=\"color: #ff0000;\">'to=destination@example.com&amp;toname=Destination&amp;subject=Example Subject&amp;text=testingtextbody&amp;from=info@domain.com&amp;api_user=your_sendgrid_username&amp;api_key=your_sendgrid_password'<\/span> https:<span style=\"color: #000000; font-weight: bold;\">\/\/<\/span>api.sendgrid.com<span style=\"color: #000000; font-weight: bold;\">\/<\/span>api<span style=\"color: #000000; font-weight: bold;\">\/<\/span>mail.send.json<\/pre><\/td><\/tr><\/table><\/div>\n\n<p>If you want to clean it up with variables:<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"bash\" style=\"font-family:monospace;\"><span style=\"color: #666666; font-style: italic;\">#!\/bin\/sh<\/span>\n<span style=\"color: #007800;\">SGTO<\/span>=receiver<span style=\"color: #000000; font-weight: bold;\">@<\/span>example.com\n<span style=\"color: #007800;\">SGTONAME<\/span>=<span style=\"color: #ff0000;\">'Some Name'<\/span>\n<span style=\"color: #007800;\">SGSUBJECT<\/span>=<span style=\"color: #ff0000;\">'Email Subject'<\/span>\n<span style=\"color: #007800;\">SGFROM<\/span>=from<span style=\"color: #000000; font-weight: bold;\">@<\/span>example.com\n<span style=\"color: #007800;\">SGTEXT<\/span>=<span style=\"color: #ff0000;\">'Email Text'<\/span>\n<span style=\"color: #007800;\">SGUSER<\/span>=user\n<span style=\"color: #007800;\">SGPASS<\/span>=password\ncurl <span style=\"color: #660033;\">-d<\/span> <span style=\"color: #ff0000;\">&quot;to=<span style=\"color: #007800;\">${SGTO}<\/span>&amp;toname=<span style=\"color: #007800;\">${SGTONAME}<\/span>&amp;subject=<span style=\"color: #007800;\">${SGSUBJECT}<\/span>&amp;text=<span style=\"color: #007800;\">${SGTEXT}<\/span>&amp;from=<span style=\"color: #007800;\">${SGFROM}<\/span>&amp;api_user=<span style=\"color: #007800;\">${SGUSER}<\/span>&amp;api_key=<span style=\"color: #007800;\">${SGPASS}<\/span>&quot;<\/span> https:<span style=\"color: #000000; font-weight: bold;\">\/\/<\/span>api.sendgrid.com<span style=\"color: #000000; font-weight: bold;\">\/<\/span>api<span style=\"color: #000000; font-weight: bold;\">\/<\/span>mail.send.json<\/pre><\/td><\/tr><\/table><\/div>\n\n<p>Voila! Sending emails from my bash script is now simple.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sending email should be simple. It&#8217;s something I really don&#8217;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&#8217;t have any mailing program installed. I also [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[66],"tags":[44,120,118,119],"_links":{"self":[{"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/posts\/707"}],"collection":[{"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":6,"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":757,"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/posts\/707\/revisions\/757"}],"wp:attachment":[{"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reviewsignal.com\/blog\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}