|
2 | 2 |
|
3 | 3 | Messages |
4 | 4 | ======== |
| 5 | +The dyn.mm.message module is where you'll find the ability to easily automate the |
| 6 | +sending of messages. |
5 | 7 |
|
6 | | -dyn.mm.message module |
7 | | ---------------------- |
| 8 | +send_message |
| 9 | +------------ |
| 10 | +The :func:`~dyn.mm.message.send_message` function allows a user to quickly fire off |
| 11 | +an email via the Message Management API |
8 | 12 |
|
9 | | -.. automodule:: dyn.mm.message |
| 13 | +.. autofunction:: dyn.mm.message.send_message |
| 14 | + |
| 15 | +Using send_message |
| 16 | +^^^^^^^^^^^^^^^^^^ |
| 17 | +Below is just a quick example on how to use :func:`~dyn.mm.message.send_message`:: |
| 18 | + |
| 19 | + >>> from dyn.mm.message import send_message |
| 20 | + >>> from_email = '[email protected]' |
| 21 | + >>> to_email = '[email protected]' |
| 22 | + >>> subject = 'A Demo Email' |
| 23 | + >>> content = 'Hello User, thank you for registering at http://mysite.com!' |
| 24 | + >>> send_message(from_email, to_email, subject, body=content) |
| 25 | + |
| 26 | + |
| 27 | +EMail |
| 28 | +----- |
| 29 | + |
| 30 | +.. autoclass:: dyn.mm.message.EMail |
| 31 | + :members: |
| 32 | + :undoc-members: |
| 33 | + |
| 34 | +Using the EMail Base class |
| 35 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 36 | +The ability to be able to customize your messages become far more apparent with |
| 37 | +the use of the :class:`~dyn.mm.message.EMail` class as you can see in the example |
| 38 | +below it's very easy to use this class for templating, or even subclassing to make |
| 39 | +sending emails quick and easy:: |
| 40 | + |
| 41 | + >>> from dyn.mm.message import EMail |
| 42 | + >>> from_email = '[email protected]' |
| 43 | + >>> to_email = '[email protected]' |
| 44 | + >>> subject = 'A Demo Email' |
| 45 | + >>> content = 'Hello %s, thank you for registering at http://mysite.com!' |
| 46 | + >>> mailer = EMail(from_email, to_email, subject) |
| 47 | + >>> user_names = ['Jon', 'Ray', 'Carol', 'Margaret'] |
| 48 | + >>> for user_name in user_names: |
| 49 | + ... mailer.body = content % user_name |
| 50 | + ... mailer.send() |
| 51 | + |
| 52 | + |
| 53 | +EMail Subclasses |
| 54 | +---------------- |
| 55 | +Below are some :class:`~dyn.mm.message.EMail` subclasses which provide some additional |
| 56 | +formatting and, hopefully, helpful features. |
| 57 | + |
| 58 | +.. autoclass:: dyn.mm.message.HTMLEMail |
10 | 59 | :members: |
11 | 60 | :undoc-members: |
12 | 61 |
|
| 62 | +.. autoclass:: dyn.mm.message.TemplateEMail |
| 63 | + :members: |
| 64 | + :undoc-members: |
| 65 | + |
| 66 | +.. autoclass:: dyn.mm.message.HTMLTemplateEMail |
| 67 | + :members: |
| 68 | + :undoc-members: |
| 69 | + |
| 70 | +Using the EMail Subclasses |
| 71 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 72 | +The :class:`~dyn.mm.message.HTMLEMail` class is identical to the :class:`~dyn.mm.message.EMail` |
| 73 | +class, with the only difference being that content passed to it's send method will |
| 74 | +be added as the messages HTML content, rather than text content. |
| 75 | + |
| 76 | +The Templating subclasses behave slightly differently. For the :class:`~dyn.mm.message.TemplateEmail` |
| 77 | +class, you provide it a template at construction time, and an iterable with the content |
| 78 | +to substitute into the template at send time. For example:: |
| 79 | + |
| 80 | + >>> from dyn.mm.message import TemplateEmail |
| 81 | + >>> from_email = '[email protected]' |
| 82 | + >>> to_email = '[email protected]' |
| 83 | + >>> subject = 'A Demo Email' |
| 84 | + >>> template = 'Hello %s, thank you for registering at http://mysite.com!' |
| 85 | + >>> mailer = TemplateEmail(from_email, to_email, subject, body=template) |
| 86 | + >>> parameters = ['Jon', 'Ray', 'Carol', 'Margaret'] |
| 87 | + >>> mailer.send(parameters) |
| 88 | + |
| 89 | +Similarly you can use the :class:`~dyn.mm.message.HTMLTemplateEMail` class to template out and send |
| 90 | +multiple HTML formatted emails easily. Let's go over a slightly more complex for that class:: |
| 91 | + |
| 92 | + >>> from textwrap import dedent |
| 93 | + >>> from dyn.mm.message import TemplateEmail |
| 94 | + >>> from_email = '[email protected]' |
| 95 | + >>> to_email = '[email protected]' |
| 96 | + >>> subject = 'A Demo Email' |
| 97 | + >>> template = """ |
| 98 | + <html> |
| 99 | + <h1>What... is the air-speed velocity of an unladen swallow?</h1> |
| 100 | + <h2>What do you mean? An %(choice1) or %(choice2) swallow?</h2> |
| 101 | + </html>""" |
| 102 | + >>> template = dedent(template) |
| 103 | + >>> mailer = HTMLTemplateEmail(from_email, to_email, subject, html=template) |
| 104 | + >>> parameters = {'choice1': 'African', 'choice2': 'European'} |
| 105 | + >>> mailer.send(parameters) |
| 106 | + |
0 commit comments