|
| 1 | +<?php |
| 2 | + |
| 3 | +use Mailtrap\Config; |
| 4 | +use Mailtrap\DTO\Request\Inbox; |
| 5 | +use Mailtrap\Helper\ResponseHelper; |
| 6 | +use Mailtrap\MailtrapClient; |
| 7 | + |
| 8 | +require __DIR__ . '/../vendor/autoload.php'; |
| 9 | + |
| 10 | +// your API token from here https://mailtrap.io/api-tokens |
| 11 | +$apiKey = getenv('MAILTRAP_API_KEY'); |
| 12 | +$mailtrap = new MailtrapClient(new Config($apiKey)); |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Get a list of inboxes. |
| 17 | + * |
| 18 | + * GET https://mailtrap.io/api/accounts/{account_id}/inboxes |
| 19 | + */ |
| 20 | +try { |
| 21 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 22 | + |
| 23 | + $response = $mailtrap->sandbox()->inboxes()->getList($accountId); |
| 24 | + |
| 25 | + // print the response body (array) |
| 26 | + var_dump(ResponseHelper::toArray($response)); |
| 27 | +} catch (Exception $e) { |
| 28 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * Create an inbox |
| 34 | + * |
| 35 | + * POST https://mailtrap.io/api/accounts/{account_id}/projects/{project_id}/inboxes |
| 36 | + */ |
| 37 | +try { |
| 38 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 39 | + $projectId = getenv('MAILTRAP_PROJECT_ID'); |
| 40 | + $inboxName = 'First inbox'; |
| 41 | + |
| 42 | + $response = $mailtrap->sandbox()->inboxes()->create($accountId, $projectId, $inboxName); |
| 43 | + |
| 44 | + // print the response body (array) |
| 45 | + var_dump(ResponseHelper::toArray($response)); |
| 46 | +} catch (Exception $e) { |
| 47 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +/** |
| 52 | + * Get inbox attributes |
| 53 | + * |
| 54 | + * GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} |
| 55 | + */ |
| 56 | +try { |
| 57 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 58 | + $inboxId = getenv('MAILTRAP_INBOX_ID'); |
| 59 | + |
| 60 | + $response = $mailtrap->sandbox()->inboxes()->getInboxAttributes($accountId, $inboxId); |
| 61 | + |
| 62 | + // print the response body (array) |
| 63 | + var_dump(ResponseHelper::toArray($response)); |
| 64 | +} catch (Exception $e) { |
| 65 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +/** |
| 70 | + * Delete an inbox |
| 71 | + * |
| 72 | + * DELETE https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} |
| 73 | + */ |
| 74 | +try { |
| 75 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 76 | + $inboxId = getenv('MAILTRAP_INBOX_ID'); |
| 77 | + |
| 78 | + $response = $mailtrap->sandbox()->inboxes()->delete($accountId, $inboxId); |
| 79 | + |
| 80 | + // print the response body (array) |
| 81 | + var_dump(ResponseHelper::toArray($response)); |
| 82 | +} catch (Exception $e) { |
| 83 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +/** |
| 88 | + * Reset email address |
| 89 | + * |
| 90 | + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/reset_email_username |
| 91 | + */ |
| 92 | +try { |
| 93 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 94 | + $inboxId = getenv('MAILTRAP_INBOX_ID'); |
| 95 | + |
| 96 | + $response = $mailtrap->sandbox()->inboxes()->resetEmailAddress($accountId, $inboxId); |
| 97 | + |
| 98 | + // print the response body (array) |
| 99 | + var_dump(ResponseHelper::toArray($response)); |
| 100 | +} catch (Exception $e) { |
| 101 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +/** |
| 106 | + * Enable/Disable email address |
| 107 | + * |
| 108 | + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/toggle_email_username |
| 109 | + */ |
| 110 | +try { |
| 111 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 112 | + $inboxId = getenv('MAILTRAP_INBOX_ID'); |
| 113 | + |
| 114 | + $response = $mailtrap->sandbox()->inboxes()->toggleEmailAddress($accountId, $inboxId); |
| 115 | + |
| 116 | + // print the response body (array) |
| 117 | + var_dump(ResponseHelper::toArray($response)); |
| 118 | +} catch (Exception $e) { |
| 119 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +/** |
| 124 | + * Reset credentials |
| 125 | + * |
| 126 | + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/reset_credentials |
| 127 | + */ |
| 128 | +try { |
| 129 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 130 | + $inboxId = getenv('MAILTRAP_INBOX_ID'); |
| 131 | + |
| 132 | + $response = $mailtrap->sandbox()->inboxes()->resetSmtpCredentials($accountId, $inboxId); |
| 133 | + |
| 134 | + // print the response body (array) |
| 135 | + var_dump(ResponseHelper::toArray($response)); |
| 136 | +} catch (Exception $e) { |
| 137 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +/** |
| 142 | + * Mark as read |
| 143 | + * |
| 144 | + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/all_read |
| 145 | + */ |
| 146 | +try { |
| 147 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 148 | + $inboxId = getenv('MAILTRAP_INBOX_ID'); |
| 149 | + |
| 150 | + $response = $mailtrap->sandbox()->inboxes()->markAsRead($accountId, $inboxId); |
| 151 | + |
| 152 | + // print the response body (array) |
| 153 | + var_dump(ResponseHelper::toArray($response)); |
| 154 | +} catch (Exception $e) { |
| 155 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 156 | +} |
| 157 | + |
| 158 | + |
| 159 | +/** |
| 160 | + * Clean inbox |
| 161 | + * |
| 162 | + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/clean |
| 163 | + */ |
| 164 | +try { |
| 165 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 166 | + $inboxId = getenv('MAILTRAP_INBOX_ID'); |
| 167 | + |
| 168 | + $response = $mailtrap->sandbox()->inboxes()->clean($accountId, $inboxId); |
| 169 | + |
| 170 | + // print the response body (array) |
| 171 | + var_dump(ResponseHelper::toArray($response)); |
| 172 | +} catch (Exception $e) { |
| 173 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 174 | +} |
| 175 | + |
| 176 | + |
| 177 | +/** |
| 178 | + * Update an inbox |
| 179 | + * |
| 180 | + * PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} |
| 181 | + */ |
| 182 | +try { |
| 183 | + $accountId = getenv('MAILTRAP_ACCOUNT_ID'); |
| 184 | + $inboxId = getenv('MAILTRAP_INBOX_ID'); |
| 185 | + $newInboxName = 'New inbox name'; |
| 186 | + $newEmailUsername = 'new-email-username'; |
| 187 | + |
| 188 | + $response = $mailtrap->sandbox()->inboxes()->update( |
| 189 | + $accountId, |
| 190 | + $inboxId, |
| 191 | + new Inbox($newInboxName, $newEmailUsername) |
| 192 | + ); |
| 193 | + |
| 194 | + // print the response body (array) |
| 195 | + var_dump(ResponseHelper::toArray($response)); |
| 196 | +} catch (Exception $e) { |
| 197 | + echo 'Caught exception: ', $e->getMessage(), "\n"; |
| 198 | +} |
0 commit comments