How to Get SMS Alerts When a User Opens an Email


March 05, 2014
Written by
SendGrid Team
Contributor
Opinions expressed by Twilio contributors are their own

Email to SMSHas there ever been a time when you wanted to know if someone opened up an important email? Maybe you want to get a real-time alert when people mark your email as spam. Or perhaps get immediately notified if a user clicks on your email's unsubscribe link.

This is possible with our Event Webhook plus an SMS API of your choosing. In this example, I will showcase a small Python program that will receive event data from SendGrid and SMS alert a user with the Twilio API.

Easy as One, Two, Three

    1. Log in to SendGrid and go to your apps page. Click on the settings link for the Event Notification app.Event notification app
    2. Put in the URL of the program where your application will reside.Event notification app settingsFor this example, we are creating a Python application. For testing, this can be run on your computer and opened up to the internet with ngrok. (We have a great ngrok tutorial if this is new to you).
    3. Here's the bulk of the Python program that receives an event and sends an SMS.
from flask import Flask, request
import json
from twilio.rest import TwilioRestClient
app = Flask(__name__)
@app.route('/',methods=['POST'])
def foo():
account_sid = "Your Twilio Account SID"
auth_token = "Your Twilio Auth Token"
client = TwilioRestClient(account_sid, auth_token)
data = json.loads(request.data)
message = "Your email just got opened up by: {}".format(data[0]['email'])
message = client.messages.create(to="+13334445555", from_="+13334445555", body=message)
return "OK"
if __name__ == '__main__':
app.run()
view raw emailalert.py hosted with ❤ by GitHub
Feel free to customize this for your specific use case. I am using Twilio for the SMS API, you can visit their website for more info. If you have questions or get stuck, I'm @Kunal732 on Twitter.

For more information on the Event Webhook and to see what other pieces of data SendGrid sends, check out our documentation.

Send With Confidence

Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.