Commit 542844d9 authored by Adrián Ribao's avatar Adrián Ribao
Browse files

using context in email

parent 2ff25eec
# -*- coding: utf-8 -*-
from premailer import Premailer
import html2text
import json
from StringIO import StringIO
from django.contrib.staticfiles.finders import find
from lxml import etree
import html2text
from django.conf import settings
from django.contrib.staticfiles.finders import find
from django.core.mail import EmailMultiAlternatives
from django.template import Template, Context
from lxml import etree
from premailer import Premailer
class CampaignDeliver(object):
def __init__(self, campaign):
......@@ -45,19 +49,33 @@ class CampaignDeliver(object):
return text
def send_to_contact(self, contact):
msg = self.compose_email(contact.email)
msg = self.compose_email(contact)
msg.send()
def compose_email(self, to):
def compose_email(self, contact):
html = self.campaign.email_template.html
html_email = self.compile_css(html)
text_content = self.convert_email_to_text(html_email)
context = {
'first_name': contact.first_name,
'last_name': contact.last_name,
'email': contact.email,
'site': contact.site,
}
if contact.data:
data = json.loads(contact.data)
context.update(data)
t = Template(html_email)
final_html = t.render(Context(context))
text_content = self.convert_email_to_text(final_html)
msg = EmailMultiAlternatives(
subject=self.campaign.subject,
body=text_content,
to=[to, ])
msg.attach_alternative(html_email, "text/html")
to=[contact.email, ])
msg.attach_alternative(final_html, "text/html")
return msg
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment