Skip to main content
The Lettr PHP SDK offers multiple ways to send emails, from quick one-liners to the full-featured email builder. This page covers every approach so you can choose the one that fits your use case.

Quick Send Methods

For simple emails, use the shorthand methods. These are convenience wrappers around the email builder that handle the most common use cases with minimal code.

HTML Email

The sendHtml() method is perfect for simple HTML emails where you don’t need advanced features like tracking configuration or metadata.
When providing HTML content, the SDK automatically enables CSS inlining and template substitutions by default. You can disable these behaviors using the email builder for more control.

Plain Text Email

The from parameter accepts either a string email address or an EmailAddress value object to set a display name. The to parameter accepts a single email string or an array of recipients (maximum 50).
You can send both HTML and plain text in the same email by using the email builder with both ->html() and ->text() methods. Mail clients that don’t support HTML will display the plain text version.

Template Email

The substitutionData array keys correspond to merge tags in your Lettr template (e.g., {{name}}). See Merge Tags & Template Language for the full syntax including conditionals and loops. For more advanced template usage including versioning, see the Templates page.

Email Builder

For complex emails that require multiple features, use the fluent email builder. This gives you access to every option the Lettr API supports in a chainable interface:
The builder is useful when you need to combine multiple features in a single email — for example, HTML content with a plain text fallback, tracking configuration, metadata for analytics, and attachments.
All builder methods return the builder instance, so you can chain as many method calls as needed. The order of method calls doesn’t matter — the builder collects all the data and sends it to the API when you call send().

Templates with Substitution Data

Send using a Lettr template with dynamic data:
The useTemplate() method accepts an optional version parameter to pin a specific template version. See Templates for details. When to use versioning:
  • Production safety — Pin to a known-good version so publishing a new draft doesn’t affect live emails
  • A/B testing — Send different versions to different cohorts and compare metrics
  • Gradual rollout — Test a new version with a subset of users before publishing

Attachments

Add attachments from file paths or binary data:
When attaching files, the SDK automatically:
  • Base64-encodes the file content
  • Detects the MIME type if not provided
  • Validates file size and format
Large attachments increase email size and can affect deliverability. Keep attachments under 10 MB total per email. For larger files, consider uploading to cloud storage and including a download link in the email instead.

Email Options

Fine-tune email behavior with tracking and processing options:
Click and open tracking are enabled by default. For transactional emails like password resets or order confirmations, leave tracking enabled — the data helps you diagnose delivery issues. For privacy-sensitive communications, disable tracking explicitly.

Handling Responses

Every send method returns a SendEmailResponse with details about the request:
The requestId is a unique identifier for this email transmission. Store it in your database to correlate emails with delivery events later. You can use it to query the Lettr API for detailed delivery information including opens, clicks, bounces, and complaints.
Save the requestId in your application’s database alongside the user record or transaction. This lets you look up delivery status, debug issues, and provide customer support without searching through logs.

Error Handling

The SDK throws typed exceptions for different API errors, making it easy to handle specific failure cases:
Always wrap email sending in a try-catch block in production. Network failures and API errors can happen at any time, and unhandled exceptions will crash your application.

What’s Next

Templates

Manage and send Lettr templates with versioning

Email Tracking

Track opens, clicks, and engagement

Metadata

Attach custom data to emails for analytics

Error Handling

Understand errors and retry strategies