PHP
PHP SDK for the Poodle’s email sending API.
Table of Contents
Section titled “Table of Contents”- Installation
- Quick Start
- Features
- Configuration
- Usage Examples
- API Reference
- Framework Integration
- Development
- Error Codes
- Contributing
- License
Installation
Section titled “Installation”Install the SDK using Composer:
composer require usepoodle/poodle-php
Quick Start
Section titled “Quick Start”<?php
require_once 'vendor/autoload.php';
use Poodle\PoodleClient;
// Initialize the client$client = new PoodleClient('your_api_key_here');
// Send an email$response = $client->send( subject: 'Hello from Poodle!', html: '<h1>Hello World!</h1><p>This is a test email.</p>', text: 'Hello World! This is a test email.');
echo "Email sent! Message: " . $response->getMessage();
Features
Section titled “Features”- Simple and intuitive API
- HTML and plain text email support
- Comprehensive error handling
- Built-in input validation
- PSR-12 compliant code
- 100% type coverage with PHPDoc
- Extensive test suite
- PHP 8.0+ support
Configuration
Section titled “Configuration”API Key
Section titled “API Key”Set your API key in one of these ways:
1. Pass directly to constructor:
$client = new PoodleClient('your_api_key_here');
2. Use environment variable:
export POODLE_API_KEY=your_api_key_here
$client = new PoodleClient(); // Will use POODLE_API_KEY
3. Use Configuration object:
use Poodle\Configuration;
$config = new Configuration( apiKey: 'your_api_key_here', baseUrl: 'https://api.usepoodle.com', timeout: 30.0, debug: true);
$client = new PoodleClient($config);
Environment Variables
Section titled “Environment Variables”Variable | Default | Description |
---|---|---|
POODLE_API_KEY | - | Your Poodle API key |
POODLE_BASE_URL | https://api.usepoodle.com | API base URL |
POODLE_TIMEOUT | 30.0 | Request timeout in seconds |
POODLE_CONNECT_TIMEOUT | 10.0 | Connection timeout in seconds |
POODLE_DEBUG | false | Enable debug logging |
Usage Examples
Section titled “Usage Examples”Basic Email Sending
Section titled “Basic Email Sending”use Poodle\PoodleClient;
$client = new PoodleClient('your_api_key');
// HTML email$response = $client->sendHtml( subject: 'Welcome!', html: '<h1>Welcome to our service!</h1>');
// Plain text email$response = $client->sendText( subject: 'Welcome!', text: 'Welcome to our service!');
Using the Email Model
Section titled “Using the Email Model”use Poodle\Model\Email;use Poodle\PoodleClient;
$client = new PoodleClient('your_api_key');
// Create email object$email = new Email( subject: 'Welcome Email', html: '<h1>Hello!</h1><p>Welcome to our service!</p>', text: 'Hello! Welcome to our service!');
// Send the email$response = $client->sendEmail($email);
if ($response->isSuccessful()) { echo "Email queued successfully!";}
Error Handling
Section titled “Error Handling”use Poodle\PoodleClient;use Poodle\Exception\ValidationException;use Poodle\Exception\AuthenticationException;use Poodle\Exception\RateLimitException;use Poodle\Exception\NetworkException;use Poodle\Exception\PoodleException;
$client = new PoodleClient('your_api_key');
try { $response = $client->send( subject: 'Test Email', html: '<h1>Hello!</h1>' );
echo "Email sent successfully!";
} catch (ValidationException $e) { echo "Validation error: " . $e->getMessage() . "\n"; foreach ($e->getErrors() as $field => $errors) { echo " {$field}: " . implode(', ', $errors) . "\n"; }} catch (AuthenticationException $e) { echo "Authentication failed: " . $e->getMessage() . "\n";} catch (RateLimitException $e) { echo "Rate limit exceeded. Retry after: " . $e->getRetryAfter() . " seconds\n";} catch (NetworkException $e) { echo "Network error: " . $e->getMessage() . "\n";} catch (PoodleException $e) { echo "Poodle error: " . $e->getMessage() . "\n"; echo "Context: " . json_encode($e->getContext()) . "\n";}
For more usage patterns, see the examples directory.
API Reference
Section titled “API Reference”PoodleClient
Section titled “PoodleClient”The main client class for sending emails.
Constructor
Section titled “Constructor”new PoodleClient(string|Configuration $apiKeyOrConfig, ?string $baseUrl = null)
Methods
Section titled “Methods”send(string $from, string $to, string $subject, ?string $html = null, ?string $text = null): EmailResponse
sendHtml(string $from, string $to, string $subject, string $html): EmailResponse
sendText(string $from, string $to, string $subject, string $text): EmailResponse
sendEmail(Email|array $email): EmailResponse
Email Model
Section titled “Email Model”Represents an email to be sent.
Constructor
Section titled “Constructor”new Email(string $from, string $to, string $subject, ?string $html = null, ?string $text = null)
Methods
Section titled “Methods”getFrom(): string
- Get sender email addressgetTo(): string
- Get recipient email addressgetSubject(): string
- Get email subjectgetHtml(): ?string
- Get HTML contentgetText(): ?string
- Get plain text contenttoArray(): array
- Convert to array for API request
EmailResponse
Section titled “EmailResponse”Represents the API response after sending an email.
Methods
Section titled “Methods”isSuccessful(): bool
- Check if email was successfully queuedgetMessage(): string
- Get response messagetoArray(): array
- Convert to arraytoJson(): string
- Convert to JSON
Configuration
Section titled “Configuration”SDK configuration object.
Constructor
Section titled “Constructor”new Configuration( ?string $apiKey = null, ?string $baseUrl = null, ?float $timeout = null, ?float $connectTimeout = null, bool $debug = false, array $httpClientOptions = [])
Framework Integration
Section titled “Framework Integration”Laravel
Section titled “Laravel”Add to your .env
:
POODLE_API_KEY=your_api_key_here
Create a service:
use Poodle\PoodleClient;
class EmailService{ private PoodleClient $client;
public function __construct() { $this->client = new PoodleClient(config('services.poodle.api_key')); }
public function sendWelcomeEmail(string $email, string $name): void { $this->client->sendHtml( to: $email, subject: "Welcome, {$name}!", html: view('emails.welcome', compact('name'))->render() ); }}
Symfony
Section titled “Symfony”services: Poodle\PoodleClient: arguments: $apiKeyOrConfig: "%env(POODLE_API_KEY)%"
Development
Section titled “Development”Running Tests
Section titled “Running Tests”# Install dependenciescomposer install
# Run testscomposer test
# Run tests with coveragecomposer test-coverage
# Code style checkcomposer cs-check
# Fix code stylecomposer cs-fix
# Static analysiscomposer phpstan
Requirements
Section titled “Requirements”- PHP 7.4 or higher
- ext-json
- GuzzleHTTP 7.0+
Error Codes
Section titled “Error Codes”HTTP Code | Exception | Description |
---|---|---|
400 | ValidationException | Invalid request data |
401 | AuthenticationException | Invalid or missing API key |
403 | AuthenticationException | Insufficient permissions |
408 | NetworkException | Request timeout |
429 | RateLimitException | Rate limit exceeded |
5xx | NetworkException | Server error |
Contributing
Section titled “Contributing”Contributions are welcome! Please read our Contributing Guide for details on the process for submitting pull requests and our Code of Conduct.
License
Section titled “License”This project is licensed under the MIT License - see the LICENSE file for details.