|
| 1 | +# nbmail.compose |
| 2 | + |
| 3 | +The `compose` module provides a high-level, Pythonic API for building HTML emails using simple building blocks. It's designed for data science workflows where you need to create professional-looking emails without writing raw HTML or MJML. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```python |
| 8 | +from nbmail.compose import compose_email, block_text, block_title, create_blocks |
| 9 | + |
| 10 | +# Simple email |
| 11 | +email = compose_email( |
| 12 | + body=block_text("Hello world!") |
| 13 | +) |
| 14 | + |
| 15 | +# Email with multiple blocks |
| 16 | +email = compose_email( |
| 17 | + title="Weekly Report", |
| 18 | + body=create_blocks( |
| 19 | + block_text("Welcome to this week's update!"), |
| 20 | + block_text("Here's what's new...") |
| 21 | + ) |
| 22 | +) |
| 23 | +``` |
| 24 | + |
| 25 | +## Architecture |
| 26 | + |
| 27 | +The compose module is built on three main concepts: |
| 28 | + |
| 29 | +1. **Blocks** - Individual content units (text, images, plots, spacers, markdown) |
| 30 | +2. **BlockList** - Collections of blocks that can be rendered together |
| 31 | +3. **compose_email()** - The main entry point that converts blocks to MJML and then to Email objects |
| 32 | + |
| 33 | +### Why Blocks? |
| 34 | + |
| 35 | +Blocks provide a simple, composable abstraction over MJML's more complex tag structure. They encapsulate common email patterns (text sections, images, spacers) in an easy-to-use API. |
| 36 | + |
| 37 | +### MJML Under the Hood |
| 38 | + |
| 39 | +Blocks compile to MJML tags internally. MJML provides: |
| 40 | +- Responsive design by default |
| 41 | +- Cross-client compatibility |
| 42 | +- Semantic structure (sections, columns, etc.) |
| 43 | + |
| 44 | +### Inline Attachments |
| 45 | + |
| 46 | +Local images are read as bytes and stored in `Email.inline_attachments` as base64 strings. During sending, these are converted to CID references in the MIME structure. |
| 47 | + |
| 48 | +### Jupyter Display Support |
| 49 | + |
| 50 | +Blocks and BlockLists implement `_repr_html_()` for rich display in notebooks: |
| 51 | + |
| 52 | +```python |
| 53 | +# In a Jupyter notebook: |
| 54 | +block = block_text("Preview me!") |
| 55 | +block # Automatically renders as HTML |
| 56 | +``` |
| 57 | + |
| 58 | +## Dependencies |
| 59 | + |
| 60 | +**Required:** |
| 61 | +- `markdown` - For Markdown processing in text blocks |
| 62 | + |
| 63 | +**Optional:** |
| 64 | +- `plotnine` - For `block_plot()` functionality |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +## API Reference |
| 69 | + |
| 70 | +Complete API documentation is available in the [nbmail reference docs](https://posit-dev.github.io/nbmail/reference/). |
0 commit comments