A lot of cars now provide you with Bluetooth connections to your mobile phone. This gives you the ability to play music from your phone as well as make and answer phone calls. This made me think, what if we could extend this ability to listen and respond to incoming emails? I found it’s actually possible by turning on email forwarding and implementing the SendGrid Parse Webhook and the Twilio API.

Ingredients

  • Twilio Account: Twilio is a voice and messaging API service. We use their service to call our phone and dictate the email.
  • SendGrid Account: We use the Parse Webhook to create an email address in which all data of emails gets POSTED to our application.
  • Mobile Phone: The number that Twilio will call.

Step 1:

The first thing we are going to do is create accounts on Twilio and SendGrid. For a SendGrid account just visit www.sendgrid.com/free.

Step 2:

Get a phone number on Twilio.

Step 3:

Set an incoming email route at https://sendgrid.com/developer/reply

  • Hostname: Callme.bymail.in (any email like something@callme.bymail.in will be broken down into individual components and posted to the URL in the field below).
  • URL: The url of my application that gets the Posted data and takes some action.

Step 4:

Run the Python Flask code below:

from flask import Flask, request, Response
from twilio.rest import TwilioRestClient

app = Flask(__name__)

sender = 'nobody'
subject = 'nobody'
body = 'nobody'

def voiceaddress(address):
   newaddress= address.replace('.',' dot ')
   newaddress = newaddress.replace('@',' at ')
   newaddress = newaddress.replace('<',',')
   newaddress = newaddress.replace('>','')
   print newaddress
   return newaddress

@app.route ('/dictate/voice.xml', methods =['POST'])
def display():
   xml = "<Response><Say voice=\"alice\">You got an email from,, "+sender+". The subject is,, "+subject+". The message is,, "+body+"</Say></Response>"
   print xml
   return Response(xml, mimetype='text/xml')


@app.route ('/incoming', methods =['POST'])
def nextweb():
   global sender
   global subject
   global body
   mailfrom = request.form['from']
   sender = voiceaddress(mailfrom)
   subject = request.form['subject']
   body = request.form['text']
   print subject

   #Call User and dictate email
   # Get these credentials from http://twilio.com/user/account
   account_sid = "##########"
   auth_token = "##########"
   client = TwilioRestClient(account_sid, auth_token)

   # Make the call
   call = client.calls.create(to="",  # Any phone number
                              from_="", # Must be a valid Twilio number
                              url="http://yourdomain.com/dictate/voice.xml") #Change domain to where your app lives.
   print call.sid
   return "OK"


if __name__=='__main__':
   app.run(debug=True)

This code gets the email and calls my cell whenever an incoming email happens.

Step 5:

Now you have the ability to listen to emails while driving! Whenever you’re going on a long journey and want to be apprised of whats happening with your email, you can just turn your email forwarding on and have email that would normally be sent to your inbox, sent to the specific email address that calls your phone.

To see the other posts involving my Code Challenge click here!



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.