diff --git a/README.rst b/README.rst index b7c4fff..59b8239 100644 --- a/README.rst +++ b/README.rst @@ -93,17 +93,27 @@ Send and get response from smtp server: .. code-block:: python r = message.send(to=('John Brown', 'jbrown@gmail.com'), + render={'field1': 'X'}, smtp={'host':'smtp.mycompany.com', 'port': 465, 'ssl': True}) assert r.status_code == 250 -Or send via Django email backend: + +Django +------ + +DjangoMessage helper sends via django configured email backend: .. code-block:: python - from django.core.mail import get_connection - from emails.message import DjangoMessageProxy - c = django.core.mail.get_connection() - c.send_messages([DjangoMessageProxy(message), ]) + from emails.django_ import DjangoMessage as Message + message = Message(...) + message.send(mail_to=('John Brown', 'jbrown@gmail.com'), + context={'field1': 'X'}) + +Flask +----- + +For flask integration take a look at `flask-emails `_ HTML transformer diff --git a/emails/loader/__init__.py b/emails/loader/__init__.py index d50697a..4989e4e 100644 --- a/emails/loader/__init__.py +++ b/emails/loader/__init__.py @@ -1,6 +1,7 @@ # encoding: utf-8 import os import os.path +from emails.loader.local_store import FileNotFound from emails.compat import to_unicode from emails.compat import urlparse from emails import Message @@ -9,6 +10,18 @@ from emails.loader.helpers import guess_charset +class LoadError(Exception): + pass + + +class IndexFileNotFound(LoadError): + pass + + +class InvalidHtmlFile(LoadError): + pass + + def from_url(url, message_params=None, requests_params=None, **kwargs): def _make_base_url(url): @@ -37,7 +50,13 @@ def _make_base_url(url): def from_directory(directory, index_file=None, message_params=None, **kwargs): store = local_store.FileSystemLoader(searchpath=directory) - index_file_name = store.find_index_file(index_file) + + try: + index_file_name = store.find_index_file(index_file) + except FileNotFound: + # reraise another exception + raise IndexFileNotFound('html file not found') + dirname, _ = os.path.split(index_file_name) if dirname: store.base_path = dirname @@ -45,6 +64,8 @@ def from_directory(directory, index_file=None, message_params=None, **kwargs): message_params = message_params or {} message = Message(html=store.content(index_file_name, is_html=True, guess_charset=True), **message_params) message.create_transformer(local_loader=store, requests_params=kwargs.pop('requests_params', None)) + if message.transformer.tree is None: + raise InvalidHtmlFile("Error parsing file '%s'" % index_file_name) message.transformer.load_and_transform(**kwargs) message.transformer.save() return message @@ -56,14 +77,22 @@ def from_file(filename, **kwargs): def from_zip(zip_file, message_params=None, **kwargs): store = local_store.ZipLoader(file=zip_file) - index_file_name = store.find_index_file() - dirname, index_file_name = os.path.split(index_file_name) + + try: + origin_index_file_name = store.find_index_file() + except FileNotFound: + # reraise another exception + raise IndexFileNotFound('html file not found') + + dirname, index_file_name = os.path.split(origin_index_file_name) if dirname: store.base_path = dirname message_params = message_params or {} message = Message(html=store.content(index_file_name, is_html=True, guess_charset=True), **message_params) message.create_transformer(local_loader=store, requests_params=kwargs.pop('requests_params', None)) + if message.transformer.tree is None: + raise InvalidHtmlFile("Error parsing file '%s'" % origin_index_file_name) message.transformer.load_and_transform(**kwargs) message.transformer.save() return message diff --git a/emails/loader/local_store.py b/emails/loader/local_store.py index 16eb237..c0f58cc 100644 --- a/emails/loader/local_store.py +++ b/emails/loader/local_store.py @@ -83,8 +83,9 @@ def find_index_file(self, filename=None): else: html_files.append(filename) - if html_files: - return html_files[0] + # Ignore hidden files (filename started with dot) + for fn in filter(lambda p: not os.path.basename(p).startswith('.'), html_files): + return fn raise FileNotFound('index html') diff --git a/emails/testsuite/message/data/pushkin.jpg b/emails/testsuite/message/data/pushkin.jpg new file mode 100644 index 0000000..744c0eb Binary files /dev/null and b/emails/testsuite/message/data/pushkin.jpg differ diff --git a/emails/testsuite/message/helpers.py b/emails/testsuite/message/helpers.py index a0c8cb8..c79e899 100644 --- a/emails/testsuite/message/helpers.py +++ b/emails/testsuite/message/helpers.py @@ -3,7 +3,6 @@ import os import emails -from emails.compat import StringIO from emails.template import JinjaTemplate TO_EMAIL = 'jbrown@hotmail.tld' @@ -12,6 +11,9 @@ TRAVIS_CI = os.environ.get('TRAVIS') HAS_INTERNET_CONNECTION = not TRAVIS_CI +ROOT = os.path.dirname(__file__) + + def common_email_data(**kw): T = JinjaTemplate data = {'charset': 'utf-8', @@ -23,8 +25,8 @@ def common_email_data(**kw): 'headers': {'X-Mailer': 'python-emails'}, 'message_id': emails.MessageID(), 'attachments': [ - {'data': 'aaa', 'filename': 'κατάσχεση.ics'}, - {'data': 'bbb', 'filename': 'map.png'} + {'data': 'Sample text', 'filename': 'κατάσχεση.txt'}, + {'data': open(os.path.join(ROOT, 'data/pushkin.jpg'), 'rb'), 'filename': 'Пушкин А.С.jpg'} ]} if kw: data.update(kw)