django-sendgrid-events Documentation

Similar documents
Flask- Documentation

kaleo Documentation Release 1.5 Eldarion

Gargoyle Documentation

django-debreach Documentation

django-baton Documentation

django-cms-search Documentation

Bricks Documentation. Release 1.0. Germano Guerrini

django-baton Documentation

Django-Select2 Documentation. Nirupam Biswas

Graphene Documentation

django-app-metrics Documentation

django-ratelimit-backend Documentation

django-messages Documentation

django-report-tools Documentation

Zoook e-sale Documentation

EXPERIENCES MOVING FROM DJANGO TO FLASK

Django MFA Documentation

Tablo Documentation. Release Conservation Biology Institute

wagtail-robots Documentation

Django Extra Views Documentation

CIS192 Python Programming

Django Groups Manager Documentation

django simple pagination Documentation

Building a Django Twilio Programmable Chat Application

Django Groups Manager Documentation

django-embed-video Documentation

Bambu API Documentation

django-contact-form Documentation

django-permission Documentation

django-avatar Documentation

django-avatar Documentation

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

django-oauth2-provider Documentation

Django PAM Documentation

wagtailtrans Documentation

django-simple-sms Documentation

django-push Documentation

django-embed-video Documentation

django-subdomains Documentation

Django File Picker Documentation

DJOAuth2 Documentation

django-embed-video Documentation

djangotribune Documentation

django-allauth-2fa Documentation

Promgen Documentation

wagtailmenus Documentation

TailorDev Contact Documentation

Django Standalone Apps

django-image-cropping Documentation

Django IPRestrict Documentation

bottle-rest Release 0.5.0

flask-jwt Documentation

django-notifier Documentation

LFC - Lightning Fast CMS Documentation

django-rest-framework-datatables Documentation

django-mongonaut Documentation

Django File Picker Documentation

Relay. Calendar Setup. Google Calendar

django-password-reset Documentation

Django-danceschool Documentation

django-geoip Documentation

Easy-select2 Documentation

django-dajaxice Documentation

MIT AITI Python Software Development Lab DJ1:

django-scaffold Documentation

django-openid Documentation

django-gollum Documentation

Quick.JS Documentation

micawber Documentation

CSE 115. Introduction to Computer Science I

django-stored-messages Documentation

django-amp-tools Documentation Release latest

Signals Documentation

Django AdminLTE 2 Documentation

froide Documentation Release alpha Stefan Wehrmeyer

alphafilter Documentation

django-antispam Documentation

django-sticky-uploads Documentation

Runtime Dynamic Models Documentation Release 1.0

Accelerating Information Technology Innovation

django-conduit Documentation

django-revproxy Documentation

wagtailmenus Documentation

ska Documentation Release Artur Barseghyan

Djam Documentation. Release Participatory Culture Foundation

django-intercom Documentation

KWizCom Corporation. Paste+ App. User Guide

Connexion Documentation

youtube-dl-api-server Release 0.3

django mail admin Documentation

CIS192 Python Programming

Django REST Framework JSON API Documentation

Pluggable Patterns. For Reusable Django Applications

Django EL(Endless) Pagination Documentation

fragapy Documentation

Integration Requirements

django-renderit Documentation

django-precise-bbcode Documentation

django-slim Documentation

django-model-report Documentation

Transcription:

django-sendgrid-events Documentation Release 1.2 Eldarion, Inc November 07, 2016

Contents 1 Development 3 i

ii

django-sendgrid-events Documentation, Release 1.2 a simple app to provide and endpoind for and handle batch events from SendGrid s Event API Contents 1

django-sendgrid-events Documentation, Release 1.2 2 Contents

CHAPTER 1 Development The source repository can be found at https://github.com/eldarion/django-sendgrid-events 1.1 Contents 1.1.1 ChangeLog 1.1 added an admin 1.0 initial release 1.1.2 Installation To install pip install django-sendgrid-events Add sendgrid_events to your INSTALLED_APPS setting: INSTALLED_APPS = ( # other apps "sendgrid_events", ) Add sengrid_events.urls as an inclusion to the your main urls.py file. You will likely want to provide a bit of security through obscurity as you are essentially going to be trusting whatever is POSTed to this url. You can do this by generating a UUID and then copy and pasting this as part of your URL: >>> import uuid >>> print uuid.uuid4() aeea4b34-e5cb-4b05-84d8-79bfee95ccf4 Then in your urls.py: 3

django-sendgrid-events Documentation, Release 1.2 url("^aeea4b34-e5cb-4b05-84d8-79bfee95ccf4/", include("sendgrid_events.urls")) Finally, go to and setup the Events API app, pasting in the end point to this url, which will be: http(s)://<yoursite.com>/aeea4b34-e5cb-4b05-84d8-79bfee95ccf4/batch/ Be sure to check the box that says Batch event notifications 1.1.3 Signals batch_processed Provides a single argument which is the sendgrid_events.models.event instance for the single event within the batch. A single batch POST can contain one or more events. 1.1.4 Usage Using this after it has been setup in your site depends on what you want to accomplish. Just installing it will enable you to receive events on every email sent through your Sendgrid account. That by itself, however, likely produces much value as there is no context to those emails to derive any meaningful use out of. One of the cool things you can do now that you have the Events API configured and django-sendgrid-events hooked up, is send identifying data in the headers of each email you send so that when you receive and Event API POST you can tied each email back to some event in your site. To accomplish this you should make sure your email settings are configured to use your Sendgrid account: EMAIL_BACKEND = "django.core.mail.backends.smtp.emailbackend" EMAIL_HOST = os.environ.get("email_host", "smtp.sendgrid.net") EMAIL_PORT = os.environ.get("email_port", 587) EMAIL_HOST_USER = os.environ.get("email_host_user", "") EMAIL_HOST_PASSWORD = os.environ.get("email_host_password", "") EMAIL_USE_TLS = True And then whenever you are sending an email in your site that you want to be able to track the response to send an extra header like so: email = EmailMultiAlternatives( "Your Subject Here", "Your plain text message here", "sender@email.com", ["recipient@email.com"], headers={ "X-SMTPAPI": json.dumps({ "unique_args": { "some_key": "some_value" }, "category": "some_category" }) } ) email.attach_alternative("your html rendered email", "text/html") email.send() 4 Chapter 1. Development

django-sendgrid-events Documentation, Release 1.2 You don t have to send HTML emails, but you can control the look a bit better since in order to get open and click event tracking with plain text emails, Sendgrids converts it to an HTML email, which can end up not looking how you d like it to. Now when your events POST to the endpoint you have setup, they ll have this extra bit of data which you can use to link to something in your site. For example, say, you want to link it to the instance of a specific content model you can pass in as a unique_arg: "unique_args": { "my_model_pk": object.pk } Then hook up a signal receiver for sendgrid_events.signals.batch_processed: from django.dispatch import receiver from sendgrid_events.signals import batch_processed from mysite.myapp.models import SomeContent @receiver(batch_processed) def handle_batch_processed(sender, events, **kwargs): for event in events: try: c = SomeContent.objects.get(pk=event.data.get("my_model_pk")) c.email_events.create(sendgrid_event=event) except SomeContent.DoesNotExist: pass Where you have created a tracking model in your mysite.myapps.models for SomeContent: class SomeContentEvent(models.Model): some_content = models.foreignkey(somecontent, related_name="email_events") sendgrid_event = models.foreignkey(event, related_name="+") class Meta: ordering = ["sendgrid_event created_at"] Now, you have access to email events for that object that you can use in your site: {% for event in some_content.email_events.all %} <span class="label label-{{ event.kind }}" title="{{ event.created_at }}"> {{ event.kind }} </span> {% endfor %} 1.1. Contents 5