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.
- 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.
- Send an email to firstname.lastname@example.org with the image attached. (If you can, try to send the image so it’s less than 1mb)
- Make sure to include a subject! I use this to name the document that gets sent back.
- 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!
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.
The entire app, written in PHP, is up on Github. Check out the core piece of the hack below.
//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 => $file)
//Convert Image to Text
$ocrapi = new Abby($config['abby']['app_id'], $config['abby']['password']);
$docName = $ocrapi->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();
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.