compose.block_image

compose.block_image(file, alt='', width=520, align='center', float='none')

Create a block containing an embedded image.

This function returns a Block with an MJML image tag. For local files, the image bytes are stored and later converted to CID references by mjml_to_email().

Parameters

file : str

Path to local image file or HTTP(S) URL. Local files are automatically read and embedded. URLs (http://, https://, or //) are used directly.

alt : str = ''

Alt text for accessibility. Default is empty string.

width : Union[int, str] = 520

Image width. Can be an integer (interpreted as pixels, e.g., 520 → “520px”) or a CSS string (e.g., “600px”, “50%”). Default is 520 (pixels).

align : Literal['center', 'left', 'right', 'inline'] = 'center'

Block-level alignment: “center”, “left”, “right”, or “inline”.

float : Literal['none', 'left', 'right'] = 'none'

CSS float value for text wrapping: “none”, “left”, or “right”. When float is not “none”, it takes precedence and wraps content around the image.

Returns

: Block

A block containing the image.

Raises

: FileNotFoundError

If a local file path is provided but the file does not exist.

Examples

from nbmail.compose import block_image, create_blocks, block_text, compose_email, md

compose_email(
    title="Product Feature",
    body=create_blocks(
        block_text(md("Check this out:")),
        block_image("https://fastly.picsum.photos/id/630/300/200.jpg?hmac=dSM5_yM5Z9Pb3CX6OviVW3dEbyHmkD04otrIKU2LQ50", alt="Product image", width="500px")
    )
)

Product Feature

Check this out:

Product image

Notes

  • Images from local files are converted to inline attachments with CID references during email processing by mjml_to_email().
  • If float is not “none”, it takes precedence and overrides align.