Skip to main content

Prerequisites

Before getting started, you’ll need to:
  1. Authorize your sending domain
  2. Create and retrieve your SendLayer API key

Installation

Install the SendLayer PHP SDK using Composer:

Storing your API key

Create a .env file in your project root and add your API key:
.env
Read those values through a config file rather than calling env() from application code. Config caching in production returns null for direct env() calls outside config files.
config/sendlayer.php
Calling env() outside a config file breaks after you run php artisan config:cache. Use config('sendlayer.api_key') in controllers, jobs, and services.

Registering the client in the container

Bind the client as a singleton in AppServiceProvider. Laravel then builds one instance per request lifecycle and injects it wherever you type-hint it.
app/Providers/AppServiceProvider.php
The check throws on a missing key at resolution time, which surfaces the problem before an API request goes out.

Sending an email from a controller

Type-hint the client in the constructor. Laravel resolves it from the container automatically.
app/Http/Controllers/ContactController.php
Laravel’s validate() method rejects malformed payloads with a 422 response before the SDK runs. The replyTo field points at the person who filled in the form, so support agents can reply from their inbox. Add the route:
routes/api.php
Use a from address on a domain you authorized in SendLayer. Requests from an unauthorized domain fail.

Rendering email bodies with Blade

Blade views give you layouts, components, and partials.
resources/views/emails/welcome.blade.php
Render a view to a string with the view() helper, then pass the result as the html parameter.
app/Jobs/SendWelcomeEmail.php
The tags array labels the message. Tags appear in your event history, which helps you separate welcome emails from other traffic.
Keep email views in their own directory. Email clients support a narrow subset of CSS, so those templates rarely share styles with your web pages.

Testing the endpoint

Start the development server:
Then post a request from another terminal:
A successful request returns the message ID:
Include the Accept: application/json header. Without it, Laravel returns validation errors as a redirect rather than JSON.
Open the Email logs page in your SendLayer dashboard to confirm delivery.

Next steps

PHP SDK Reference

Review every method and parameter in the PHP SDK.

Managing Webhooks

Create, list, and delete webhook subscriptions.

Retrieving Events

Query delivery events for any message.

Rate Limits

Understand recipient and request limits.