def build_email_content(self, contact):
        """Generate the mail for a contact"""
        uidb36, token = tokenize(contact)
        context = Context({'contact': contact,
                           'domain': Site.objects.get_current().domain,
                           'newsletter': self.newsletter,
                           'tracking_image_format': TRACKING_IMAGE_FORMAT,
                           'uidb36': uidb36, 'token': token})
        content = self.newsletter_template.render(context)
        if TRACKING_LINKS:
            content = track_links(content, context)

        link_site = ''
        if INCLUDE_SITE_LINKS:
            site_link_markup = render_to_string(
                'newsletter/newsletter_link_site.html',
                context
            )
            link_site = BeautifulSoup(site_link_markup)

        content = body_insertion(content, link_site)

        if INCLUDE_UNSUBSCRIPTION:
            unsubscription_link_markup = render_to_string(
                'newsletter/newsletter_link_unsubscribe.html',
                context
            )
            unsubscription = BeautifulSoup(unsubscription_link_markup)
            content = body_insertion(content, unsubscription, end=True)

        if TRACKING_IMAGE:
            tracking_image_markup = render_to_string(
                'newsletter/newsletter_image_tracking.html', context
            )
            image_tracking = BeautifulSoup(tracking_image_markup)
            content = body_insertion(content, image_tracking, end=True)

        return smart_unicode(content)
def render_newsletter(request, slug, context):
    """Return a newsletter in HTML format"""
    newsletter = get_object_or_404(Newsletter, slug=slug)
    context.update({'newsletter': newsletter,
                    'domain': Site.objects.get_current().domain})

    content = render_string(newsletter.content, context)
    title = render_string(newsletter.title, context)
    if TRACKING_LINKS:
        content = track_links(content, context)
    unsubscription = render_file('newsletter/newsletter_link_unsubscribe.html', context)
    content = body_insertion(content, unsubscription, end=True)

    return render_to_response('newsletter/newsletter_detail.html',
                              {'content': content,
                               'title': title,
                               'object': newsletter},
                              context_instance=RequestContext(request))