-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathall.php
More file actions
224 lines (192 loc) · 6.74 KB
/
all.php
File metadata and controls
224 lines (192 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
use Mailtrap\Config;
use Mailtrap\DTO\Request\Contact\CreateContact;
use Mailtrap\DTO\Request\Contact\CreateContactEvent;
use Mailtrap\DTO\Request\Contact\ImportContact;
use Mailtrap\DTO\Request\Contact\UpdateContact;
use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapGeneralClient;
use Mailtrap\DTO\Request\Contact\ContactExportFilter;
require __DIR__ . '/../../vendor/autoload.php';
$accountId = $_ENV['MAILTRAP_ACCOUNT_ID'];
$config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens
$contacts = (new MailtrapGeneralClient($config))->contacts($accountId);
/**
* 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
*
* POST https://mailtrap.io/api/accounts/{account_id}/contacts
*/
try {
$response = $contacts->createContact(
CreateContact::init(
'john.smith@example.com',
['first_name' => 'John', 'last_name' => 'Smith'], // Fields
[1, 2] // List IDs to which the contact will be added
)
);
// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
/**
* Update contact by ID or Email.
*
* PATCH https://mailtrap.io/api/accounts/{account_id}/contacts/{id_or_email}
*/
try {
// Update contact by ID
$response = $contacts->updateContactById(
'019706a8-0000-0000-0000-4f26816b467a',
UpdateContact::init(
'john.smith@example.com',
['first_name' => 'John', 'last_name' => 'Smith'], // Fields
[3], // List IDs to which the contact will be added
[1, 2], // List IDs from which the contact will be removed
true // Unsubscribe the contact
)
);
// OR update contact by email
$response = $contacts->updateContactByEmail(
'john.smith@example.com',
UpdateContact::init(
'john.smith@example.com',
['first_name' => 'John', 'last_name' => 'Smith'], // Fields
[3], // List IDs to which the contact will be added
[1, 2], // List IDs from which the contact will be removed
true // Unsubscribe the contact
)
);
// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
/**
* Delete contact
*
* Delete https://mailtrap.io/api/accounts/{account_id}/contacts/{id_or_email}
*/
try {
// Delete contact by ID
$response = $contacts->deleteContactById('019706a8-0000-0000-0000-4f26816b467a');
// OR delete contact by email
$response = $contacts->deleteContactByEmail('john.smith@example.com');
// Print the response status code
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
/**
* Import contacts in bulk with support for custom fields and list management.
* Existing contacts with matching email addresses will be updated automatically.
* You can import up to 50,000 contacts per request.
* The import process runs asynchronously - use the returned import ID to check the status and results.
*
* POST https://mailtrap.io/api/accounts/{account_id}/contacts/imports
*/
try {
$contactsToImport = [
new ImportContact(
email: 'customer1@example.com',
fields: ['first_name' => 'John', 'last_name' => 'Smith', 'zip_code' => 11111],
listIdsIncluded: [1, 2],
listIdsExcluded: [4, 5]
),
new ImportContact(
email: 'customer2@example.com',
fields: ['first_name' => 'Joe', 'last_name' => 'Doe', 'zip_code' => 22222],
listIdsIncluded: [1],
listIdsExcluded: [4]
),
];
$response = $contacts->importContacts($contactsToImport);
// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
/**
* Get the status of a contact import by ID.
*
* GET https://mailtrap.io/api/accounts/{account_id}/contacts/imports/{import_id}
*/
try {
$importId = 1; // Replace 1 with the actual import ID
$response = $contacts->getContactImport($importId);
// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
/**
* Create a new Contact Event
*
* POST https://mailtrap.io/api/accounts/{account_id}/contacts/{contact_identifier}/events
*/
try {
// Create event using contact email
$response = $contacts->createContactEvent(
'john.smith@example.com', // Contact identifier (email or UUID)
CreateContactEvent::init(
'UserLogin',
[
'user_id' => 101,
'user_name' => 'John Smith',
'is_active' => true,
'last_seen' => null
]
)
);
// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
/**
* Create a new Contact Export (asynchronous task)
*
* POST https://mailtrap.io/api/accounts/{account_id}/contacts/exports
*/
try {
$filters = [
// Export contacts that belong to lists 1 or 2
ContactExportFilter::init('list_id', 'equal', [1, 2]),
// Only subscribed contacts
ContactExportFilter::init('subscription_status', 'equal', 'subscribed'),
];
$response = $contacts->createContactExport($filters);
// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}
/**
* Get Contact Export status / download URL
* (Poll this endpoint until status becomes `finished` and `url` is not null)
*
* GET https://mailtrap.io/api/accounts/{account_id}/contacts/exports/{export_id}
*/
try {
$exportId = 1; // Replace 1 with the actual export ID obtained from createContactExport
$response = $contacts->getContactExport($exportId);
// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), PHP_EOL;
}