Skip to content

Commit 7e04265

Browse files
authored
Merge pull request #39 from railsware/feature/new-api-functionality
[3.2.0] Add Email Templates API functionality + Contact Lists API functionality
2 parents b48e1d1 + 4850fe1 commit 7e04265

File tree

15 files changed

+831
-16
lines changed

15 files changed

+831
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [3.2.0] - 2025-06-13
2+
- Add Contact Lists API functionality
3+
- Add Email Templates API functionality
4+
15
## [3.1.0] - 2025-05-27
26
- Add Contacts API functionality
37

examples/general/contacts.php

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,90 @@
2727
}
2828

2929

30+
/**
31+
* Get a specific Contact List by ID.
32+
*
33+
* GET https://mailtrap.io/api/accounts/{account_id}/contacts/lists/{list_id}
34+
*/
35+
try {
36+
$contactListId = 1; // Replace 1 with the actual list ID
37+
$response = $contacts->getContactList($contactListId);
38+
39+
// print the response body (array)
40+
var_dump(ResponseHelper::toArray($response));
41+
} catch (Exception $e) {
42+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
43+
}
44+
45+
46+
/**
47+
* Create a new Contact List.
48+
*
49+
* POST https://mailtrap.io/api/accounts/{account_id}/contacts/lists
50+
*/
51+
try {
52+
$contactListName = 'New Contact List'; // Replace with your desired list name
53+
$response = $contacts->createContactList($contactListName);
54+
55+
// print the response body (array)
56+
var_dump(ResponseHelper::toArray($response));
57+
} catch (Exception $e) {
58+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
59+
}
60+
61+
62+
/**
63+
* Update a Contact List by ID.
64+
*
65+
* PATCH https://mailtrap.io/api/accounts/{account_id}/contacts/lists/{list_id}
66+
*/
67+
try {
68+
$contactListId = 1; // Replace 1 with the actual list ID
69+
$newContactListName = 'Updated Contact List Name'; // Replace with your desired list name
70+
$response = $contacts->updateContactList($contactListId, $newContactListName);
71+
72+
// print the response body (array)
73+
var_dump(ResponseHelper::toArray($response));
74+
} catch (Exception $e) {
75+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
76+
}
77+
78+
79+
/**
80+
* Delete a Contact List by ID.
81+
*
82+
* DELETE https://mailtrap.io/api/accounts/{account_id}/contacts/lists/{list_id}
83+
*/
84+
try {
85+
$contactListId = 1; // Replace 1 with the actual list ID
86+
$response = $contacts->deleteContactList($contactListId);
87+
88+
// Print the response status code
89+
var_dump($response->getStatusCode());
90+
} catch (Exception $e) {
91+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
92+
}
93+
94+
95+
/**
96+
* Get contact
97+
*
98+
* GET https://mailtrap.io/api/accounts/{account_id}/contacts/{id_or_email}
99+
*/
100+
try {
101+
// Get contact by ID
102+
$response = $contacts->getContactById('019706a8-0000-0000-0000-4f26816b467a');
103+
104+
// OR get contact by email
105+
$response = $contacts->getContactByEmail('john.smith@example.com');
106+
107+
// print the response body (array)
108+
var_dump(ResponseHelper::toArray($response));
109+
} catch (Exception $e) {
110+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
111+
}
112+
113+
30114
/**
31115
* Create a new Contact
32116
*
@@ -97,8 +181,8 @@
97181
// OR delete contact by email
98182
$response = $contacts->deleteContactByEmail('john.smith@example.com');
99183

100-
// print the response body (array)
101-
var_dump(ResponseHelper::toArray($response));
184+
// Print the response status code
185+
var_dump($response->getStatusCode());
102186
} catch (Exception $e) {
103187
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
104188
}

examples/general/emailTemplates.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Mailtrap\Config;
6+
use Mailtrap\DTO\Request\EmailTemplate;
7+
use Mailtrap\Helper\ResponseHelper;
8+
use Mailtrap\MailtrapGeneralClient;
9+
10+
require __DIR__ . '/../vendor/autoload.php';
11+
12+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
13+
$config = new Config(getenv('MAILTRAP_API_KEY')); // Your API token from https://mailtrap.io/api-tokens
14+
$emailTemplates = (new MailtrapGeneralClient($config))->emailTemplates($accountId);
15+
16+
/**
17+
* Get all Email Templates.
18+
*
19+
* GET https://mailtrap.io/api/accounts/{account_id}/email_templates
20+
*/
21+
try {
22+
$response = $emailTemplates->getAllEmailTemplates();
23+
24+
// Print the response body (array)
25+
var_dump(ResponseHelper::toArray($response));
26+
} catch (Exception $e) {
27+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
28+
}
29+
30+
31+
/**
32+
* Get Email Template by ID.
33+
*
34+
* GET https://mailtrap.io/api/accounts/{account_id}/email_templates/{id}
35+
*/
36+
try {
37+
$templateId = 12345; // Replace with a valid template ID
38+
$response = $emailTemplates->getEmailTemplate($templateId);
39+
40+
// Print the response body (array)
41+
var_dump(ResponseHelper::toArray($response));
42+
} catch (Exception $e) {
43+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
44+
}
45+
46+
47+
/**
48+
* Create a new Email Template.
49+
*
50+
* POST https://mailtrap.io/api/accounts/{account_id}/email_templates
51+
*/
52+
try {
53+
$response = $emailTemplates->createEmailTemplate(
54+
EmailTemplate::init(
55+
'Welcome Email', // Name
56+
'Welcome to our service!', // Subject
57+
'Transactional', // Category
58+
'Welcome to our service!', // Text Body
59+
'<div>Welcome to our service!</div>' // HTML Body
60+
)
61+
);
62+
63+
// Print the response body (array)
64+
var_dump(ResponseHelper::toArray($response));
65+
} catch (Exception $e) {
66+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
67+
}
68+
69+
70+
/**
71+
* Update an Email Template by ID.
72+
*
73+
* PATCH https://mailtrap.io/api/accounts/{account_id}/email_templates/{email_template_id}
74+
*/
75+
try {
76+
$templateId = 12345; // Replace with a valid template ID
77+
$response = $emailTemplates->updateEmailTemplate(
78+
$templateId,
79+
EmailTemplate::init(
80+
'Updated Welcome Email', // Name
81+
'Updated Subject', // Subject
82+
'Transactional', // Category
83+
'Updated Text Body', // Text Body
84+
'<div>Updated HTML Body</div>', // HTML Body
85+
)
86+
);
87+
88+
// Print the response body (array)
89+
var_dump(ResponseHelper::toArray($response));
90+
} catch (Exception $e) {
91+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
92+
}
93+
94+
95+
/**
96+
* Delete an Email Template by ID.
97+
*
98+
* DELETE https://mailtrap.io/api/accounts/{account_id}/email_templates/{email_template_id}
99+
*/
100+
try {
101+
$templateId = 12345; // Replace with a valid template ID
102+
$response = $emailTemplates->deleteEmailTemplate($templateId);
103+
104+
// Print the response status code
105+
var_dump($response->getStatusCode());
106+
} catch (Exception $e) {
107+
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
108+
}

examples/general/users.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939

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

42-
// print the response body (array)
43-
var_dump(ResponseHelper::toArray($response));
42+
// Print the response status code
43+
var_dump($response->getStatusCode());
4444
} catch (Exception $e) {
4545
echo 'Caught exception: ', $e->getMessage(), "\n";
4646
}

examples/testing/inboxes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272

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

75-
// print the response body (array)
76-
var_dump(ResponseHelper::toArray($response));
75+
// Print the response status code
76+
var_dump($response->getStatusCode());
7777
} catch (Exception $e) {
7878
echo 'Caught exception: ', $e->getMessage(), "\n";
7979
}

examples/testing/messages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@
197197

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

200-
// print the response body (array)
201-
var_dump(ResponseHelper::toArray($response));
200+
// Print the response status code
201+
var_dump($response->getStatusCode());
202202
} catch (Exception $e) {
203203
echo 'Caught exception: ', $e->getMessage(), "\n";
204204
}

examples/testing/projects.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989

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

92-
// print the response body (array)
93-
var_dump(ResponseHelper::toArray($response));
92+
// Print the response status code
93+
var_dump($response->getStatusCode());
9494
} catch (Exception $e) {
9595
echo 'Caught exception: ', $e->getMessage(), "\n";
9696
}

src/Api/General/Contact.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,87 @@ public function getAllContactLists(): ResponseInterface
3232
);
3333
}
3434

35+
/**
36+
* Get a specific Contact List by ID.
37+
*
38+
* @param int $listId
39+
* @return ResponseInterface
40+
*/
41+
public function getContactList(int $listId): ResponseInterface
42+
{
43+
return $this->handleResponse(
44+
$this->httpGet($this->getBasePath() . '/lists/' . $listId)
45+
);
46+
}
47+
48+
/**
49+
* Create a new Contact List.
50+
*
51+
* @param string $name
52+
* @return ResponseInterface
53+
*/
54+
public function createContactList(string $name): ResponseInterface
55+
{
56+
return $this->handleResponse(
57+
$this->httpPost(
58+
path: $this->getBasePath() . '/lists',
59+
body: ['name' => $name]
60+
)
61+
);
62+
}
63+
64+
/**
65+
* Update an existing Contact List by ID.
66+
*
67+
* @param int $listId
68+
* @param string $name
69+
* @return ResponseInterface
70+
*/
71+
public function updateContactList(int $listId, string $name): ResponseInterface
72+
{
73+
return $this->handleResponse(
74+
$this->httpPatch(
75+
path: $this->getBasePath() . '/lists/' . $listId,
76+
body: ['name' => $name]
77+
)
78+
);
79+
}
80+
81+
/**
82+
* Delete a Contact List by ID.
83+
*
84+
* @param int $listId
85+
* @return ResponseInterface
86+
*/
87+
public function deleteContactList(int $listId): ResponseInterface
88+
{
89+
return $this->handleResponse(
90+
$this->httpDelete($this->getBasePath() . '/lists/' . $listId)
91+
);
92+
}
93+
94+
/**
95+
* Get a Contact by ID (UUID)
96+
*
97+
* @param string $contactId
98+
* @return ResponseInterface
99+
*/
100+
public function getContactById(string $contactId): ResponseInterface
101+
{
102+
return $this->getContact($contactId);
103+
}
104+
105+
/**
106+
* Get a Contact by Email.
107+
*
108+
* @param string $email
109+
* @return ResponseInterface
110+
*/
111+
public function getContactByEmail(string $email): ResponseInterface
112+
{
113+
return $this->getContact($email);
114+
}
115+
35116
/**
36117
* Create a new Contact.
37118
*
@@ -96,6 +177,19 @@ public function getAccountId(): int
96177
return $this->accountId;
97178
}
98179

180+
/**
181+
* Get a Contact by ID or Email.
182+
*
183+
* @param string $idOrEmail
184+
* @return ResponseInterface
185+
*/
186+
private function getContact(string $idOrEmail): ResponseInterface
187+
{
188+
return $this->handleResponse(
189+
$this->httpGet($this->getBasePath() . '/' . urlencode($idOrEmail))
190+
);
191+
}
192+
99193
/**
100194
* Update an existing Contact.
101195
*

0 commit comments

Comments
 (0)