Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/lavr/flask-emails>`_


HTML transformer
Expand Down
35 changes: 32 additions & 3 deletions emails/loader/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -37,14 +50,22 @@ 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

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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions emails/loader/local_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Binary file added emails/testsuite/message/data/pushkin.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions emails/testsuite/message/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os

import emails
from emails.compat import StringIO
from emails.template import JinjaTemplate

TO_EMAIL = 'jbrown@hotmail.tld'
Expand All @@ -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',
Expand All @@ -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)
Expand Down