Why Code Generation?
Using string literals for template slugs and merge tags is error-prone:LettrTemplate::WelcomEmail doesn’t exist, and WelcomeEmailData requires name, not nme.
Generate Everything at Once
Thelettr:init command can generate all code for you during setup:
Template Enum
Generate an enum containing all your template slugs:app/Enums/LettrTemplate.php:
.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:app/Dto/Lettr/:
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
Mailable Classes
Generate Mailable classes for your templates:app/Mail/Lettr/:
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:Recommended Workflow
- Create or update a template in the Lettr dashboard
- Run
php artisan lettr:generate-enumandphp artisan lettr:generate-dtos - Commit the generated files
- Use the enum and DTO in your application code
Configuration
Customize paths and names inconfig/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