2. Messages¶
The dyn.mm.message module is where you’ll find the ability to easily automate the sending of messages.
2.1. send_message¶
The send_message()
function allows a user to quickly fire off
an email via the Message Management API
-
dyn.mm.message.
send_message
(from_field, to, subject, cc=None, body=None, html=None, replyto=None, xheaders=None)[source]¶ Create and send an email on the fly. For information on the arguments accepted by this function see the documentation for
EMail
2.1.1. Using send_message¶
Below is just a quick example on how to use send_message()
:
>>> from dyn.mm.message import send_message
>>> from_email = 'user@email.com'
>>> to_email = 'your@email.com'
>>> subject = 'A Demo Email'
>>> content = 'Hello User, thank you for registering at http://mysite.com!'
>>> send_message(from_email, to_email, subject, body=content)
2.2. EMail¶
-
class
dyn.mm.message.
EMail
(from_field, to, subject, cc=None, body=None, html=None, replyto=None, xheaders=None)[source]¶ Create an and Send it from one of your approved senders
-
__init__
(from_field, to, subject, cc=None, body=None, html=None, replyto=None, xheaders=None)[source]¶ Create a new
EMail
objectParameters: - from_field – Sender email address - This can either be an email address or a properly formatted from header (example: “From Name” <example@email.com>). NOTE: The sender must be one of your account’s Approved Senders
- to – A list of Address(es) or a single Address that the email will be sent to — This/These can either be an email address or a properly formatted from header (example: “To Name” <example@email.com>). The To field in the email will contain all the addresses when it is sent out and will be sent to all the addresses.
- subject – The subject of the email being sent
- cc – Address(es) to copy the email to - This can either be an email address or a properly formatted cc header (example: “cc Name” <example@email.com>). For multiple addresses, each address must have its own ‘cc’ field. (example: cc = “example1@email.com”, cc = “example2@email.com”).
- body – The plain/text version of the email; this field may be encoded in Base64 (recommended), quoted-printable, 8-bit, or 7-bit.
- html – The text/html version of the email; this field may be encoded in 7-bit, 8-bit, quoted-printable, or base64.
- replyto – The email address for the recipient to reply to. If left blank, defaults to the from address.
- xheaders – Any additional custom X-headers to send in the email - Pass the X-header’s name as the field name and the X-header’s value as the value (example: x-demonheader=zoom).
-
send
(content=None)[source]¶ Send the content of this
Email
object to the provided list of recipients.Parameters: content – The optional content field can be used to overrwrite, or to specify the actual content of the body of the message. Note: If content, this instance’s body, and this instance’s html fields are all None, then a DynInvalidArgumentError
will be raised.
-
uri
= '/send'¶
-
2.2.1. Using the EMail Base class¶
The ability to be able to customize your messages become far more apparent with
the use of the EMail
class as you can see in the example
below it’s very easy to use this class for templating, or even subclassing to make
sending emails quick and easy:
>>> from dyn.mm.message import EMail
>>> from_email = 'user@email.com'
>>> to_email = 'your@email.com'
>>> subject = 'A Demo Email'
>>> content = 'Hello %s, thank you for registering at http://mysite.com!'
>>> mailer = EMail(from_email, to_email, subject)
>>> user_names = ['Jon', 'Ray', 'Carol', 'Margaret']
>>> for user_name in user_names:
... mailer.body = content % user_name
... mailer.send()
2.3. EMail Subclasses¶
Below are some EMail
subclasses which provide some additional
formatting and, hopefully, helpful features.
-
class
dyn.mm.message.
HTMLEMail
(from_field, to, subject, cc=None, body=None, html=None, replyto=None, xheaders=None)[source]¶ EMail
subclass with an overridden send method for specifying html content on the fly-
send
(content=None)[source]¶ Send the content of this
Email
object to the provided list of recipients.Parameters: content – The optional content field can be used to overrwrite, or to specify the actual content of the html of the message. Note: If content, this instance’s body, and this instance’s html fields are all None, then a DynInvalidArgumentError
will be raised.
-
-
class
dyn.mm.message.
TemplateEMail
(from_field, to, subject, cc=None, body=None, html=None, replyto=None, xheaders=None)[source]¶ EMail
subclass which treats it’s bodytext attribute as a template. Allowing you to send out chains of emails by only writing the templated email once, and then specifying an iterable with the formatting content at send time.-
send
(formatters=None)[source]¶ Send the content of this
Email
object to the provided list of recipients.Parameters: formatters – Any iterable containing the data you wish inserted into your template. Unlike in the EMail
class this field is not optional and will raise aDynInvalidArgumentError
if not provided. This exception will also be raised if this instances bodytext attribute has not also been set.
-
-
class
dyn.mm.message.
HTMLTemplateEMail
(from_field, to, subject, cc=None, body=None, html=None, replyto=None, xheaders=None)[source]¶ Similar to the
TemplateEMail
class theHTMLEMail
subclass which treats it’s bodyhtml attribute as a template. Allowing you to send out chains of emails by only writing the templated html email once, and then specifying an iterable with the formatting content at send time.-
send
(formatters=None)[source]¶ Send the content of this
Email
object to the provided list of recipients.Parameters: formatters – Any iterable containing the data you wish inserted into your html template. Unlike in the HTMLEMail
class this field is not optional and will raise aDynInvalidArgumentError
if not provided. This exception will also be raised if this instances bodyhtml attribute has not also been set.
-
2.3.1. Using the EMail Subclasses¶
The HTMLEMail
class is identical to the EMail
class, with the only difference being that content passed to it’s send method will
be added as the messages HTML content, rather than text content.
The Templating subclasses behave slightly differently. For the TemplateEmail
class, you provide it a template at construction time, and an iterable with the content
to substitute into the template at send time. For example:
>>> from dyn.mm.message import TemplateEmail
>>> from_email = 'user@email.com'
>>> to_email = 'your@email.com'
>>> subject = 'A Demo Email'
>>> template = 'Hello %s, thank you for registering at http://mysite.com!'
>>> mailer = TemplateEmail(from_email, to_email, subject, body=template)
>>> parameters = ['Jon', 'Ray', 'Carol', 'Margaret']
>>> mailer.send(parameters)
Similarly you can use the HTMLTemplateEMail
class to template out and send
multiple HTML formatted emails easily. Let’s go over a slightly more complex for that class:
>>> from textwrap import dedent
>>> from dyn.mm.message import TemplateEmail
>>> from_email = 'user@email.com'
>>> to_email = 'your@email.com'
>>> subject = 'A Demo Email'
>>> template = """
<html>
<h1>What... is the air-speed velocity of an unladen swallow?</h1>
<h2>What do you mean? An %(choice1) or %(choice2) swallow?</h2>
</html>"""
>>> template = dedent(template)
>>> mailer = HTMLTemplateEmail(from_email, to_email, subject, html=template)
>>> parameters = {'choice1': 'African', 'choice2': 'European'}
>>> mailer.send(parameters)