SlideShare une entreprise Scribd logo
1  sur  14
Building an Automated Email System 
with Django and AWS 
Brett DiDonato 
email: brett at brettdidonato dot com 
twitter: @brettdidonato
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
Configure AWS SES 
1. Test Settings – Verify Individual Email Addresses 
2. Production Settings – Verify Domain 
3. DNS – AWS Route 53
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
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 
1. Install django-ses 
2. 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 
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
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")
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)
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
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
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)
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
Building an Automated Email System 
with Django and AWS 
Thanks! 
Brett DiDonato 
email: brett at brettdidonato dot com 
twitter: @brettdidonato

Contenu connexe

Similaire à Building an automated email system with django and aws

NZYP Project Casestudy using SilverStripe CMS
NZYP Project Casestudy using SilverStripe CMSNZYP Project Casestudy using SilverStripe CMS
NZYP Project Casestudy using SilverStripe CMSCam Findlay
 
Zotonic tutorial EUC 2013
Zotonic tutorial EUC 2013Zotonic tutorial EUC 2013
Zotonic tutorial EUC 2013Arjan
 
Session 4 StudioX EMEA email automation.pdf
Session 4 StudioX EMEA email automation.pdfSession 4 StudioX EMEA email automation.pdf
Session 4 StudioX EMEA email automation.pdfCristina Vidu
 
LAPHP/LAMPSig Talk: Intro to SendGrid - Building a Scalable Email Infrastructure
LAPHP/LAMPSig Talk: Intro to SendGrid - Building a Scalable Email InfrastructureLAPHP/LAMPSig Talk: Intro to SendGrid - Building a Scalable Email Infrastructure
LAPHP/LAMPSig Talk: Intro to SendGrid - Building a Scalable Email InfrastructureSendGrid
 
6 Things You Didn't Know About Firebase Auth
6 Things You Didn't Know About Firebase Auth6 Things You Didn't Know About Firebase Auth
6 Things You Didn't Know About Firebase AuthPeter Friese
 
Building apps for microsoft teams - aossg
Building apps for microsoft teams - aossgBuilding apps for microsoft teams - aossg
Building apps for microsoft teams - aossgJenkins NS
 
Android ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codesAndroid ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codesAravindharamanan S
 
The Big Picture and How to Get Started
The Big Picture and How to Get StartedThe Big Picture and How to Get Started
The Big Picture and How to Get Startedguest1af57e
 
Authentication
AuthenticationAuthentication
Authenticationsoon
 
Build and deploy PyTorch models with Azure Machine Learning - Henk - CCDays
Build and deploy PyTorch models with Azure Machine Learning - Henk - CCDaysBuild and deploy PyTorch models with Azure Machine Learning - Henk - CCDays
Build and deploy PyTorch models with Azure Machine Learning - Henk - CCDaysCodeOps Technologies LLP
 
Shift Remote AI: Build and deploy PyTorch Models with Azure Machine Learning ...
Shift Remote AI: Build and deploy PyTorch Models with Azure Machine Learning ...Shift Remote AI: Build and deploy PyTorch Models with Azure Machine Learning ...
Shift Remote AI: Build and deploy PyTorch Models with Azure Machine Learning ...Shift Conference
 
MozCon Seattle 2011 - Social Design
MozCon Seattle 2011 - Social DesignMozCon Seattle 2011 - Social Design
MozCon Seattle 2011 - Social DesignMat Clayton
 
Mobile, web and cloud - the triple crown of modern applications
Mobile, web and cloud -  the triple crown of modern applicationsMobile, web and cloud -  the triple crown of modern applications
Mobile, web and cloud - the triple crown of modern applicationsIdo Green
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Djangofool2nd
 
Odoo Experience 2018 - Inherit from These 10 Mixins to Empower Your App
Odoo Experience 2018 - Inherit from These 10 Mixins to Empower Your AppOdoo Experience 2018 - Inherit from These 10 Mixins to Empower Your App
Odoo Experience 2018 - Inherit from These 10 Mixins to Empower Your AppElínAnna Jónasdóttir
 
App engine devfest_mexico_10
App engine devfest_mexico_10App engine devfest_mexico_10
App engine devfest_mexico_10Chris Schalk
 
JavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeJavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeLaurence Svekis ✔
 

Similaire à Building an automated email system with django and aws (20)

NZYP Project Casestudy using SilverStripe CMS
NZYP Project Casestudy using SilverStripe CMSNZYP Project Casestudy using SilverStripe CMS
NZYP Project Casestudy using SilverStripe CMS
 
Zotonic tutorial EUC 2013
Zotonic tutorial EUC 2013Zotonic tutorial EUC 2013
Zotonic tutorial EUC 2013
 
Session 4 StudioX EMEA email automation.pdf
Session 4 StudioX EMEA email automation.pdfSession 4 StudioX EMEA email automation.pdf
Session 4 StudioX EMEA email automation.pdf
 
LAPHP/LAMPSig Talk: Intro to SendGrid - Building a Scalable Email Infrastructure
LAPHP/LAMPSig Talk: Intro to SendGrid - Building a Scalable Email InfrastructureLAPHP/LAMPSig Talk: Intro to SendGrid - Building a Scalable Email Infrastructure
LAPHP/LAMPSig Talk: Intro to SendGrid - Building a Scalable Email Infrastructure
 
6 Things You Didn't Know About Firebase Auth
6 Things You Didn't Know About Firebase Auth6 Things You Didn't Know About Firebase Auth
6 Things You Didn't Know About Firebase Auth
 
Building apps for microsoft teams - aossg
Building apps for microsoft teams - aossgBuilding apps for microsoft teams - aossg
Building apps for microsoft teams - aossg
 
Android ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codesAndroid ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codes
 
The Big Picture and How to Get Started
The Big Picture and How to Get StartedThe Big Picture and How to Get Started
The Big Picture and How to Get Started
 
Jquery
JqueryJquery
Jquery
 
Authentication
AuthenticationAuthentication
Authentication
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Build and deploy PyTorch models with Azure Machine Learning - Henk - CCDays
Build and deploy PyTorch models with Azure Machine Learning - Henk - CCDaysBuild and deploy PyTorch models with Azure Machine Learning - Henk - CCDays
Build and deploy PyTorch models with Azure Machine Learning - Henk - CCDays
 
Shift Remote AI: Build and deploy PyTorch Models with Azure Machine Learning ...
Shift Remote AI: Build and deploy PyTorch Models with Azure Machine Learning ...Shift Remote AI: Build and deploy PyTorch Models with Azure Machine Learning ...
Shift Remote AI: Build and deploy PyTorch Models with Azure Machine Learning ...
 
MozCon Seattle 2011 - Social Design
MozCon Seattle 2011 - Social DesignMozCon Seattle 2011 - Social Design
MozCon Seattle 2011 - Social Design
 
Mobile, web and cloud - the triple crown of modern applications
Mobile, web and cloud -  the triple crown of modern applicationsMobile, web and cloud -  the triple crown of modern applications
Mobile, web and cloud - the triple crown of modern applications
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
 
Odoo Experience 2018 - Inherit from These 10 Mixins to Empower Your App
Odoo Experience 2018 - Inherit from These 10 Mixins to Empower Your AppOdoo Experience 2018 - Inherit from These 10 Mixins to Empower Your App
Odoo Experience 2018 - Inherit from These 10 Mixins to Empower Your App
 
App engine devfest_mexico_10
App engine devfest_mexico_10App engine devfest_mexico_10
App engine devfest_mexico_10
 
JavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeJavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive Code
 
ASP.NET Lecture 5
ASP.NET Lecture 5ASP.NET Lecture 5
ASP.NET Lecture 5
 

Dernier

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 

Dernier (20)

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 

Building an automated email system with django and aws

  • 1. Building an Automated Email System with Django and AWS Brett DiDonato email: brett at brettdidonato dot com twitter: @brettdidonato
  • 2. 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
  • 3. Configure AWS SES 1. Test Settings – Verify Individual Email Addresses 2. Production Settings – Verify Domain 3. DNS – AWS Route 53
  • 4. 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
  • 5. 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] )
  • 6. Configure SES With Django 1. Install django-ses 2. 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 )
  • 7. 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
  • 8. 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")
  • 9. 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)
  • 10. 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
  • 11. 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
  • 12. 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)
  • 13. 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
  • 14. Building an Automated Email System with Django and AWS Thanks! Brett DiDonato email: brett at brettdidonato dot com twitter: @brettdidonato