wuttjamaican.email.handler
¶
Email Handler
- class wuttjamaican.email.handler.EmailHandler(*args, **kwargs)[source]¶
Base class and default implementation for the email handler.
Responsible for sending email messages on behalf of the app.
You normally would not create this directly, but instead call
get_email_handler()
on your app handler.- deliver_message(message, sender=None, recips=None)[source]¶
Deliver a message via SMTP smarthost.
- Parameters:
message – Either a
Message
object or similar, or a string representing the complete message to be sent as-is.sender – Optional sender address to use for delivery. If not specified, will be read from
message
.recips – Optional recipient address(es) for delivery. If not specified, will be read from
message
.
A general rule here is that you can either provide a proper
Message
object, or you must providesender
andrecips
. The logic is not smart enough (yet?) to parse sender/recips from a simple string message.Note also, this method does not (yet?) have robust error handling, so if an error occurs with the SMTP session, it will simply raise to caller.
- Returns:
None
- get_auto_bcc(key)[source]¶
Returns automatic
bcc
recipient address(es) for a message, as determined by config.
- get_auto_cc(key)[source]¶
Returns automatic
cc
recipient address(es) for a message, as determined by config.
- get_auto_html_body(key, context={})[source]¶
Returns automatic
html_body
content for a message, as determined by config. This renders a template with the given context.
- get_auto_sender(key)[source]¶
Returns automatic
sender
address for a message, as determined by config.
- get_auto_subject(key, context={}, rendered=True)[source]¶
Returns automatic
subject
line for a message, as determined by config.This calls
get_auto_subject_template()
and then renders the result using the given context.- Parameters:
rendered – If this is
False
, the “raw” subject template will be returned, instead of the final/rendered subject text.
- get_auto_subject_template(key)[source]¶
Returns the template string to use for automatic subject line of a message, as determined by config.
In many cases this will be a simple string and not a “template” per se; however it is still treated as a template.
The template returned from this method is used to render the final subject line in
get_auto_subject()
.
- get_auto_to(key)[source]¶
Returns automatic
to
recipient address(es) for a message, as determined by config.
- get_auto_txt_body(key, context={})[source]¶
Returns automatic
txt_body
content for a message, as determined by config. This renders a template with the given context.
- make_auto_message(key, context={}, **kwargs)[source]¶
Make a new email message using config to determine its properties, and auto-generating body from a template.
Once everything has been collected/prepared,
make_message()
is called to create the final message, and that is returned.- Parameters:
key – Unique key for this particular “type” of message. This key is used as a prefix for all config settings and template names pertinent to the message.
context – Context dict used to render template(s) for the message.
**kwargs – Any remaining kwargs are passed as-is to
make_message()
. More on this below.
- Returns:
Message
object.
This method may invoke some others, to gather the message attributes. Each will check config, or render a template, or both. However if a particular attribute is provided by the caller, the corresponding “auto” method is skipped.
- make_message(**kwargs)[source]¶
Make and return a new email message.
This is the “raw” factory which is simply a wrapper around the class constructor. See also
make_auto_message()
.- Returns:
Message
object.
- send_email(key=None, context={}, message=None, sender=None, recips=None, **kwargs)[source]¶
Send an email message.
This method can send a
message
you provide, or it can construct one automatically from key/config/templates.- Parameters:
key – Indicates which “type” of automatic email to send. Used to lookup config settings and template files.
context – Context dict for rendering automatic email template(s).
message – Optional pre-built message instance, to send as-is.
sender –
Optional sender address for the message/delivery.
If
message
is not provided, then thesender
(if provided) will also be used when constructing the auto-message (i.e. to set theFrom:
header).In any case if
sender
is provided, it will be used for the actual SMTP delivery.recips –
Optional list of recipient addresses for delivery. If not specified, will be read from the message itself (after auto-generating it, if applicable).
Note
This param does not affect an auto-generated message; it is used for delivery only. As such it must contain all true recipients.
If you provide the
message
but not therecips
, the latter will be read from message headers:To:
,Cc:
andBcc:
If you want an auto-generated message but also want to override various recipient headers, then you must provide those explicitly:
context = {'data': [1, 2, 3]} app.send_email('foo', context, to='me@example.com', cc='bobby@example.com')
**kwargs – Any remaining kwargs are passed along to
make_auto_message()
. So, not used if you provide themessage
.