PHP SDK
Send email, manage domains, contacts, suppressions, templates, and webhooks from PHP with the official Taifa Mail SDK.
The official PHP SDK for Taifa Mail. It wraps the Mailer REST API with a small, typed client: bearer authentication, JSON encode/decode, the from to from_ wire mapping, multipart uploads, and automatic retries on 429 and 5xx responses are all handled for you.
- Package (Packagist):
taifamail/sdk - Source: github.com/GovConnectKenya/taifa-mail-php
Methods return plain associative arrays decoded straight from the API response, so you work with the same field names documented in the API reference.
Installation
Install with Composer:
Requirements:
- PHP 8.1 or later
- Guzzle (pulled in automatically as a dependency)
Client setup
Create a client with your API key. Keys start with tfm_k_ and are passed as a bearer token on every request. Read it from the environment rather than hardcoding it.
The constructor takes an optional second array $options argument:
| Option | Type | Default | Description |
|---|---|---|---|
baseUrl | string | https://govconnect.ke | API base URL. Override for staging. |
maxRetries | int | 3 | Attempts for retryable (429/5xx) responses. |
timeout | float|int | 30 | Per-request timeout in seconds. |
http | GuzzleHttp\ClientInterface | new Guzzle client | Custom HTTP client, mainly for testing. |
The client exposes one property per resource group: $taifamail->emails, $taifamail->domains, $taifamail->contacts, $taifamail->suppressions, $taifamail->templates, and $taifamail->webhooks.
Send an email
Pass an associative array to emails->send(). The address fields (from, to, cc, bcc, replyTo) each accept either a bare string or an ['email' => ..., 'name' => ...] array. A bare string is shorthand for ['email' => $string]. The to, cc, and bcc fields also accept a list of addresses.
A fuller example using named addresses, multiple recipients, and scheduling:
The SDK maps the clean from key to the API's from_ field for you, and turns string addresses into address objects. You never write from_ directly. Scheduling with sendAt requires the Starter plan or above.
The response is an associative array with id, status, message_id, and rejection_reason.
Emails
Send, look up, search, schedule, and inspect messages via $taifamail->emails.
Domains
Register, verify, inspect, and transfer sending domains via $taifamail->domains.
Contacts
Manage subscriber lists, their contacts, CSV imports, and templated bulk sends via $taifamail->contacts.
The list id is injected as contact_list_id automatically, so you do not set it in the bulkSend array. The senderAddressId maps to the wire field sender_address_id.
Suppressions
Manage the do-not-send list via $taifamail->suppressions.
Templates
Manage reusable email templates via $taifamail->templates. Available on the Starter plan and above.
Webhooks
Manage event subscriptions and inspect deliveries via $taifamail->webhooks.
Error handling
Every non-2xx response, and any transport failure that survives all retries, throws TaifaMail\Sdk\TaifaMailException. Inspect getStatus() for the HTTP status code and getCode() for the API's string error code.
getStatus() returns 0 for a network or transport failure where no HTTP response was received. Retries on 429 and 5xx (honouring Retry-After) happen automatically before an exception is thrown, so by the time you catch one, those have already been exhausted. 4xx responses other than 429 are never retried.
Configuration
All knobs are passed through the second constructor argument.
Retry behaviour: the transport retries 429 and 5xx responses with exponential backoff, honouring the Retry-After header when the server sends one. Multipart uploads (CSV and suppression imports) are never retried because they are not idempotent.
You can also inject a preconfigured Guzzle client via the http option, which is useful for testing or for sharing connection pools and middleware.
Next steps
- SDKs overview - the full list of official SDKs and shared conventions.
- Emails API reference - field-level detail for every email endpoint.