├── README.md ├── assets ├── css │ └── styles.css └── js │ ├── scripts.js │ └── timer.js ├── config ├── api.php ├── auth.php ├── data.php └── examples.php ├── index.php └── src ├── DataFilter.php ├── MailUpClient.php ├── MailUpException.php └── bootstrap.php /README.md: -------------------------------------------------------------------------------- 1 | PHP Rest API Client 2 | ================ 3 | PHP REST API integration/implementation samples 4 | 5 | Requirements 6 | ------------------------ 7 | * Apache web server 8 | * A valid MailUp account ( trial accounts allowed ) 9 | * Your own API application keys 10 | 11 | 12 | notes : 13 | * For further API information, please visit [MailUp REST API Help](http://help.mailup.com/display/mailupapi/REST+API) 14 | * For MailUp trial account activation please go to [MailUp web site](http://www.mailup.com/p/pc/mailup-free-trial-d44.htm) 15 | * For further API application keys information please go to [Get API application keys](http://help.mailup.com/display/mailupapi/Get+a+Developer+Account) 16 | 17 | Samples overview 18 | ------------------------ 19 | This project encloses a short list of pre definied samples describing some of the most common processes within MailUp. 20 | 21 | * Sample 1 - Importing recipients into a new group 22 | * Sample 2 - Unsubscribing a recipient from a group 23 | * Sample 3 - Updating a recipient information 24 | * Sample 4 - Creating a message from a custom template (deprecated) 25 | * Sample 5 - Building a message with images and attachments 26 | * Sample 6 - Tagging an email message 27 | * Sample 7 - Sending an email message 28 | * Sample 8 - Displaying statistics with regards to message created in sample 4 or 5 and/or sent out in sample 7 29 | 30 | Before starting 31 | ------------------------ 32 | After you get the MailUp account ID and the API application keys, please set them into your local config file. You can find the path of the config file here: 33 | ``` 34 | rest-samples-php/config/auth.php 35 | ``` 36 | 37 | Debugging tool 38 | ------------------------ 39 | 40 | Notes 41 | ------------------------ 42 | To learn more about API keys and how to get them, please refer to [MailUp REST API Keys and endpoints](http://help.mailup.com/display/mailupapi/All+API+Keys+and+Endpoints+in+one+page) 43 | 44 | Revision history 45 | ------------------------ 46 | -------------------------------------------------------------------------------- /assets/css/styles.css: -------------------------------------------------------------------------------- 1 | h3 { 2 | color: #174222; 3 | } 4 | .example-body { 5 | word-break: break-all; 6 | height: auto; 7 | min-height: 34px; 8 | max-height: 400px; 9 | overflow-y: auto; 10 | margin-top: 15px; 11 | } 12 | .panel-default { 13 | border-color: #ddd; 14 | } 15 | .bd-pageheader { 16 | padding: 2rem 15px; 17 | margin-bottom: 1.5rem; 18 | color: #c0dac0; 19 | text-align: center; 20 | background-color: #327040; 21 | } 22 | .error-answer { 23 | color: #a94442; 24 | margin-top:10px; 25 | margin-left:15px; 26 | } 27 | .successfully-answer { 28 | color: #198534; 29 | margin-top:10px; 30 | margin-left:15px; 31 | } 32 | .spoiler-wrap { 33 | background:#fff; 34 | margin: 0 0 1px; 35 | } 36 | .spoiler-head { 37 | background:#efefef; 38 | cursor:pointer; 39 | margin-top:10px; 40 | margin-left:15px; 41 | margin-right:15px; 42 | color:#174222; 43 | text-decoration: underline; 44 | padding-left:15px; 45 | } 46 | .spoiler-body { 47 | padding:15px; 48 | } 49 | .spoiler-wrap.disabled .spoiler-body { 50 | display:none; 51 | margin-top:5px; 52 | } 53 | .spoiler-wrap.active { 54 | border-color:rgb(218, 223, 217); 55 | } 56 | .spoiler-wrap.active .spoiler-head { 57 | background:rgb(211, 224, 207); 58 | } 59 | .img { 60 | width: 55px; 61 | height: 55px; 62 | } 63 | .auth-panel { 64 | height: 240px; 65 | margin-top: 10px; 66 | } 67 | .auth-panel.panel-default > .panel-heading { 68 | border: none; 69 | position: relative; 70 | top: -10px; 71 | display: inline; 72 | padding: 6px; 73 | margin: 0; 74 | left: 20px; 75 | background-color: inherit; 76 | } 77 | .auth-panel-sign { 78 | text-align: center; 79 | margin-top: 50px; 80 | } 81 | .right { 82 | text-align: right; 83 | } 84 | #result-string { 85 | margin-top: 15px; 86 | } 87 | #example { 88 | margin-top: 10px; 89 | margin-left:15px; 90 | margin-bottom:10px; 91 | } 92 | #unix-time { 93 | display: none; 94 | } -------------------------------------------------------------------------------- /assets/js/scripts.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function () { 2 | $('.spoiler-body').hide(300); 3 | 4 | $(document).on('click', '.spoiler-head', function (e) { 5 | e.preventDefault(); 6 | $(this).parents('.spoiler-wrap').toggleClass("active").find('.spoiler-body').slideToggle(); 7 | }); 8 | 9 | for (let i = 1; i < 9; i++) { 10 | if ($("#example-" + i + " > .spoiler-wrap").length !== 0 | $("#example-" + i + " > .error-answer").length !== 0) { 11 | $("#example-" + i).attr("aria-expanded", "true").addClass("in"); 12 | }; 13 | } 14 | 15 | $("#auth-form").submit(function (e) { 16 | if ($("#username").val() === "" | $("#password").val() === "") { 17 | e.preventDefault(); 18 | } 19 | }); 20 | }); -------------------------------------------------------------------------------- /assets/js/timer.js: -------------------------------------------------------------------------------- 1 | window.onload = function () { 2 | const duration = $("#unix-time").text(); 3 | const display = document.querySelector('#token-time'); 4 | startTimer(duration, display); 5 | }; 6 | 7 | function startTimer(duration, display) { 8 | let timer = duration; 9 | let hours, minutes, seconds; 10 | let time; 11 | 12 | let timerId = setInterval(function () { 13 | 14 | --timer; 15 | 16 | hours = parseInt(timer / (60 * 60), 10); 17 | minutes = parseInt((timer / 60 - hours * 60), 10); 18 | seconds = parseInt(timer - minutes * 60, 10); 19 | 20 | 21 | hours = hours < 10 ? "0" + hours : hours; 22 | minutes = minutes < 10 ? "0" + minutes : minutes; 23 | seconds = seconds < 10 ? "0" + seconds : seconds; 24 | 25 | if(hours !== 0) { 26 | time = hours + "h "; 27 | } 28 | 29 | if (timer === 0) { 30 | hours = minutes = seconds = "00"; 31 | clearInterval(timerId); 32 | } 33 | 34 | time = minutes + "m " + seconds + "s"; 35 | 36 | display.textContent = time; 37 | 38 | }, 1000); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /config/api.php: -------------------------------------------------------------------------------- 1 | 'https://services.mailup.com/Authorization/OAuth/LogOn', 5 | 'authorization' => 'https://services.mailup.com/Authorization/OAuth/Authorization', 6 | 'token' => 'https://services.mailup.com/Authorization/OAuth/Token', 7 | 'console' => 'https://services.mailup.com/API/v1.1/Rest/ConsoleService.svc', 8 | 'mail_stats' => 'https://services.mailup.com/API/v1.1/Rest/MailStatisticsService.svc' 9 | ); -------------------------------------------------------------------------------- /config/auth.php: -------------------------------------------------------------------------------- 1 | '', 5 | 'secret_key' => '', 6 | 'callback_url' => 'http://mailup.test/' 7 | ); 8 | -------------------------------------------------------------------------------- /config/data.php: -------------------------------------------------------------------------------- 1 | '1' 5 | ); -------------------------------------------------------------------------------- /config/examples.php: -------------------------------------------------------------------------------- 1 | 'Run example code 1 - Import recipients', 5 | '2' => 'Run example code 2 - Unsubscribe a recipient', 6 | '3' => 'Run example code 3 - Update a recipient', 7 | '4' => 'Run example code 4 - Create a message from template', 8 | '5' => 'Run example code 5 - Create a message from scratch', 9 | '6' => 'Run example code 6 - Tag a message', 10 | '7' => 'Run example code 7 - Send a message', 11 | '8' => 'Run example code 8 - Retrieve statistics' 12 | ); -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | MailUp Demo Client 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 |
19 |

MailUp Demo Client

20 |
21 | 22 |

Authentication

23 |
24 |
25 |
26 |
Authorization code grant
27 |
28 |
29 |
30 |
31 |
32 | 33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
Password grant
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 | getAccessToken() === null): ?> 68 |
Unauthorized
69 | 70 |
Authorized.
71 |
Token: " . $mailUp->getAccessToken(); ?>
72 | 73 | 74 |
75 | 76 |
77 | 78 |
79 | getTokenTime(); 81 | if(null !== $token_time): 82 | ?> 83 | 84 | Expires in: 85 | 86 |
87 | getAccessToken() !== null): ?> 88 |
89 |
90 | 91 |
92 |
93 | 94 |
95 | 96 |

Custom method call

97 |
98 |
99 |
100 |
101 |
102 | 103 | 109 |
110 |
111 | 112 | 116 |
117 |
118 | 119 | 123 |
124 |
125 | 126 | 127 |
128 |
129 |
130 | 131 | 132 |
133 | 134 |

135 |
136 |
137 |
138 | 139 |

Run example set of calls

140 | $text): ?> 141 |
142 |
143 |
144 |

145 | 146 |

147 |
148 |
149 |
150 | 151 |
152 | 156 | 157 |
158 |
159 | 160 |
161 | 191 |
192 | 193 | 194 |
195 | Error code: ". $errors['example_' . $number]['code'] . "
"; 197 | echo "Message: " . $errors['example_' . $number]['message'] . "
"; 198 | echo "URL: " . $errors['example_' . $number]['url'] . ""; 199 | ?> 200 |
201 | 202 |
Example methods completed successfully
203 | 204 | 205 |
206 |
207 |
208 | 209 | 210 |
211 | 212 | -------------------------------------------------------------------------------- /src/DataFilter.php: -------------------------------------------------------------------------------- 1 | clientId = $auth['client_id']; 19 | $this->secretKey = $auth['secret_key']; 20 | $this->api = $api; 21 | 22 | $this->loadToken(); 23 | } 24 | 25 | public function getAccessToken() 26 | { 27 | return $this->accessToken; 28 | } 29 | 30 | public function getConsoleUrl() 31 | { 32 | return $this->api['console']; 33 | } 34 | 35 | public function getMailStatsUrl() 36 | { 37 | return $this->api['mail_stats']; 38 | } 39 | 40 | public function getErrorUrl() 41 | { 42 | return $this->errorUrl; 43 | } 44 | 45 | public function getTokenTime() 46 | { 47 | $time = $this->tokenTime; 48 | 49 | if (null !== $this->tokenTime) { 50 | $time = $this->tokenTime - time(); 51 | } 52 | 53 | return $time; 54 | } 55 | 56 | public function logonByKey($callback) 57 | { 58 | $url = $this->api['logon'] . "?client_id=" . $this->clientId . "&client_secret=" . $this->secretKey . "&response_type=code&redirect_uri=" . $callback; 59 | header("Location: " . $url); 60 | } 61 | 62 | public function retrieveTokenByCode($code) 63 | { 64 | $url = $this->api['token'] . "?code=" . $code . "&grant_type=authorization_code"; 65 | 66 | $curl = curl_init(); 67 | curl_setopt($curl, CURLOPT_URL, $url); 68 | // Return result as string to script 69 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 70 | // Not verify the host certificate 71 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 72 | // Not check name in the certificate 73 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 74 | 75 | $result = curl_exec($curl); 76 | $response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 77 | curl_close($curl); 78 | 79 | if ($response_code !== 200 & $response_code !== 302) { 80 | $this->clearToken(); 81 | throw new MailUpException($code, "Authorization error"); 82 | } 83 | 84 | $result = json_decode($result); 85 | 86 | $this->saveToken($result->access_token, $result->refresh_token, $result->expires_in); 87 | } 88 | 89 | public function retrieveTokenByPassword($username, $password) 90 | { 91 | $curl = curl_init(); 92 | curl_setopt($curl, CURLOPT_URL, $this->api['token']); 93 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 94 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 95 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 96 | curl_setopt($curl, CURLOPT_POST, true); 97 | 98 | $username = DataFilter::convertToString($username); 99 | $password = DataFilter::convertToString($password); 100 | 101 | $body = 'grant_type=password&username=' . rawurlencode($username) . '&password=' . rawurlencode($password) . '&client_id=' . rawurlencode($this->clientId) . '&client_secret=' . rawurlencode($this->secretKey); 102 | $headers = array( 103 | "Content-type: application/x-www-form-urlencoded", 104 | "Content-length: " . strlen($body), 105 | "Accept: application/json", 106 | "Authorization: Basic " . base64_encode($this->clientId . ':' . $this->secretKey) 107 | ); 108 | 109 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 110 | curl_setopt($curl, CURLOPT_POSTFIELDS, $body); 111 | 112 | $result = curl_exec($curl); 113 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 114 | curl_close($curl); 115 | 116 | if ($code !== 200 & $code !== 302) { 117 | $this->clearToken(); 118 | throw new MailUpException($code, "Authorization error"); 119 | } 120 | 121 | $result = json_decode($result); 122 | 123 | $this->saveToken($result->access_token, $result->refresh_token, $result->expires_in); 124 | } 125 | 126 | public function refreshToken() 127 | { 128 | $body = "client_id=" . rawurlencode($this->clientId) . "&client_secret=" . rawurlencode($this->secretKey) . "&refresh_token=" . rawurlencode($this->refreshToken) . "&grant_type=refresh_token"; 129 | $headers = array( 130 | "Content-type: application/x-www-form-urlencoded", 131 | "Content-length: " . strlen($body), 132 | "Accept: application/json" 133 | ); 134 | 135 | $curl = curl_init(); 136 | curl_setopt($curl, CURLOPT_URL, $this->api['token']); 137 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 138 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 139 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 140 | curl_setopt($curl, CURLOPT_POST, true); 141 | curl_setopt($curl, CURLOPT_POSTFIELDS, $body); 142 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 143 | 144 | $result = curl_exec($curl); 145 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 146 | curl_close($curl); 147 | 148 | if ($code != 200 && $code != 302) { 149 | $this->clearToken(); 150 | throw new MailUpException($code, "Authorization error"); 151 | } 152 | 153 | $result = json_decode($result); 154 | 155 | $this->saveToken($result->access_token, $result->refresh_token, $result->expires_in); 156 | } 157 | 158 | public function makeRequest($method, $content_type = "JSON", $url, $body = "", $refresh = true) 159 | { 160 | $temp_file = null; 161 | $content_type = ($content_type === "XML" ? "application/xml" : "application/json"); 162 | $headers = array( 163 | "Content-type: " . $content_type, 164 | "Content-length: " . strlen($body), 165 | "Accept: " . $content_type, 166 | "Authorization: Bearer " . $this->accessToken 167 | ); 168 | 169 | $curl = curl_init(); 170 | curl_setopt($curl, CURLOPT_URL, $url); 171 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 172 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 173 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 174 | 175 | switch($method) { 176 | case "POST": 177 | curl_setopt($curl, CURLOPT_POST, true); 178 | curl_setopt($curl, CURLOPT_POSTFIELDS, $body); 179 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 180 | break; 181 | case "PUT": 182 | $temp_file = tmpfile(); 183 | fwrite($temp_file, $body); 184 | fseek($temp_file, 0); 185 | curl_setopt($curl, CURLOPT_PUT, true); 186 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 187 | curl_setopt($curl, CURLOPT_INFILE, $temp_file); 188 | curl_setopt($curl, CURLOPT_INFILESIZE, strlen($body)); 189 | break; 190 | case "DELETE": 191 | $headers[1] = "Content-length: 0"; 192 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 193 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 194 | break; 195 | default: 196 | $headers[1] = "Content-length: 0"; 197 | curl_setopt($curl, CURLOPT_HTTPGET, true); 198 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 199 | } 200 | 201 | $result = curl_exec($curl); 202 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 203 | 204 | if ($temp_file !== null) { 205 | fclose($temp_file); 206 | } 207 | curl_close($curl); 208 | 209 | if ($code === 401 & $refresh === true) { 210 | $this->refreshToken(); 211 | return $this->makeRequest($method, $content_type, $url, $body, false); 212 | } 213 | 214 | if ($code !== 200 & $code !== 302) { 215 | $result = json_decode($result); 216 | $error_desc = ""; 217 | 218 | if (isset($result->ErrorDescription)) { 219 | $error_desc = $result->ErrorDescription; 220 | } else { 221 | $error_desc = "Unknown error"; 222 | } 223 | 224 | throw new MailUpException($code, $error_desc); 225 | } 226 | 227 | return $result; 228 | } 229 | 230 | private function loadToken() 231 | { 232 | if (isset($_COOKIE['access_token'])) { 233 | $this->accessToken = $_COOKIE["access_token"]; 234 | } 235 | 236 | if (isset($_COOKIE['refresh_token'])) { 237 | $this->refreshToken = $_COOKIE["refresh_token"]; 238 | }; 239 | 240 | if (isset($_COOKIE['token_time'])) { 241 | $this->tokenTime = $_COOKIE['token_time']; 242 | } 243 | } 244 | 245 | private function clearToken() 246 | { 247 | $this->accessToken = null; 248 | $this->refreshToken = null; 249 | $this->tokenTime = null; 250 | 251 | setcookie("access_token", $this->accessToken, $this->tokenTime); 252 | setcookie("refresh_token", $this->refreshToken, $this->tokenTime); 253 | setcookie("token_time", $this->tokenTime, $this->tokenTime); 254 | } 255 | private function saveToken($token, $refresh, $time) 256 | { 257 | $this->accessToken = $token; 258 | $this->refreshToken = $refresh; 259 | $this->tokenTime = time() + $time; 260 | 261 | setcookie("access_token", $this->accessToken, $this->tokenTime); 262 | setcookie("refresh_token", $this->refreshToken, $this->tokenTime); 263 | setcookie("token_time", $this->tokenTime, $this->tokenTime); 264 | } 265 | 266 | public function runExample1($list_id = -1) 267 | { 268 | $_SESSION['examples']['example_1'] = array(); 269 | 270 | if ($list_id === -1) { 271 | $result = $this->getResult( 272 | "POST", 273 | "JSON", 274 | json_encode( 275 | array( 276 | "Deletable" => true, 277 | "Name" => "test import", 278 | "Notes" => "test import" 279 | ) 280 | ), 281 | "Console", 282 | "/Console/List/" . 100 . "/Groups", 283 | "If the list does not contain a group named test \"import\", create it" 284 | ); 285 | } else { 286 | $result = $this->getResult( 287 | "GET", 288 | "JSON", 289 | null, 290 | "Console", 291 | "/Console/List/" . $list_id . "/Groups", 292 | "Given a default list id (use idList = " . $list_id . "), request for user visible groups" 293 | ); 294 | } 295 | 296 | $obj_result = json_decode($result['res_body']); 297 | $items = $obj_result->Items; 298 | for ($i = 0; $i < count($items); $i++) { 299 | $group = $items[$i]; 300 | if ("test import" === $group->Name) $group_id = $group->idGroup; 301 | } 302 | $_SESSION['examples']['example_1'][] = $result; 303 | 304 | $_SESSION['group_id'] = $group_id; 305 | 306 | // Request for dynamic fields to map recipient name and surname 307 | $result = $this->getResult( 308 | "GET", 309 | "JSON", 310 | null, 311 | "Console", 312 | "/Console/Recipient/DynamicFields", 313 | "Request for dynamic fields to map recipient name and surname" 314 | ); 315 | $_SESSION['examples']['example_1'][] = $result; 316 | 317 | // Import recipients to group 318 | $result = $this->getResult( 319 | "POST", 320 | "JSON", 321 | json_encode( 322 | array( 323 | array( 324 | "Email" => "test@test.test", 325 | "Fields" => array( 326 | array( 327 | "Description" => "String description", 328 | "Id" => 1, 329 | "Value" => "String value" 330 | ) 331 | ), 332 | "MobileNumber" => "", 333 | "MobilePrefix" => "", 334 | "Name" => "John Smith" 335 | ) 336 | ) 337 | ), 338 | "Console", 339 | "/Console/Group/" . $group_id . "/Recipients", 340 | "Import recipients to group" 341 | ); 342 | $_SESSION['examples']['example_1'][] = $result; 343 | 344 | $import_id = $result['res_body']; 345 | // Check the import result 346 | $result = $this->getResult( 347 | "GET", 348 | "JSON", 349 | null, 350 | "Console", 351 | "/Console/Import/" . $import_id, 352 | "Check the import result" 353 | ); 354 | $_SESSION['examples']['example_1'][] = $result; 355 | } 356 | 357 | public function runExample2($group_id) 358 | { 359 | $_SESSION['examples']['example_2'] = array(); 360 | // Request for recipient in a group 361 | $result = $this->getResult( 362 | "GET", 363 | "JSON", 364 | null, 365 | "Console", 366 | "/Console/Group/" . $group_id . "/Recipients", 367 | "Request for recipient in a group" 368 | ); 369 | 370 | $_SESSION['examples']['example_2'][] = $result; 371 | $obj_result = json_decode($result['res_body']); 372 | $items = $obj_result->Items; 373 | 374 | if (count($items) > 0) { 375 | $recipient_id = $items[0]->idRecipient; 376 | // Pick up a recipient and unsubscribe it 377 | $result = $this->getResult( 378 | "DELETE", 379 | "JSON", 380 | null, 381 | "Console", 382 | "/Console/Group/" . $group_id . "/Unsubscribe/" . $recipient_id, 383 | "Pick up a recipient and unsubscribe it" 384 | ); 385 | $_SESSION['examples']['example_2'][] = $result; 386 | } 387 | } 388 | 389 | public function runExample3($list_id) 390 | { 391 | $_SESSION['examples']['example_3'] = array(); 392 | // Request for existing subscribed recipients 393 | $result = $this->getResult( 394 | "GET", 395 | "JSON", 396 | null, 397 | "Console", 398 | "/Console/List/" . $list_id . "/Recipients/Subscribed", 399 | "Request for existing subscribed recipients" 400 | ); 401 | $_SESSION['examples']['example_3'][] = $result; 402 | 403 | $obj_result = json_decode($result['res_body']); 404 | $items = $obj_result->Items; 405 | 406 | if (count($items) > 0) { 407 | $fields = '{ 408 | "Description": "", 409 | "Id": 1, 410 | "Value": "Updated value" 411 | }'; 412 | $items[0]->Fields[0] = json_decode($fields); 413 | 414 | $result = $this->getResult( 415 | "PUT", 416 | "JSON", 417 | json_encode($items[0]), 418 | "Console", 419 | "/Console/Recipient/Detail", 420 | "Update the modified recipient" 421 | ); 422 | 423 | $_SESSION['examples']['example_3'][] = $result; 424 | } 425 | } 426 | 427 | public function runExample4($list_id) 428 | { 429 | $_SESSION['examples']['example_4'] = array(); 430 | // Get the available template list 431 | $result = $this->getResult( 432 | "GET", 433 | "JSON", 434 | null, 435 | "Console", 436 | "/Console/List/" . $list_id . "/Templates", 437 | "Get the available template list" 438 | ); 439 | $_SESSION['examples']['example_4'][] = $result; 440 | 441 | $template_id = 1; 442 | $obj_result = json_decode($result['res_body']); 443 | $items = $obj_result->Items; 444 | 445 | if (count($items) > 0) { 446 | $template_id = $items[1]->Id; 447 | } 448 | 449 | // Create the new message 450 | $result = $this->getResult( 451 | "POST", 452 | "JSON", 453 | null, 454 | "Console", 455 | "/Console/List/" . $list_id . "/Email/Template/" . $template_id, 456 | "Create the new message" 457 | ); 458 | $_SESSION['examples']['example_4'][] = $result; 459 | 460 | $obj_result = json_decode($result['res_body']); 461 | 462 | if (count($obj_result) > 0) { 463 | $email_id = $obj_result->idMessage; 464 | } 465 | 466 | $_SESSION["email_id"] = $email_id; 467 | 468 | // Request for messages list 469 | $result = $this->getResult( 470 | "GET", 471 | "JSON", 472 | null, 473 | "console", 474 | "/Console/List/" . $list_id . "/Emails", 475 | "Request for messages list" 476 | ); 477 | $_SESSION['examples']['example_4'][] = $result; 478 | } 479 | 480 | public function runExample5($list_id) 481 | { 482 | $_SESSION['examples']['example_5'] = array(); 483 | // Image bytes can be obtained from file, database or any other source 484 | $img = file_get_contents("https://www.mailup.it/risorse/logo/512x512.png"); 485 | $img = base64_encode($img); 486 | $body = '{ 487 | "Base64Data": "' . $img . '", 488 | "Name": "Avatar" 489 | }'; 490 | // Upload an image 491 | $result = $this->getResult( 492 | "POST", 493 | "JSON", 494 | $body, 495 | "Console", 496 | "/Console/List/" . $list_id . "/Images", 497 | "Upload an image" 498 | ); 499 | $_SESSION['examples']['example_5'][] = $result; 500 | 501 | // Get the images available 502 | $result = $this->getResult( 503 | "GET", 504 | "JSON", 505 | null, 506 | "Console", 507 | "/Console/Images", 508 | "Get the images available" 509 | ); 510 | $_SESSION['examples']['example_5'][] = $result; 511 | 512 | $img_src = ""; 513 | $arr_result = json_decode($result['res_body']); 514 | 515 | if (count($arr_result) > 0) { 516 | $img_src = $arr_result[0]; 517 | } 518 | 519 | $img_src = str_replace("\\", "\\\\", $img_src); 520 | $img_src = str_replace("/", "\\/", $img_src); 521 | // Not format this string, otherwise the server will return 400 error. 522 | $message = "

Hello<\\/p><\\/body><\\/html>"; 523 | $email = '{ 524 | "Subject": "Test Message Objective-C", 525 | "idList": "' . $list_id . '", 526 | "Content": "' . $message . '", 527 | "Embed": true, 528 | "IsConfirmation": true, 529 | "Fields": [], 530 | "Notes": "Some notes", 531 | "Tags": [], 532 | "TrackingInfo": { 533 | "CustomParams": "", 534 | "Enabled": true, 535 | "Protocols": [ 536 | "http" 537 | ] 538 | } 539 | }'; 540 | // Create and save "hello" message 541 | $result = $this->getResult( 542 | "POST", 543 | "JSON", 544 | $email, 545 | "Console", 546 | "/Console/List/" . $list_id . "/Email", 547 | "Create and save \"hello\" message" 548 | ); 549 | $_SESSION['examples']['example_5'][] = $result; 550 | 551 | $obj_result = json_decode($result['res_body']); 552 | 553 | if (count($obj_result) > 0) { 554 | $email_id = $obj_result->idMessage; 555 | } 556 | 557 | $_SESSION['email_id'] = $email_id; 558 | 559 | $attachment = "QmFzZSA2NCBTdHJlYW0="; 560 | $body = '{ 561 | "Base64Data": "' . $attachment . '", 562 | "Name": "TestFile.txt", 563 | "Slot": 1, 564 | "idList": 1, 565 | "idMessage": ' . $email_id . 566 | '}'; 567 | // Add an attachment 568 | $result = $this->getResult( 569 | "POST", 570 | "JSON", 571 | $body, 572 | "Console", 573 | "/Console/List/" . $list_id . "/Email/" . $email_id . "/Attachment/1", 574 | "Add an attachment" 575 | ); 576 | $_SESSION['examples']['example_5'][] = $result; 577 | // Retreive message details 578 | $result = $this->getResult( 579 | "GET", 580 | "JSON", 581 | null, 582 | "Console", 583 | "/Console/List/" . $list_id . "/Email/" . $email_id, 584 | "Retreive message detail" 585 | ); 586 | $_SESSION['examples']['example_5'][] = $result; 587 | } 588 | 589 | public function runExample6($list_id, $email_id) 590 | { 591 | $_SESSION['examples']['example_6'] = array(); 592 | // Create a new tag 593 | $result = $this->getResult( 594 | "POST", 595 | "JSON", 596 | '"test tag"', 597 | "Console", 598 | "/Console/List/" . $list_id . "/Tag", 599 | "Create a new tag" 600 | ); 601 | $_SESSION['examples']['example_6'][] = $result; 602 | 603 | $tag_id = 1; 604 | $obj_result = json_decode($result['res_body']); 605 | 606 | if (count($obj_result) > 0) { 607 | $tag_id = $obj_result->Id; 608 | } 609 | // Pick up a message and retrieve detailed informations 610 | $result = $this->getResult( 611 | "GET", 612 | "JSON", 613 | null, 614 | "Console", 615 | "/Console/List/" . $list_id . "/Email" . "/" . $email_id, 616 | "Pick up a message and retrieve detailed informations" 617 | ); 618 | $_SESSION['examples']['example_6'][] = $result; 619 | 620 | $obj_result = json_decode($result['res_body']); 621 | $tags = '[ 622 | { 623 | "Id": "' . $tag_id . '", 624 | "Enabled": true, 625 | "Name": "test tag" 626 | } 627 | ]'; 628 | $obj_result->Tags = json_decode($tags); 629 | 630 | // Add the tag to the message details and save 631 | $result = $this->getResult( 632 | "PUT", 633 | "JSON", 634 | json_encode($obj_result), 635 | "Console", 636 | "/Console/List/" . $list_id . "/Email/" . $email_id, 637 | "Add the tag to the message details and save" 638 | ); 639 | $_SESSION['examples']['example_6'][] = $result; 640 | } 641 | 642 | public function runExample7($list_id, $email_id) 643 | { 644 | $_SESSION['examples']['example_7'] = array(); 645 | // Get the list of the existing messages 646 | $result = $this->getResult( 647 | "GET", 648 | "JSON", 649 | null, 650 | "Console", 651 | "/Console/List/" . $list_id . "/Emails", 652 | "Get the list of the existing messages" 653 | ); 654 | $_SESSION['examples']['example_7'][] = $result; 655 | 656 | $obj_result = json_decode($result['res_body']); 657 | $items = $obj_result->Items; 658 | 659 | if (count($items) > 0) { 660 | $email_id = $items[0]->idMessage; 661 | } 662 | 663 | $_SESSION["email_id"] = $email_id; 664 | 665 | // Send email to all recipients in the list 666 | $result = $this->getResult( 667 | "POST", 668 | "JSON", 669 | null, 670 | "Console", 671 | "/Console/List/" . $list_id . "/Email" . "/" . $email_id . "/Send", 672 | "Send email to all recipients in the list" 673 | ); 674 | $_SESSION['examples']['example_7'][] = $result; 675 | } 676 | 677 | public function runExample8($email_id) 678 | { 679 | $_SESSION['examples']['example_8'] = array(); 680 | // Request (to MailStatisticsService.svc) for paged message views list for the previously sent message 681 | $result = $this->getResult( 682 | "GET", 683 | "JSON", 684 | null, 685 | "MailStatistics", 686 | "/Message" . "/" . $email_id . "/List/Views?pageSize=5&pageNum=0", 687 | "Request (to MailStatisticsService.svc) for paged message views list for the previously sent message" 688 | ); 689 | $_SESSION['examples']['example_8'][] = $result; 690 | } 691 | 692 | private function getResult($method, $type, $body, $env, $ep, $text) 693 | { 694 | $result = array(); 695 | $url = "Console" === $env ? $this->api['console'] : $this->api['mail_stats']; 696 | $url = $url . $ep; 697 | $this->errorUrl = $url; 698 | $result['res_body'] = $this->makeRequest($method, $type, $url, $body); 699 | $result['url'] = $env; 700 | $result['content_type'] = $type; 701 | $result['method'] = $method; 702 | $result['endpoint'] = $ep; 703 | $result['text'] = $text; 704 | $result['req_body'] = $body; 705 | return $result; 706 | } 707 | } -------------------------------------------------------------------------------- /src/MailUpException.php: -------------------------------------------------------------------------------- 1 | statusCode = $statusCode; 12 | } 13 | 14 | public function getStatusCode() 15 | { 16 | return $this->statusCode; 17 | } 18 | } -------------------------------------------------------------------------------- /src/bootstrap.php: -------------------------------------------------------------------------------- 1 | logonByKey($auth_data['callback_url']); 14 | // Code returned by MailUp 15 | } else if (isset($_GET['code'])) { 16 | $mailUp->retrieveTokenByCode($_GET['code']); 17 | } 18 | 19 | try { 20 | if (isset($_POST['logon_by_password'])) { 21 | $mailUp->retrieveTokenByPassword($_POST['username'], $_POST['password']); 22 | } 23 | } catch (MailUpException $e) { 24 | $error = "Exception Code: " . $e->getStatusCode() . "
" . $e->getMessage(); 25 | } 26 | 27 | if (isset($_POST['refresh_token'])) { 28 | $mailUp->refreshToken(); 29 | } 30 | 31 | $result = null; 32 | 33 | if (isset($_POST['execute_request'])) { 34 | $uri = $_POST['url'] . str_replace(' ','%20', $_POST['endpoint']); 35 | try { 36 | $result = $mailUp->makeRequest($_POST['method'], $_POST['content_type'], $uri, $_POST['body']); 37 | } catch (MailUpException $e) { 38 | $result = "Exception Code: " . $e->getStatusCode() . "
" . $e->getMessage(); 39 | } 40 | } 41 | // Examples 42 | session_start(); 43 | 44 | $email_id = 1; 45 | $group_id = 1; 46 | $examples = array(); 47 | $errors = array(); 48 | 49 | if (isset($_SESSION['group_id'])) { 50 | $group_id = $_SESSION['group_id']; 51 | } 52 | 53 | if (isset($_SESSION['email_id'])) { 54 | $email_id = $_SESSION['email_id']; 55 | } 56 | 57 | // Example 1 58 | try { 59 | if (isset($_POST['example_1'])) { 60 | $mailUp->runExample1($data['list_id']); 61 | unset($_SESSION['errors']['example_1']); 62 | } 63 | } catch (MailUpException $e) { 64 | $_SESSION['errors']['example_1'] = array( 65 | "code" => $e->getStatusCode(), 66 | "message" => $e->getMessage(), 67 | "url" => $mailUp->getErrorUrl() 68 | ); 69 | } 70 | // Example 2 71 | try { 72 | if (isset($_POST['example_2'])) { 73 | $mailUp->runExample2($group_id); 74 | unset($_SESSION['errors']['example_2']); 75 | } 76 | } catch (MailUpException $e) { 77 | $_SESSION['errors']['example_2'] = array( 78 | "code" => $e->getStatusCode(), 79 | "message" => $e->getMessage(), 80 | "url" => $mailUp->getErrorUrl() 81 | ); 82 | } 83 | // Example 3 84 | try { 85 | if (isset($_POST['example_3'])) { 86 | $mailUp->runExample3($data['list_id']); 87 | unset($_SESSION['errors']['example_3']); 88 | } 89 | } catch (MailUpException $e) { 90 | $_SESSION['errors']['example_3'] = array( 91 | "code" => $e->getStatusCode(), 92 | "message" => $e->getMessage(), 93 | "url" => $mailUp->getErrorUrl() 94 | ); 95 | } 96 | // Example 4 97 | try { 98 | if (isset($_POST['example_4'])) { 99 | $mailUp->runExample4($data['list_id']); 100 | unset($_SESSION['errors']['example_4']); 101 | } 102 | } catch (MailUpException $e) { 103 | $_SESSION['errors']['example_4'] = array( 104 | "code" => $e->getStatusCode(), 105 | "message" => $e->getMessage(), 106 | "url" => $mailUp->getErrorUrl() 107 | ); 108 | } 109 | // Example 5 110 | try { 111 | if (isset($_POST['example_5'])) { 112 | $mailUp->runExample5($data['list_id']); 113 | unset($_SESSION['errors']['example_5']); 114 | } 115 | } catch (MailUpException $e) { 116 | $_SESSION['errors']['example_5'] = array( 117 | "code" => $e->getStatusCode(), 118 | "message" => $e->getMessage(), 119 | "url" => $mailUp->getErrorUrl() 120 | ); 121 | } 122 | // Example 6 123 | try { 124 | if (isset($_POST['example_6'])) { 125 | $mailUp->runExample6($data['list_id'], $email_id); 126 | unset($_SESSION['errors']['example_6']); 127 | } 128 | } catch (MailUpException $e) { 129 | $_SESSION['errors']['example_6'] = array( 130 | "code" => $e->getStatusCode(), 131 | "message" => $e->getMessage(), 132 | "url" => $mailUp->getErrorUrl() 133 | ); 134 | } 135 | // Example 7 136 | try { 137 | if (isset($_POST['example_7'])) { 138 | $mailUp->runExample7($data['list_id'], $email_id); 139 | unset($_SESSION['errors']['example_7']); 140 | } 141 | } catch (MailUpException $e) { 142 | $_SESSION['errors']['example_7'] = array( 143 | "code" => $e->getStatusCode(), 144 | "message" => $e->getMessage(), 145 | "url" => $mailUp->getErrorUrl() 146 | ); 147 | } 148 | // Example 8 149 | try { 150 | if (isset($_POST['example_8'])) { 151 | $mailUp->runExample8($email_id); 152 | unset($_SESSION['errors']['example_8']); 153 | } 154 | } catch (MailUpException $e) { 155 | $_SESSION['errors']['example_8'] = array( 156 | "code" => $e->getStatusCode(), 157 | "message" => $e->getMessage(), 158 | "url" => $mailUp->getErrorUrl() 159 | ); 160 | } 161 | 162 | if (isset($_SESSION['examples'])) { 163 | $examples = $_SESSION['examples']; 164 | } 165 | 166 | if (isset($_SESSION['errors'])) { 167 | $errors = $_SESSION['errors']; 168 | } --------------------------------------------------------------------------------