150 likes | 330 Views
B uilding an Automated Email System w ith Django and AWS. Brett DiDonato email: brett@giftivo.com twitter: @giftivo. Agenda. Configure AWS Simple Email Services (SES) Configure AWS Elastic Cloud Compute (EC2) Creating an Email System Sending Emails With Python & Django
E N D
Building an Automated Email System with Django and AWS Brett DiDonato email: brett@giftivo.com twitter: @giftivo
Agenda • Configure AWS Simple Email Services (SES) • Configure AWS Elastic Cloud Compute (EC2) • Creating an Email System • Sending Emails With Python & Django • Tracking Opens & Unsubscribes • Open Email In Browser • Scheduling With Cron • Formatting Emails – Best Practices • Personalizing The Experience • Case Study: Email Reminder System
Configure AWS SES • Test Settings – Verify Individual Email Addresses • Production Settings – Verify Domain • DNS – AWS Route 53
Configure AWS EC2 • Launch an instance • Configure Apache, Python, Python Libraries, Django, MongoDB • Start Apache & MongoDB • Install Python library boto – used for SES and other AWS functions
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 <from@domain.com>", • subject=emailSubject, • body=emailBody, • format="html", • to_addresses=[listOfToEmails] • )
Configure SES With Django • Install django-ses • 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’, • ‘from@domain.com’, • [listOfToEmails], • fail_silently=False • )
Track Opens • Embed image in email body with URL pointing towards Django server address: <img src=“mywebsite.com/email_img/campaign/identifier” /> • urls.py: (r'^email_img/(?P<campaign>.*)/(?P<identifier>.*)$', email_img), • 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
Track Unsubscribes • Embed link in email body with URL pointing towards Django server address: <a href=http://mywebsite.com/unsubscribe/userid>Unsubscribe</a> • urls.py: (r'^unsubscribe/(?P<campaign>.*)/(?P<userid>.*)$', email_tracking_file), • 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")
Open Email in Browser • 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> • Urls.py: (r'^email_in_browser/(?P<campaign>.*)/(?P<userid>.*)$', email_tracking_file), • Log unsubscribe event and return confirmation response (probably want to actually confirm it): • def email_in_browser(response, campaign, userid): • … • return HttpResponse(emailBody)
Scheduling with Cron • Open cron table: crontab –e • Run job daily at midnight… 0 0 * * * python /directory/for/email_script.py • Log file: /var/log/cron
Formatting Emails – Best Practices • No JavaScript • GIF or JPG Images • HTML Tables • Inline CSS • Max width ~600px • Avoid “spammy” words (in subject especially) • Test with major email providers: Gmail, Yahoo!, Outlook, Hotmail/Outlook.com, AOL
Personalizing the Experience • Build something that adds value • Give the user control • Personalized subject & body: • Name • Age • Sex • Browsing history • Shopping history • 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)
Case Study: Email Reminder System • Users log in to website and configure important life events • Event details stored in MongoDB • Cron job scheduled once per day against reminder events for current day • Database logging and events updated • Emails sent with personalized email subject and body • Track opens and provide “view in browser” link • Try it out at giftivo.com, log in, click red “reminders” button, configure reminders, click “Get A Gift Now” to view email in browser
Building an Automated Email System with Django and AWS • Get the notes from this presentation: • giftivo.com/djangonyc Brett DiDonato email: brett@giftivo.com twitter: @giftivo