Package lamson :: Module html :: Class HtmlMail
[hide private]
[frames] | no frames]

Class HtmlMail

source code


Acts as a lamson.mail.MailResponse generator that produces a properly formatted HTML mail message, including inline CSS applied to all HTML tags.

Instance Methods [hide private]
 
__init__(self, css_template, html_template, variables={}, wiki=markdown)
You pass in a CleverCSS template (it'll be run through the template engine before CleverCSS), the html_template, and any variables that the CSS template needs.
source code
 
load_css(self, css_template, variables)
If you want to change the CSS, simply call this with the new CSS and variables.
source code
 
reduce_tags(self, name, tags)
Used mostly internally to find all the tags that fit the given CSS selector.
source code
 
apply_styles(self, html)
Used mostly internally but helpful for testing, this takes the given HTML and applies the configured CSS you've set.
source code
 
render(self, variables, content_template, pretty=False)
Works like lamson.view.render, but uses apply_styles to modify the HTML with the configured CSS before returning it to you.
source code
 
respond(self, variables, content, **kwd)
Works like lamson.view.respond letting you craft a lamson.mail.MailResponse immediately from the results of a lamson.html.HtmlMail.render call.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, css_template, html_template, variables={}, wiki=markdown)
(Constructor)

source code 

You pass in a CleverCSS template (it'll be run through the template engine before CleverCSS), the html_template, and any variables that the CSS template needs.

The CSS template is processed once, the html_template is processed each time you call render or respond.

If you don't like markdown, then you can set the wiki variable to any callable that processes your templates.

Overrides: object.__init__

load_css(self, css_template, variables)

source code 

If you want to change the CSS, simply call this with the new CSS and variables. It will change internal state so that later calls to render or respond use the new CSS.

reduce_tags(self, name, tags)

source code 

Used mostly internally to find all the tags that fit the given CSS selector. It's fairly primitive, working only on tag names, classes, and ids. You shouldn't get too fancy with the CSS you create.

apply_styles(self, html)

source code 

Used mostly internally but helpful for testing, this takes the given HTML and applies the configured CSS you've set. It returns a BeautifulSoup object with all the style attributes set and nothing else changed.

render(self, variables, content_template, pretty=False)

source code 

Works like lamson.view.render, but uses apply_styles to modify the HTML with the configured CSS before returning it to you.

If you set the pretty=True then it will prettyprint the results, which is a waste of bandwidth, but helps when debugging.

Remember that content_template is run through the template system, and then processed with self.wiki (defaults to markdown). This let's you do template processing and write the HTML contents like you would an email.

You could also attach the content_template as a text version of the message for people without HTML. Simply set the .Body attribute of the returned lamson.mail.MailResponse object.

respond(self, variables, content, **kwd)

source code 

Works like lamson.view.respond letting you craft a lamson.mail.MailResponse immediately from the results of a lamson.html.HtmlMail.render call. Simply pass in the From, To, and Subject parameters you would normally pass in for MailResponse, and it'll craft the HTML mail for you and return it ready to deliver.

A slight convenience in this function is that if the Body kw parameter equals the content parameter, then it's assumed you want the raw markdown content to be sent as the text version, and it will produce a nice dual HTML/text email.