diff --git a/docs/install.rst b/docs/install.rst index f8eb840..9ea6e96 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -15,3 +15,9 @@ To use HTML transformation features (CSS inlining, image embedding, loading from .. code-block:: bash $ pip install emails[html] + +To use Jinja2 templates (the ``T()`` shortcut): + +.. code-block:: bash + + $ pip install emails[jinja] diff --git a/emails/template/jinja_template.py b/emails/template/jinja_template.py index 51d608f..381ac3e 100644 --- a/emails/template/jinja_template.py +++ b/emails/template/jinja_template.py @@ -16,7 +16,13 @@ def __init__(self, template_text, environment=None): self.environment = environment else: if 'jinja2' not in globals(): - globals()['jinja2'] = __import__('jinja2') + try: + globals()['jinja2'] = __import__('jinja2') + except ImportError: + raise ImportError( + "jinja2 is required for template support. " + "Install it with: pip install emails[jinja]" + ) self.environment = jinja2.Environment(**self.DEFAULT_JINJA_ENVIRONMENT) def compile_template(self): diff --git a/setup.py b/setup.py index 507c4fc..ac120d9 100644 --- a/setup.py +++ b/setup.py @@ -128,6 +128,7 @@ def find_version(*file_paths): install_requires=['python-dateutil', 'puremagic', 'dkimpy'], extras_require={ 'html': ['cssutils', 'lxml', 'chardet', 'requests', 'premailer'], + 'jinja': ['jinja2'], }, zip_safe=False, classifiers=(