14
Brett DiDonato email: brett at brettdidonato dot com twitter: @brettdidonato Building an Automated Email System with Django and AWS

Building an automated email system with django and aws

Embed Size (px)

DESCRIPTION

This presentation details how to build an automated email system using Amazon Web Services (AWS), Python/Django and CRON. This was originally presented at the Django-NYC meetup on February 26, 2013.

Citation preview

Page 1: Building an automated email system with django and aws

Brett DiDonatoemail: brett at brettdidonato dot comtwitter: @brettdidonato

Building an Automated Email Systemwith Django and AWS

Page 2: Building an automated email system with django and aws

Agenda

1. Configure AWS Simple Email Services (SES)

2. Configure AWS Elastic Cloud Compute (EC2)

3. Creating an Email System

a. Sending Emails With Python & Django

b. Tracking Opens & Unsubscribes

c. Open Email In Browser

d. Scheduling With Cron

e. Formatting Emails – Best Practices

f. Personalizing The Experience

4. Case Study: Email Reminder System

Page 3: Building an automated email system with django and aws

Configure AWS SES

1. Test Settings – Verify Individual Email Addresses

2. Production Settings – Verify Domain

3. DNS – AWS Route 53

Page 4: Building an automated email system with django and aws

Configure AWS EC2

1. Launch an instance

2. Configure Apache, Python, Python Libraries, Django, MongoDB

3. Start Apache & MongoDB

4. Install Python library boto – used for SES and other AWS functions

Page 5: Building an automated email system with django and aws

Configure SES With Python

1. Create /etc/boto.cfg file: [Credentials] aws_access_key_id = your-key-here aws_secret_access_key = your-secret-here

2. Sending emails: import boto conn = boto.connect_ses() response = conn.send_email( source=“Name <[email protected]>",

subject=emailSubject,body=emailBody,format="html",to_addresses=[listOfToEmails]

)

Page 6: Building an automated email system with django and aws

Configure SES With Django

1. Install django-ses2. Add key, secret and SES reference to settings.py: aws_access_key_id = your-key-here aws_secret_access_key = your-secret-here EMAIL_BACKEND = ‘django_ses.SESBackend’3. Sending Emails: from django.core.mail import send_mail send_mail( ‘Subject’, ‘Body’, ‘[email protected]’, [listOfToEmails], fail_silently=False )

Page 7: Building an automated email system with django and aws

Track Opens

1. Embed image in email body with URL pointing towards Django server address: <img src=“mywebsite.com/email_img/campaign/identifier” />

2. urls.py: (r'^email_img/(?P<campaign>.*)/(?P<identifier>.*)$', email_img),

3. Log read event and serve image: from PIL import Image from datetime import datetime from pymongo import Connection connection = Connection()

def email_img(response, campaign, identifier): myDict = {} myDict['campaign'] = campaign myDict['identifier'] = identifier myDict['read_timestamp'] = datetime.now() connection.dbName.trackEmails.insert(myDict)

image = Image.open(“/directory/for/my-image.gif") transparent = image.info["transparency"] response = HttpResponse(mimetype="image/gif") image.save(response, "gif", transparency=transparent)

return response

Page 8: Building an automated email system with django and aws

Track Unsubscribes

1. Embed link in email body with URL pointing towards Django server address: <a href=http://mywebsite.com/unsubscribe/userid>Unsubscribe</a>

2. urls.py: (r'^unsubscribe/(?P<campaign>.*)/(?P<userid>.*)$', email_tracking_file),

3. Log unsubscribe event and return confirmation response (probably want to actually confirm it): from pymongo import Connection connection = Connection()

def unsubscribe(response, userid): connection.dbName.userCollection.update({"userid":userid},{"$set":{"unsubscribed":True}}) return HttpResponse("unsubscribed")

Page 9: Building an automated email system with django and aws

Open Email in Browser

1. In the email body include a link to view the email in the browser: <a href=“http://mywebsite.com/campaign/userid”>View This Email In Your Browser</a>

2. Urls.py: (r'^email_in_browser/(?P<campaign>.*)/(?P<userid>.*)$', email_tracking_file),

3. Log unsubscribe event and return confirmation response (probably want to actually confirm it):

def email_in_browser(response, campaign, userid):

return HttpResponse(emailBody)

Page 10: Building an automated email system with django and aws

Scheduling with Cron

1. Open cron table: crontab –e

2. Run job daily at midnight… 0 0 * * * python /directory/for/email_script.py

Log file: /var/log/cron

Note: consider using a CRON alternative like Celery for more advanced use cases

Page 11: Building an automated email system with django and aws

Formatting Emails – Best Practices

1. No JavaScript

2. GIF or JPG Images

3. HTML Tables

4. Inline CSS

5. Max width ~600px

6. Avoid “spammy” words (in subject especially)

7. Test with major email providers: Gmail, Yahoo!, Outlook, Hotmail/Outlook.com, AOL

Page 12: Building an automated email system with django and aws

Personalizing the Experience

1. Build something that adds value

2. Give the user control

3. Personalized subject & body:

1. Name

2. Age

3. Sex

4. Browsing history

5. Shopping history

6. Location – pygeoip

import pygeoip ip = response.META['REMOTE_ADDR'] gi = pygeoip.GeoIP('/usr/local/share/GeoIP/GeoIP.dat',

pygeoip.MEMORY_CACHE) country = gi.country_code_by_name(ip)

Page 13: Building an automated email system with django and aws

Case Study: Email Reminder System

1. Users log in to website and configure important life events

2. Event details stored in MongoDB

3. Cron job scheduled once per day against reminder events for current day

4. Database logging and events updated

5. Emails sent with personalized email subject and body

6. Track opens and provide “view in browser” link

7. Try it out at giftivo.com, log in, click red “reminders” button, configure reminders, click “Get A Gift Now” to view email in browser

Page 14: Building an automated email system with django and aws

Building an Automated Email Systemwith Django and AWS

Thanks!

Brett DiDonatoemail: brett at brettdidonato dot comtwitter: @brettdidonato