Skip to main content

Create and send email campaigns from Wagtail

Project description

A plugin for wagtail that allows you to create, send, preview, edit and test email campaigns from within Wagtail. Campaign templates are created using mjml.

Basic usage

Install birdsong:

pip install wagtail-birdsong

Add the following to your installed apps:

'mjml',
'birdsong',
'wagtail.contrib.modeladmin',

Make a new app e.g. email, create a models.py with a model that extends the included Campaign model. Some compatible mjml streamfield blocks are included in birdsong for convenience.

from birdsong.blocks import DefaultBlocks
from django.db import models
from wagtail.admin.edit_handlers import StreamFieldPanel
from wagtail.core.fields import StreamField


class SaleEmail(Campaign):
    body = StreamField(DefaultBlocks())

    panels = Campaign.panels + [
        StreamFieldPanel('body'),
    ]

Then in the same app, create a wagtail_hooks.py if it doesn’t exist, this is where the admin is created for content editors to create/edit/send campaigns.

The CampaignAdmin is just an extension of Wagtail’s ModelAdmin class so most of the same options are available for overriding functionality.

from wagtail.contrib.modeladmin.options import modeladmin_register
from birdsong.options import CampaignAdmin

from .models import SaleEmail


@modeladmin_register
class SaleEmailAdmin(CampaignAdmin):
    campaign = SaleEmail
    menu_label = 'Sale Email'
    menu_icon = 'mail'
    menu_order = 200

Create your campaign template in {app_folder}/templates/{app_name}/mail/{model_name}.html eg email/templates/email/mail/sale_email.html, alternatively override the get_template method on your campaign model.

Campaign templates us django-mjml for responsive, well designed emails. To read up how to setup django-mjml you can read the docs here. There is a base template included in Birdsong that can be extended.

sale_email.html

{% extends "birdsong/mail/base_email.html" %}

{% block email_body %}
<mj-section>
    <mj-column>
        <mj-text>Hello {{ contact.email }}!</mj-text>
        {% for b in self.body %}
            {{ b }}
        {% endfor %}
    </mj-column>
</mj-section>
{% endblock email_body %}

Custom Contact models

By default the included Contact model is used for every campaign, sometimes you may want to store extra data, like names and preferences. You can override the default Contact model by setting an option on the admin for your campaign:

models.py

from birdsong.models import Contact
from django.db import models

class ExtendedContact(Contact):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    location = models.CharField(max_length=255)

wagtail_hooks.py

from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from birdsong.options import CampaignAdmin

from .models import ExtendedContact, SaleEmail


@modeladmin_register
class SaleEmailAdmin(CampaignAdmin):
    campaign = SaleEmail
    menu_label = 'Sale Email'
    menu_icon = 'mail'
    menu_order = 200
    contact_class = ExtendedContact


# You may want to add your own modeladmin here to list/edit/add contacts
@modeladmin_register
class ContactAdmin(ModelAdmin):
    model = ExtendedContact
    menu_label = 'Contacts'
    menu_icon = 'user'
    list_diplay = ('email', 'first_name', 'last_name', 'location')

Filtering on contact properties

You might want to only send a campaign to a subset of you Contact models. Creating a filter using django-filter and adding it to the CampaignAdmin allows users to filter on any property.

filters.py

from django_filters import FilterSet
from django_filters.filters import AllValuesFilter

from .models import ExtendedContact


class ContactFilter(FilterSet):
    location = AllValuesFilter()

    class Meta:
        model = ExtendedContact
        fields = ('location',)

wagtail_hooks.py

from wagtail.contrib.modeladmin.options import modeladmin_register
from birdsong.options import CampaignAdmin

from .filters import ContactFilter
from .models import ExtendedContact, SaleEmail


@modeladmin_register
class SaleEmailAdmin(CampaignAdmin):
    campaign = SaleEmail
    menu_label = 'Sale Email'
    menu_icon = 'mail'
    menu_order = 200
    contact_class = ExtendedContact
    contact_filter_class = ContactFilter

Users will now be able to send campaigns to a subset of contacts base on location.

Unsubscribe url

Included in birdsong is a basic way to contacts to unsubscribe, just include the url configuration and add the unsubscribe url to your email template.

urls.py

from birdsong import urls as birdsong_urls
from django.urls import include, path

urlpatterns = [
    ...
    path('mail/', include(birdsong_urls)),
    ...
]

sale_email.html

{% extends "birdsong/mail/base_email.html" %}

{% block email_body %}
<mj-section>
    <mj-column>
        <mj-text>Hello {{ contact.email }}!</mj-text>
        {% for b in self.body %}
            {{ b }}
        {% endfor %}
    </mj-column>
</mj-section>
<mj-section>
    Click <a href="{{ site.full_url }}{% url 'birdsong:unsubscribe' contact.id %}">here</a> to unsubscribe.
</mj-section>
{% endblock email_body %}

## Future features:

  • Tests!

  • Backends other thans SMTP for sending emails so analytics can be gathered (email opened, bounced etc)

  • In place previewing of email templates like wagtail-livepreview

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail-birdsong-0.0.3.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

wagtail_birdsong-0.0.3-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file wagtail-birdsong-0.0.3.tar.gz.

File metadata

  • Download URL: wagtail-birdsong-0.0.3.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.5

File hashes

Hashes for wagtail-birdsong-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c1e0b33466d9ccbec9bdf1b43f8c2d10dd216615ae22c31699d474af38b2b19d
MD5 55eb951d420f9aae9726c2ea0da1f8b6
BLAKE2b-256 a0cdcb1a43aa75c8dad7a52522c3e8e5edf47254fb8fbc43118a89385f63c610

See more details on using hashes here.

File details

Details for the file wagtail_birdsong-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: wagtail_birdsong-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.5

File hashes

Hashes for wagtail_birdsong-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 153ff908ba8855b1b70d234f25a2b3002c095f6c236edf639aa2408d02a5f906
MD5 aae9fbfdc648839bf1a12de877a583e6
BLAKE2b-256 9fc03db3ef67166b2b574a9e69ff7c996846183d8cf5965b71a58f51cc92f8b4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page