We Are Hiring

My First Hack as a Developer Evangelist


Posted on

Hi I’m Kunal, I am a new Developer Evangelist at SendGrid. I want to introduce you to my first SendGrid hack, the OCR App. However, before I tell you more about my hack I want to give you a little insight behind my motivation. I wanted to create something that showcased the simplicity and power of our APIs. I also wanted it to be something that people could use after hearing about it. I thought it would be interesting to do a mashup between our SMTP & Inbound Parse APIs as well as Abbyy’s OCR SDK.

What does this hack do?

A user just takes a picture of any printed text and emails that to my address, they then get an editable text document as a reply. This hack is live, so go ahead and try it yourself.

  1. Take out your phone and take a photo of some printed text. You can see below that I took a photo of a nutrition label.Photo of nutritional information
  2. Send an email to ocrthat@si.ht with the image attached. (If you can, try to send the image so it’s less than 1mb)Send photo in email
  3. Make sure to include a subject! I use this to name the document that gets sent back.
  4. You will get back an email within a couple of minutes with an .rtf file attached. You can open that up in your editor and see that the image you sent is now an editable file!RTF file with editable text

That’s what it looks like to a user. Here’s the hacker’s view of things.

Behind the Scenes

This app takes advantage of our Inbound Parse API, which is actually a webhook. This means that whenever an email is sent to my domain — www.si.ht — SendGrid first parses the email into its components and posts them to a url of my choice. My application then gets those individual pieces and performs some logic.

In this case, my app gets your email, subject and image. It then converts that image to an rtf file using Abbyy’s OCR SDK and then uses our SMTP API to send you back a reply email with the attached document.

Code

The entire app, written in PHP, is up on Github. Check out the core piece of the hack below.

<?php
require_once('config.php');
require_once('abby.php');
include 'sendgrid/sendgrid-php/SendGrid_loader.php';

//Get Post from SendGrid
$from = $_POST["from"];
$subject = $_POST["subject"];

//Get Attached Image
$fh = fopen('/tmp/parse.log', 'a+');
if ( $fh )
{
  fwrite($fh, print_r($_POST, true) . print_r($_FILES, true));
  foreach ($_FILES as $key =&gt; $file)
  {
    move_uploaded_file($file['tmp_name'], "upload/".$file['name']);
  }
  fclose($fh);
}

//Convert Image to Text
$ocrapi = new Abby($config['abby']['app_id'], $config['abby']['password']);
$docName = $ocrapi-&gt;convert($file['name']);

//Send reply email with attached text document
$filePath = dirname(__FILE__).'/upload/'.$docName;
$sendgrid = new SendGrid($config['sendgrid']['api_user'], $config['sendgrid']['api_key']);

$mail = new SendGrid\Mail();

$mail-&gt;
addTo($from)-&gt;
setFrom($config['sendgrid']['my_email'])-&gt;
setSubject('Re: '.$subject)-&gt;
setText($docName)-&gt;
setHtml($docName)-&gt;
addAttachment($filePath);

$sendgrid-&gt;smtp-&gt;send($mail);
?>

If you want to get started and create your own SendGrid application that takes advantage of our Inbound Parse API, create an account on SendGrid.com and follow the instructions on our awesome documentation.


Meet Kunal, Developer Evangelist at-large for SendGrid and home-grown New Jersey hacker. He previously ran General Machines, which developed Deaftel - a phone service for the deaf that converts voice to text and text back into voice over a phone call. Since March 2013, Kunal has been working as a Developer Evangelist at SendGrid, helping enable developers to get the tools and resources they need to make awesome, creative stuff.

2 thoughts on “My First Hack as a Developer Evangelist

  1. Pingback: Using SendGrid's Webhooks for Fun and Profit

  2. Pingback: Collect Inbound Email Using Python and Flask

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>