Brett DiDonato email: brett@giftivo twitter: @giftivo

14
Brett DiDonato email: [email protected] twitter: @giftivo Building an Automated Email System with Django and AWS

description

B uilding an Automated Email System w ith Django and AWS. Brett DiDonato email: [email protected] twitter: @giftivo. Agenda. Configure AWS Simple Email Services (SES) Configure AWS Elastic Cloud Compute (EC2) Creating an Email System Sending Emails With Python & Django - PowerPoint PPT Presentation

Transcript of Brett DiDonato email: brett@giftivo twitter: @giftivo

Page 1: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

Brett DiDonatoemail: [email protected]: @giftivo

Building an Automated Email Systemwith Django and AWS

Page 2: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

Configure AWS SES

1. Test Settings – Verify Individual Email Addresses

2. Production Settings – Verify Domain

3. DNS – AWS Route 53

Page 4: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

Scheduling with Cron

1. Open cron table: crontab –e

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

3. Log file: /var/log/cron

Page 11: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

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: Brett DiDonato email:  brett@giftivo twitter:  @giftivo

Building an Automated Email Systemwith Django and AWS

Get the notes from this presentation:

giftivo.com/djangonyc

Brett DiDonatoemail: [email protected]: @giftivo