Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [3.2.0] - 2025-06-13
- Add Contact Lists API functionality
- Add Email Templates API functionality

## [3.1.0] - 2025-05-27
- Add Contacts API functionality

Expand Down
88 changes: 86 additions & 2 deletions examples/general/contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,90 @@
}


/**
* Get a specific Contact List by ID.
*
* GET https://mailtrap.io/api/accounts/{account_id}/contacts/lists/{list_id}
*/
try {
$contactListId = 1; // Replace 1 with the actual list ID
$response = $contacts->getContactList($contactListId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Create a new Contact List.
*
* POST https://mailtrap.io/api/accounts/{account_id}/contacts/lists
*/
try {
$contactListName = 'New Contact List'; // Replace with your desired list name
$response = $contacts->createContactList($contactListName);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Update a Contact List by ID.
*
* PATCH https://mailtrap.io/api/accounts/{account_id}/contacts/lists/{list_id}
*/
try {
$contactListId = 1; // Replace 1 with the actual list ID
$newContactListName = 'Updated Contact List Name'; // Replace with your desired list name
$response = $contacts->updateContactList($contactListId, $newContactListName);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Delete a Contact List by ID.
*
* DELETE https://mailtrap.io/api/accounts/{account_id}/contacts/lists/{list_id}
*/
try {
$contactListId = 1; // Replace 1 with the actual list ID
$response = $contacts->deleteContactList($contactListId);

// Print the response status code
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Get contact
*
* GET https://mailtrap.io/api/accounts/{account_id}/contacts/{id_or_email}
*/
try {
// Get contact by ID
$response = $contacts->getContactById('019706a8-0000-0000-0000-4f26816b467a');

// OR get contact by email
$response = $contacts->getContactByEmail('john.smith@example.com');

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Create a new Contact
*
Expand Down Expand Up @@ -97,8 +181,8 @@
// OR delete contact by email
$response = $contacts->deleteContactByEmail('john.smith@example.com');

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
// Print the response status code
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
108 changes: 108 additions & 0 deletions examples/general/emailTemplates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

use Mailtrap\Config;
use Mailtrap\DTO\Request\EmailTemplate;
use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapGeneralClient;

require __DIR__ . '/../vendor/autoload.php';

$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$config = new Config(getenv('MAILTRAP_API_KEY')); // Your API token from https://mailtrap.io/api-tokens
$emailTemplates = (new MailtrapGeneralClient($config))->emailTemplates($accountId);

/**
* Get all Email Templates.
*
* GET https://mailtrap.io/api/accounts/{account_id}/email_templates
*/
try {
$response = $emailTemplates->getAllEmailTemplates();

// Print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Get Email Template by ID.
*
* GET https://mailtrap.io/api/accounts/{account_id}/email_templates/{id}
*/
try {
$templateId = 12345; // Replace with a valid template ID
$response = $emailTemplates->getEmailTemplate($templateId);

// Print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Create a new Email Template.
*
* POST https://mailtrap.io/api/accounts/{account_id}/email_templates
*/
try {
$response = $emailTemplates->createEmailTemplate(
EmailTemplate::init(
'Welcome Email', // Name
'Welcome to our service!', // Subject
'Transactional', // Category
'Welcome to our service!', // Text Body
'<div>Welcome to our service!</div>' // HTML Body
)
);

// Print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Update an Email Template by ID.
*
* PATCH https://mailtrap.io/api/accounts/{account_id}/email_templates/{email_template_id}
*/
try {
$templateId = 12345; // Replace with a valid template ID
$response = $emailTemplates->updateEmailTemplate(
$templateId,
EmailTemplate::init(
'Updated Welcome Email', // Name
'Updated Subject', // Subject
'Transactional', // Category
'Updated Text Body', // Text Body
'<div>Updated HTML Body</div>', // HTML Body
)
);

// Print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}


/**
* Delete an Email Template by ID.
*
* DELETE https://mailtrap.io/api/accounts/{account_id}/email_templates/{email_template_id}
*/
try {
$templateId = 12345; // Replace with a valid template ID
$response = $emailTemplates->deleteEmailTemplate($templateId);

// Print the response status code
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
4 changes: 2 additions & 2 deletions examples/general/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

$response = $generalUsers->delete($accountAccessId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
// Print the response status code
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
4 changes: 2 additions & 2 deletions examples/testing/inboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@

$response = $sandboxInboxes->delete($inboxId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
// Print the response status code
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Expand Down
4 changes: 2 additions & 2 deletions examples/testing/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@

$response = $sandboxMessages->delete($messageId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
// Print the response status code
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
4 changes: 2 additions & 2 deletions examples/testing/projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@

$response = $sandboxProjects->delete($projectId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
// Print the response status code
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Expand Down
94 changes: 94 additions & 0 deletions src/Api/General/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,87 @@ public function getAllContactLists(): ResponseInterface
);
}

/**
* Get a specific Contact List by ID.
*
* @param int $listId
* @return ResponseInterface
*/
public function getContactList(int $listId): ResponseInterface
{
return $this->handleResponse(
$this->httpGet($this->getBasePath() . '/lists/' . $listId)
);
}

/**
* Create a new Contact List.
*
* @param string $name
* @return ResponseInterface
*/
public function createContactList(string $name): ResponseInterface
{
return $this->handleResponse(
$this->httpPost(
path: $this->getBasePath() . '/lists',
body: ['name' => $name]
)
);
}

/**
* Update an existing Contact List by ID.
*
* @param int $listId
* @param string $name
* @return ResponseInterface
*/
public function updateContactList(int $listId, string $name): ResponseInterface
{
return $this->handleResponse(
$this->httpPatch(
path: $this->getBasePath() . '/lists/' . $listId,
body: ['name' => $name]
)
);
}

/**
* Delete a Contact List by ID.
*
* @param int $listId
* @return ResponseInterface
*/
public function deleteContactList(int $listId): ResponseInterface
{
return $this->handleResponse(
$this->httpDelete($this->getBasePath() . '/lists/' . $listId)
);
}

/**
* Get a Contact by ID (UUID)
*
* @param string $contactId
* @return ResponseInterface
*/
public function getContactById(string $contactId): ResponseInterface
{
return $this->getContact($contactId);
}

/**
* Get a Contact by Email.
*
* @param string $email
* @return ResponseInterface
*/
public function getContactByEmail(string $email): ResponseInterface
{
return $this->getContact($email);
}

/**
* Create a new Contact.
*
Expand Down Expand Up @@ -96,6 +177,19 @@ public function getAccountId(): int
return $this->accountId;
}

/**
* Get a Contact by ID or Email.
*
* @param string $idOrEmail
* @return ResponseInterface
*/
private function getContact(string $idOrEmail): ResponseInterface
{
return $this->handleResponse(
$this->httpGet($this->getBasePath() . '/' . urlencode($idOrEmail))
);
}

/**
* Update an existing Contact.
*
Expand Down
Loading