Package: django-mailviews
Class-based email views for the Django framework, including a message previewer.IntroductionRendering and sending emails in Django can quickly become repetitive and
error-prone. By encapsulating message rendering within view classes, you can
easily compose messages in a structured and clear manner.Basic Usagefrommailviews.messagesimportEmailMessageView# Subclass the `EmailMessageView`, adding the templates you want to render.classWelcomeMessageView(EmailMessageView):subject_template_name='emails/welcome/subject.txt'body_template_name='emails/welcome/body.txt'# Instantiate and send a message.message=WelcomeMessageView().send(extra_context={'user':user,},to=(user.email,))This isn’t actually the best pattern for sending messages to a user – read the
notes under “Best Practices” for a better approach.Using the Preview SiteRegistering URLs and Enabling DiscoveryAddmailviewsto your project’sINSTALLED_APPSsetting.Add the following somewhere within your project’sROOT_URLCONF:frommailviews.previewsimportautodiscover,siteautodiscover()urlpatterns=patterns('',url(regex=r'^emails/',view=site.urls),)The preview index will now be available at theemails/URL.Creating Preview ClassesTo create a simple preview, add aemails.previewssubmodule within one of yourINSTALLED_APPS, and create a new subclass ofPreview.frommailviews.previewsimportPreview,sitefromexample.emails.viewsimportWelcomeMessageView# Define a new preview class.classBasicPreview(Preview):message_view=WelcomeMessageView# Register the preview class with the preview index.site.register(BasicPreview)You can see more detailed examples within thetest suiteor in the code documentation formailviews.previews.Customizing Preview BehaviorYou can also use Django forms to customize the creation of message previews by
adding aform_classattribute to yourPreviewsubclasses. The form must
provide aget_message_view_kwargsmethod that returns a the keyword arguments
to be used when constructing the message view instance.Best PracticesTry and avoid using theextra_contextargument when sending emails.
Instead, create anEmailMessageViewsubclass whose constructor accepts
as arguments all of the objects that you require to generate the context and
send the message. For example, the code shown in “Basic Usage” could written
instead as the following:frommailviews.messagesimportEmailMessageViewclassWelcomeMessageView(EmailMessageView):subject_template_name='emails/welcome/subject.txt'body_template_name='emails/welcome/body.txt'def__init__(self,user,*args,**kwargs):super(WelcomeMessageView,self).__init__(*args,**kwargs)self.user=userdefget_context_data(self,**kwargs):context=super(WelcomeMessageView,self).get_context_data(**kwargs)context['user']=self.userreturncontextdefrender_to_message(self,*args,**kwargs):assert'to'notinkwargs# this should only be sent to the userkwargs['to']=(self.user.email,)returnsuper(WelcomeMessageView,self).render_to_message(*args,**kwargs)# Instantiate and send a message.WelcomeMessageView(user).send()In fact, you might find it helpful to encapsulate the above “message for a user”
pattern into a mixin or subclass that provides a standard abstraction for all
user-related emails. (This is left as an exercise for the reader.)Testing and DevelopmentTested on Python 2.6, 2.7 and 3.4 (where supported by Django) and Django
versions 1.3 through 1.8. (For specific versions supported, see the Travis or
Tox build matrices.) To run tests against the entire build matrix locally, runmaketest-matrix(ortox, if already installed.)DevelopmentTo install the project in development mode, run:makedevelopThis installs dependencies, as well as builds static assets.TestingTo run the test suite against your installed Django version, run:python-mmailviews.testsTo view an example preview site, you can start a test server by running:python-mmailviews.tests.managerunserverIntegration with Third-Party ApplicationsAll tests will automatically be run using the Django test runner when you run
the tests for your own projects if you usepython manage.py testandmailviewsis within yoursettings.INSTALLED_APPS.CoverageTo generate a Coverage report usingcoverage.py, run:coveragerun--source=.-mmailviews.tests