Skip to content
This repository was archived by the owner on Aug 27, 2019. It is now read-only.

Commit ae7a3da

Browse files
committed
Add, document SendableEmail.create_message. Fixes #35, #36.
1 parent 1ca911e commit ae7a3da

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

docs/quickstart.rst

+17
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ version of the email will use "Jane Doe".
6969
The email will also contain a call-to-action (CTA) that directs the
7070
user to a website.
7171

72+
Registering the email with the gallery
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
7275
Now we just need to let the email example gallery know about the
7376
existence of your new template. Do this by adding the following to
7477
your project's ``settings.py``:
@@ -81,3 +84,17 @@ your project's ``settings.py``:
8184
Now you're set! Start your app and visit ``/examples/``; you should
8285
see the email gallery with a single entry, and be able to view your
8386
example email as HTML and plaintext.
87+
88+
Sending the email
89+
~~~~~~~~~~~~~~~~~
90+
91+
You can create a Django :py:class:`~django.core.mail.EmailMessage` with your
92+
email's :py:meth:`~emailpal.SendableEmail.create_message` method like so:
93+
94+
.. literalinclude:: ../emailpal/tests/test_sendable_email.py
95+
:language: python
96+
:dedent: 4
97+
:start-after: Start create_message doc snippet
98+
:end-before: End create_message doc snippet
99+
100+
Then you can send the message with ``msg.send()``.

docs/sendable-emails.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Sendable emails
66
.. currentmodule:: emailpal
77

88
.. autoclass:: SendableEmail
9-
:members: subject, template_name, send_messages
9+
:members: subject, template_name, create_message

emailpal/sendable_email.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33
from typing import Dict, TypeVar, Generic, Dict, Any, cast # NOQA
44
from django.utils.safestring import SafeString
5-
from django.core.mail import EmailMultiAlternatives
5+
from django.core.mail import EmailMultiAlternatives, EmailMessage
66
from django.template.loader import render_to_string
77
from django.utils.html import strip_tags
88

@@ -64,7 +64,7 @@ def subject(self) -> str:
6464
:py:meth:`str.format` and passed the same context that is
6565
passed to templates when rendering the email, so you can
6666
include context variables via brace notation, e.g.
67-
``Hello {full_name}!``.
67+
``"Hello {full_name}!"``.
6868
'''
6969

7070
pass # pragma: no cover
@@ -74,7 +74,7 @@ def subject(self) -> str:
7474
def template_name(self) -> str:
7575
'''
7676
The path to the template used to render the email, e.g.
77-
``my_app/my_email.html``.
77+
``"my_app/my_email.html"``.
7878
'''
7979

8080
pass # pragma: no cover
@@ -109,14 +109,17 @@ def render_body_as_html(self, ctx: T) -> SafeString:
109109
def render_subject(self, ctx: T) -> str:
110110
return self.subject.format(**self._cast_to_dict(ctx))
111111

112-
def send_messages(self, ctx: T, from_email=None, to=None, bcc=None,
113-
connection=None, attachments=None, headers=None,
114-
alternatives=None, cc=None, reply_to=None) -> int:
112+
def create_message(self, ctx: T, from_email=None, to=None, bcc=None,
113+
connection=None, attachments=None, headers=None,
114+
alternatives=None, cc=None,
115+
reply_to=None) -> EmailMessage:
115116
'''
116-
Renders the email using context specified by ``ctx`` and sends it.
117+
Creates and returns a :py:class:`django.core.mail.EmailMessage`
118+
which contains the plaintext and HTML versions of the email,
119+
using the context specified by ``ctx``.
117120
118121
Aside from ``ctx``, arguments to this method are the
119-
same as those for :py:class:`django.core.mail.EmailMessage`.
122+
same as those for :py:class:`~django.core.mail.EmailMessage`.
120123
'''
121124

122125
msg = EmailMultiAlternatives(
@@ -133,4 +136,4 @@ def send_messages(self, ctx: T, from_email=None, to=None, bcc=None,
133136
reply_to=reply_to,
134137
)
135138
msg.attach_alternative(self.render_body_as_html(ctx), 'text/html')
136-
return msg.send(fail_silently=False)
139+
return msg

emailpal/tests/test_sendable_email.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,17 @@ def mail_connection():
4747

4848

4949
def test_email_sending_works(mail_connection):
50-
ctx = MyContext(full_name='boop jones')
51-
e = MySendableEmail()
52-
num_sent = e.send_messages(
53-
ctx,
50+
# Start create_message doc snippet
51+
msg = MySendableEmail().create_message(
52+
{'full_name': 'boop jones'},
5453
from_email='foo@example.org',
5554
to=['bar@example.org'],
5655
headers={'Message-ID': 'blah'},
57-
connection=mail_connection
5856
)
57+
# End create_message doc snippet
58+
59+
msg.connection = mail_connection
60+
num_sent = msg.send(fail_silently=False)
5961
assert num_sent == 1
6062
assert len(mail.outbox) == 1
6163
msg = mail.outbox[0]

0 commit comments

Comments
 (0)