Skip to main content
Generate type-safe PHP code from your Lettr templates. Catch errors at compile time instead of runtime. The SDK provides three Artisan commands that connect to the Lettr API, read your templates and their merge tags, and generate PHP classes you can use in your application. This means your IDE can autocomplete template slugs, enforce required merge tags, and flag typos before you deploy.

Why Code Generation?

Using string literals for template slugs and merge tags is error-prone:
With generated code, your IDE catches mistakes immediately:
The first example silently sends an email with the wrong template slug and missing merge tags. The second example fails at compile time with clear error messages — LettrTemplate::WelcomEmail doesn’t exist, and WelcomeEmailData requires name, not nme.

Generate Everything at Once

The lettr:init command can generate all code for you during setup:
Or generate individually using the commands below.

Template Enum

Generate an enum containing all your template slugs:
This creates app/Enums/LettrTemplate.php:
The command fetches all templates from your Lettr account (using the API key in your .env) and generates a backed enum case for each one. The case name is a PascalCase version of the template slug.

Usage

Benefits

  • Autocomplete - Your IDE suggests available templates
  • Refactoring - Rename templates safely across your codebase
  • Discovery - See all templates without leaving your editor

Template DTOs

Generate data transfer objects for each template’s merge tags:
This creates a DTO for each template in app/Dto/Lettr/:
The command reads each template’s merge tags from the Lettr API and generates a DTO class with typed constructor parameters. Required merge tags become required parameters; optional ones get default null values. The toArray() method strips null values so only provided merge tags are sent.

Usage

Benefits

  • Required fields - Constructor enforces required merge tags
  • Type hints - Know exactly what data each template needs
  • Documentation - DTOs serve as living documentation
Combine DTOs with the template enum for maximum safety:

Mailable Classes

Generate Mailable classes for your templates:
This creates Mailables in app/Mail/Lettr/:
Generated Mailables extend LettrMailable and follow standard Laravel conventions. The constructor takes typed parameters for each merge tag, and the build() method maps them to the template’s substitution data.

Usage

Benefits

  • Standard Laravel - Works with queues, events, and testing
  • Encapsulation - Template logic stays in one place
  • Familiar API - Same patterns you already use

Keeping Code in Sync

When you add or modify templates in Lettr, regenerate your code:
Generated files are safe to commit to version control. They act as a snapshot of your templates at generation time. If a template is renamed or a merge tag is added in Lettr, the generated code won’t update automatically — run the commands again to pick up changes.
Add these commands to your CI/CD pipeline to ensure code stays in sync with your templates. If the generated output differs from what’s committed, you know someone updated a template without regenerating.
  1. Create or update a template in the Lettr dashboard
  2. Run php artisan lettr:generate-enum and php artisan lettr:generate-dtos
  3. Commit the generated files
  4. Use the enum and DTO in your application code

Configuration

Customize paths and names in config/lettr.php:

Artisan Commands Reference

What’s Next

Sending Emails

Put your generated code to use

Templates

Template versioning, projects, and sync commands

API Reference

Complete REST API documentation

Best Practices

Sending best practices and deliverability tips