> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sendsculpt.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP

> SendSculpt PHP official client.

<Note>Requires PHP 7.4+, `ext-curl`, and `ext-json`</Note>

## Installation

Install using Composer:

```bash theme={null}
composer require sendsculpt/sdk
```

## Vanilla PHP

Initialize the client, passing the **[API Key](https://sendsculpt.com/settings)**.

```php theme={null}
require_once 'path/to/SendSculptClient.php';

use SendSculpt\SendSculptClient;

$client = new SendSculptClient("your_secret_api_key_here");

try {
    $response = $client->sendEmail([
        'to' => ['recipient@example.com'],
        'subject' => 'Welcome to SendSculpt!',
        'from_email' => 'noreply@yourdomain.com',
        'body_html' => '<b>Hello!</b> This is a test from PHP.',
        'attachments' => [
            [
                'filename' => 'document.txt',
                'file_path' => '/path/to/local/document.txt', // Auto-converts to Base64
                'mime_type' => 'text/plain'
            ]
        ]
    ]);

    echo "Email sent successfully! Message ID: " . $response['message_id'];
} catch (Exception $e) {
    echo "Error sending email: " . $e->getMessage();
}
```

<Tip>
  The SendSculpt API automatically handles environments based on your API key. Use a Sandbox key for testing and a Live key for
  production.
</Tip>

## Framework Integrations

<AccordionGroup>
  <Accordion title="Laravel">
    Use the provided Service Provider by placing `SendSculptServiceProvider.php` in your `app/Providers` directory.

    In `config/app.php`, add the provider to the `providers` array:

    ```php theme={null}
    'providers' => [
        // ...
        App\Providers\SendSculptServiceProvider::class,
    ],
    ```

    In `config/services.php`, append the following:

    ```php theme={null}
    'sendsculpt' => [
        'key' => env('SENDSCULPT_API_KEY')
    ],
    ```

    Update your `.env` file:

    ```env theme={null}
    SENDSCULPT_API_KEY="your-api-key"
    ```

    **Usage in Controller**

    ```php theme={null}
    namespace App\Http\Controllers;

    use SendSculpt\SendSculptClient;

    class EmailController extends Controller
    {
        public function send(SendSculptClient $client)
        {
            $response = $client->sendEmail([
                'to' => ['john@doe.com'],
                'subject' => 'Laravel SendSculpt Test',
                'from_email' => 'test@yourdomain.com',
                'body_text' => 'It works perfectly in Laravel!'
            ]);

            return response()->json($response);
        }
    }
    ```
  </Accordion>

  <Accordion title="CodeIgniter 4">
    Inject it within your BaseController or explicitly initialize it.

    ```php theme={null}
    namespace App\Controllers;

    use SendSculpt\SendSculptClient;

    class MailController extends BaseController
    {
        public function index()
        {
            $apiKey = getenv('SENDSCULPT_API_KEY');

            $client = new SendSculptClient($apiKey);

            try {
                $response = $client->sendEmail([
                    'to' => ['recipient@example.com'],
                    'from_email' => 'noreply@yourdomain.com',
                    'subject' => 'CodeIgniter Integration',
                    'template_id' => 'your-template-id',
                    'template_data' => ['name' => 'John Doe']
                ]);

                return $this->response->setJSON(['status' => 'success', 'data' => $response]);
            } catch (\Exception $e) {
                return $this->response->setStatusCode(500)->setJSON(['error' => $e->getMessage()]);
            }
        }
    }
    ```
  </Accordion>

  <Accordion title="Symfony">
    Register `SendSculptClient` as a service in `config/services.yaml`:

    ```yaml theme={null}
    parameters:
        sendsculpt_api_key: '%env(SENDSCULPT_API_KEY)%'

    services:
        SendSculpt\SendSculptClient:
            arguments:
                $apiKey: '%sendsculpt_api_key%'
    ```

    **Usage in Controller**

    ```php theme={null}
    namespace App\Controller;

    use SendSculpt\SendSculptClient;
    use Symfony\Component\HttpFoundation\JsonResponse;
    use Symfony\Component\Routing\Annotation\Route;

    class MailerController
    {
        #[Route('/test-email', name: 'test_email')]
        public function index(SendSculptClient $client): JsonResponse
        {
            try {
                $response = $client->sendEmail([
                    'to' => ['user@example.com'],
                    'subject' => 'Symfony Integration',
                    'from_email' => 'hello@yourdomain.com',
                    'body_html' => '<h1>Hello from Symfony</h1>'
                ]);

                return new JsonResponse($response);
            } catch (\Exception $e) {
                return new JsonResponse(['error' => $e->getMessage()], 500);
            }
        }
    }
    ```
  </Accordion>
</AccordionGroup>
