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

# PHP

> Send emails from PHP using the SendKit SDK.

<CardGroup cols={2}>
  <Card title="Source Code" icon="github" href="https://github.com/sendkitdev/sendkit-php">
    View on GitHub
  </Card>

  <Card title="Package" icon="box" href="https://packagist.org/packages/sendkit/sendkit-php">
    View on Packagist
  </Card>
</CardGroup>

## Install

```bash theme={null}
composer require sendkit/sendkit-php
```

## Send email

```php theme={null}
use SendKit\SendKit;

$client = SendKit::client('sk_your_api_key');

$response = $client->emails()->send([
    'from' => 'Your Name <you@yourdomain.com>',
    'to' => 'recipient@example.com',
    'subject' => 'Hello from SendKit',
    'html' => '<h1>Welcome!</h1><p>Your first email with SendKit.</p>',
]);

echo $response['id'];
```

## Validate email

Validate an email address before sending. Each validation costs credits.

```php theme={null}
use SendKit\SendKit;

$client = SendKit::client('sk_your_api_key');

$result = $client->validateEmail('recipient@example.com');

if ($result['should_block']) {
    // Email should not be used
    echo $result['block_reason'];
}

echo $result['is_valid'];           // "HIGH" or "LOW"
echo $result['evaluations'];        // detailed checks
```

The `evaluations` array contains:

| Key                | Description                                      |
| ------------------ | ------------------------------------------------ |
| `has_valid_syntax` | Whether the email has valid syntax               |
| `has_valid_dns`    | Whether the domain has valid DNS records         |
| `mailbox_exists`   | Whether the mailbox exists                       |
| `is_role_address`  | Whether it's a role address (e.g. info@, admin@) |
| `is_disposable`    | Whether it's a disposable email                  |
| `is_random_input`  | Whether it appears to be random input            |

## Contacts

### Create or update a contact

Create a new contact or update an existing one if the email already exists (upsert).

```php theme={null}
$contact = $client->contacts()->create([
    'email' => 'john@example.com',
    'first_name' => 'John',
    'last_name' => 'Doe',
    'list_ids' => ['list-uuid-1', 'list-uuid-2'],
    'properties' => ['COMPANY' => 'Acme'],
]);

echo $contact['id'];
```

### List contacts

Retrieve a paginated list of contacts.

```php theme={null}
$contacts = $client->contacts()->list();

// With pagination
$contacts = $client->contacts()->list(['page' => 2]);

echo $contacts['meta']['total'];    // total contacts
```

### Get a contact

```php theme={null}
$contact = $client->contacts()->get('contact-uuid');

echo $contact['email'];
echo $contact['properties']['COMPANY'];
```

### Update a contact

```php theme={null}
$contact = $client->contacts()->update('contact-uuid', [
    'first_name' => 'Johnny',
    'unsubscribed' => true,
]);
```

### Delete a contact

```php theme={null}
$client->contacts()->delete('contact-uuid');
```

### Add a contact to lists

```php theme={null}
$contact = $client->contacts()->addToLists('contact-uuid', [
    'list-uuid-1',
    'list-uuid-2',
]);
```

### List a contact's lists

```php theme={null}
$lists = $client->contacts()->listLists('contact-uuid');

// With pagination
$lists = $client->contacts()->listLists('contact-uuid', ['page' => 2]);
```

### Remove a contact from a list

```php theme={null}
$client->contacts()->removeFromList('contact-uuid', 'list-uuid');
```

## Contact properties

Contact properties let you define custom fields for your contacts.

### Create a property

```php theme={null}
$property = $client->contactProperties()->create([
    'key' => 'company',
    'type' => 'string',           // "string", "number", or "date"
    'fallback_value' => 'N/A',    // optional
]);

echo $property['id'];
```

### List properties

```php theme={null}
$properties = $client->contactProperties()->list();

// With pagination
$properties = $client->contactProperties()->list(['page' => 2]);
```

### Update a property

```php theme={null}
$property = $client->contactProperties()->update('property-uuid', [
    'key' => 'organization',
    'fallback_value' => 'Unknown',
]);
```

### Delete a property

```php theme={null}
$client->contactProperties()->delete('property-uuid');
```

<Note>
  A `SendKitException` with status `409` is thrown if the property is used in segment filters.
</Note>
