adachsoft / purelymail-client
A typed PHP client for the Purelymail account-management API.
Requires
- php: ^8.3
- adachsoft/collection: ^3.0
- psr/http-client: ^1
- psr/http-factory: ^1
Requires (Dev)
- adachsoft/php-code-style: ^0.7.4
- friendsofphp/php-cs-fixer: ^3.95
- guzzlehttp/guzzle: ^7
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/purelymail-client is a typed PHP client for the Purelymail account-management API. It provides a PSR-18/PSR-17 based HTTP transport, typed request and response objects, and a domain onboarding service that returns the DNS records needed for a mailbox on a custom domain.
The client covers domain, user, routing, app-password, password-reset, and account-credit operations. It does not provide IMAP or SMTP access to message contents.
Requirements
- PHP 8.3 or newer
- A PSR-18 HTTP client
- PSR-17 request and stream factories
Installation
Install the library with Composer:
composer require adachsoft/purelymail-client
For example, Guzzle 7 provides a PSR-18 client and PSR-17 factories:
composer require guzzlehttp/guzzle:^7
API token and client setup
Create or refresh the API token in your Purelymail account settings. Provide it to the application through an environment variable or a secrets manager. Never hard-code the token or commit it to source control.
<?php
declare(strict_types=1);
use AdachSoft\PurelymailClient\PurelymailClient;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
$apiToken = getenv('PURELYMAIL_API_TOKEN');
if ($apiToken === false || $apiToken === '') {
throw new RuntimeException('Set PURELYMAIL_API_TOKEN before creating the client.');
}
$httpFactory = new HttpFactory();
$purelymail = PurelymailClient::create(
$apiToken,
new Client(),
$httpFactory,
$httpFactory,
);
PurelymailClient::create() uses https://purelymail.com/api/v0/ by default. A different API base URL can be supplied as the final argument, for example when testing against a compatible local server.
Usage
All calls are made through the PurelymailClient facade. Request DTOs keep the API field names and response collections contain typed values.
Domains
The domain API supports addDomain(), listDomains(), getOwnershipCode(), updateDomainSettings(), and deleteDomain(). The ownership proof response is available as $ownershipCode->code.
$ownershipCode = $purelymail->getOwnershipCode();
$domains = $purelymail->listDomains();
// Add a domain after its required DNS records have been published.
$purelymail->addDomain('example.com');
listDomains() excludes shared Purelymail domains by default; pass true to include them.
Users
The user API supports createUser(), listUser(), getUser(), modifyUser(), and deleteUser().
use AdachSoft\PurelymailClient\User\CreateUserRequest;
$mailboxPassword = getenv('PROJECT_MAILBOX_PASSWORD');
if ($mailboxPassword === false || $mailboxPassword === '') {
throw new RuntimeException('Set PROJECT_MAILBOX_PASSWORD in your secrets manager.');
}
$purelymail->createUser(new CreateUserRequest('contact', 'example.com', $mailboxPassword));
$mailboxNames = $purelymail->listUser();
$mailbox = $purelymail->getUser('contact@example.com');
The password should be supplied through a secret store or environment variable just like the API token.
Routing rules
The routing API supports createRoutingRule(), listRoutingRules(), and deleteRoutingRule().
use AdachSoft\PurelymailClient\Collection\StringCollection;
use AdachSoft\PurelymailClient\Routing\CreateRoutingRuleRequest;
$purelymail->createRoutingRule(new CreateRoutingRuleRequest(
'example.com',
false,
'sales',
new StringCollection(['sales@example.net']),
false,
));
$rules = $purelymail->listRoutingRules();
App passwords
The app-password API supports createAppPassword() and deleteAppPassword().
use AdachSoft\PurelymailClient\AppPassword\AppPasswordRequest;
$appPassword = $purelymail->createAppPassword(new AppPasswordRequest(
'contact@example.com',
'Project mail client',
));
The generated secret is available as $appPassword->appPassword. Store it securely; it is not a substitute for the account API token.
Password-reset methods
The password-reset API supports upsertPasswordReset(), listPasswordReset(), and deletePasswordReset().
use AdachSoft\PurelymailClient\PasswordReset\UpsertPasswordResetRequest;
$purelymail->upsertPasswordReset(new UpsertPasswordResetRequest(
'contact@example.com',
'email',
'recovery@example.net',
));
$resetMethods = $purelymail->listPasswordReset('contact@example.com');
Account credit
The account API supports checkAccountCredit(). The credit value is returned as a precision-preserving string.
$credit = $purelymail->checkAccountCredit()->credit;
Domain mailbox onboarding
DomainMailboxOnboarding coordinates domain registration, mailbox creation, and an optional catch-all rule. It does not modify DNS at your registrar; publish the returned records through your DNS provider.
- Get the relative DNS records and publish them for the domain.
- Wait for DNS propagation and Purelymail's DNS checks.
- Call
onboard(). It adds the domain if it is not listed, creates the mailbox only if missing, and creates the requested catch-all route only if an equivalent route is missing.
use AdachSoft\PurelymailClient\Onboarding\DomainMailboxOnboarding;
use AdachSoft\PurelymailClient\Onboarding\DomainOnboardingStateEnum;
use AdachSoft\PurelymailClient\Onboarding\OnboardDomainRequest;
$onboarding = new DomainMailboxOnboarding($purelymail);
$requiredRecords = $onboarding->requiredDnsRecords('example.com');
foreach ($requiredRecords as $record) {
printf(
"%s %s %s%s\n",
$record->type->value,
$record->host,
$record->value,
$record->priority === null ? '' : ' (priority ' . $record->priority . ')',
);
}
// Publish the records above, then use a password from your secret store.
$result = $onboarding->onboard(new OnboardDomainRequest(
domainName: 'example.com',
localPart: 'contact',
password: $mailboxPassword,
createCatchAllRule: true,
));
if ($result->state === DomainOnboardingStateEnum::DNS_PENDING) {
// Check the DNS records and retry after Purelymail reports them as passing.
}
printf("Mailbox: %s\n", $result->mailboxAddress);
requiredDnsRecords() includes the ownership TXT value returned by Purelymail and the default MX, SPF, DKIM, and DMARC records. The default setup values are centralized in PurelymailDnsRecordTemplate and should be verified against Purelymail's setup page. The template accepts custom fixed records and an ownership-record host.
domainStatus() returns whether the domain is listed and the passesMx, passesSpf, passesDkim, and passesDmarc flags reported by Purelymail. An unlisted domain has isAdded === false and no DNS summary. OnboardDomainResult reports READY or DNS_PENDING, creation flags, the required DNS records, and the mailbox address.
The repository's OpenAPI specification copy is docs/purelymail-openapi.json. It describes the API operations and response fields used by this client. That copy does not specify a DNS-specific addDomain error code or message, so such API errors are propagated as PurelymailApiException rather than guessed to mean DNS is pending.
Error handling
All client exceptions implement PurelymailExceptionInterface:
PurelymailExceptionInterface (extends Throwable)
├── PurelymailApiException (extends RuntimeException)
│ └── PurelymailAuthenticationException
└── PurelymailTransportException (extends RuntimeException)
PurelymailAuthenticationExceptionrepresents an authentication or authorization failure.PurelymailApiExceptionrepresents another API error. UsegetApiCode()andgetHttpStatus()for structured details.PurelymailTransportExceptionrepresents HTTP transport, request encoding, or response decoding failures.
Catch the authentication subtype before its API exception parent:
use AdachSoft\PurelymailClient\Exception\PurelymailApiException;
use AdachSoft\PurelymailClient\Exception\PurelymailAuthenticationException;
use AdachSoft\PurelymailClient\Exception\PurelymailTransportException;
try {
$domains = $purelymail->listDomains();
} catch (PurelymailAuthenticationException $exception) {
// Check the account token and access permissions.
} catch (PurelymailApiException $exception) {
// Handle a Purelymail API rejection.
} catch (PurelymailTransportException $exception) {
// Handle a network or response-processing failure.
}