-
-
Notifications
You must be signed in to change notification settings - Fork 262
docs: add PHP getting started guide #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,105 @@ | ||||||
| --- | ||||||
| title: PHP (API) | ||||||
| description: "Use the REST API to send emails and manage contacts from PHP." | ||||||
| icon: php | ||||||
| --- | ||||||
|
|
||||||
| useSend doesn't ship an official PHP SDK yet, but you can call the REST API with any HTTP client. The examples below use [Guzzle](https://github.com/guzzle/guzzle). | ||||||
|
|
||||||
| ## Prerequisites | ||||||
|
|
||||||
| - [useSend API key](https://app.usesend.com/dev-settings/api-keys) | ||||||
| - [Verified domain](https://app.usesend.com/domains) | ||||||
| - PHP 8.1+ with Composer | ||||||
|
|
||||||
| ## Install an HTTP client | ||||||
|
|
||||||
| ```bash | ||||||
| composer require guzzlehttp/guzzle | ||||||
| ``` | ||||||
|
|
||||||
| ## Configure a reusable client | ||||||
|
|
||||||
| ```php | ||||||
| <?php | ||||||
| require __DIR__ . '/vendor/autoload.php'; | ||||||
|
|
||||||
| use GuzzleHttp\\Client; | ||||||
|
|
||||||
| $apiKey = getenv('USESEND_API_KEY'); | ||||||
|
|
||||||
| $client = new Client([ | ||||||
| 'base_uri' => 'https://app.usesend.com/api/', | ||||||
| 'headers' => [ | ||||||
| 'Authorization' => "Bearer {$apiKey}", | ||||||
| 'Content-Type' => 'application/json', | ||||||
| ], | ||||||
| ]); | ||||||
|
|
||||||
| // Self-hosted example: | ||||||
| // $client = new Client(['base_uri' => 'https://your-self-hosted.example/api/', ...]); | ||||||
| ``` | ||||||
|
|
||||||
| ## Send an email | ||||||
|
|
||||||
| ```php | ||||||
| <?php | ||||||
| $response = $client->post('v1/emails', [ | ||||||
| 'json' => [ | ||||||
| 'to' => '[email protected]', | ||||||
| 'from' => '[email protected]', | ||||||
| 'subject' => 'Welcome to useSend', | ||||||
| 'html' => '<p>useSend is the best open source product to send emails</p>', | ||||||
| 'text' => 'useSend is the best open source product to send emails', | ||||||
| 'headers' => [ | ||||||
| 'X-Campaign' => 'welcome', | ||||||
| ], | ||||||
| ], | ||||||
| ]); | ||||||
|
|
||||||
| $body = json_decode($response->getBody()->getContents(), true); | ||||||
| ``` | ||||||
|
|
||||||
| > Custom headers are forwarded as-is. useSend only manages the `X-Usesend-Email-ID` and `References` headers. | ||||||
|
|
||||||
| ## Create or update contacts | ||||||
|
|
||||||
| All contact operations require a contact book ID from the [Contacts dashboard](https://app.usesend.com/contacts/). | ||||||
|
|
||||||
| ```php | ||||||
| <?php | ||||||
| $bookId = 'contact_book_id'; | ||||||
|
|
||||||
| // Create a contact | ||||||
| $createResponse = $client->post("v1/contactBooks/{$bookId}/contacts", [ | ||||||
| 'json' => [ | ||||||
| 'email' => '[email protected]', | ||||||
| 'firstName' => 'Ada', | ||||||
| 'properties' => ['plan' => 'pro'], | ||||||
| ], | ||||||
| ]); | ||||||
|
|
||||||
| // Update a contact | ||||||
| $contactId = 'contact_123'; | ||||||
| $updateResponse = $client->patch("v1/contactBooks/{$bookId}/contacts/{$contactId}", [ | ||||||
| 'json' => [ | ||||||
| 'subscribed' => false, | ||||||
| ], | ||||||
| ]); | ||||||
| ``` | ||||||
|
|
||||||
| ## Basic error handling | ||||||
|
|
||||||
| ```php | ||||||
| <?php | ||||||
| try { | ||||||
| $response = $client->get('v1/emails/email_123'); | ||||||
| $email = json_decode($response->getBody()->getContents(), true); | ||||||
| } catch (\\GuzzleHttp\\Exception\\ClientException $e) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ClientException catch statement escapes the namespace separator twice, making the provided PHP snippet uncompilable. Use single backslashes in the fully qualified class name. Prompt for AI agents
Suggested change
|
||||||
| // 4xx responses | ||||||
| error_log($e->getResponse()->getBody()->getContents()); | ||||||
| } catch (\\GuzzleHttp\\Exception\\ServerException $e) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ServerException catch block double-escapes the namespace separator, so the example cannot be pasted into PHP as-is. Replace it with a single backslash-qualified class name. Prompt for AI agents
Suggested change
|
||||||
| // 5xx responses | ||||||
| error_log('Server error: ' . $e->getMessage()); | ||||||
| } | ||||||
| ``` | ||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
usestatement shows a double backslash, so copying the snippet yields invalid PHP syntax. Use a single namespace separator when importingGuzzleHttp\Client.Prompt for AI agents