-
Notifications
You must be signed in to change notification settings - Fork 1
/
example-full.php
283 lines (260 loc) · 9.54 KB
/
example-full.php
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* Smartmessages API example script.
*
* Example script demonstrating all available Smartmessages API calls using the Smartmessages API wrapper class.
* Note that this script will not work as is - you will need to substitute your own login ID, password, API key
* and test list IDs in the variables below (or in an override file)
* @author Marcus Bointon <[email protected]>
* @copyright 2015 Synchromedia Limited
* @link https://info.smartmessages.net/ Smartmessages mailing lists management
* @link https://wiki.smartmessages.net/ Smartmessages user and developer documentation
*/
/**
* Include API client class if you're not using composer's autoloader
*/
require 'src/Smartmessages/Client.php';
/**
* @var string $baseurl The initial root URL of API calls
*/
$baseurl = 'https://www.smartmessages.net/api/';
/**
* @var string $user The user name (email address) you use to log into your Smartmessages account
*/
$user = '';
/**
* @var string $pass The password you use to log into your Smartmessages account
*/
$pass = '';
/**
* @var string $apikey The API key that appears on the account page in your Smartmessages account
*/
$apikey = '';
/**
* @var string $endpoint The access location that the login call tells you to use for subsequent requests
* Will usually be the same as the initial access URL, but may change in future, so please use it
*/
$endpoint = '';
/**
* @var integer $testlistid A list id to test with, as found on the contacts page of your Smartmessages account
* or in response to the getLists API call
*/
$testlistid = 0;
//You can set the above properties in this included file in order not to pollute this example code with real data!
if (file_exists('testsettings.php')) {
include 'testsettings.php';
}
try {
//Create an API instance in debug mode (will produce debug output)
$smartmessages = new Smartmessages\Client(true);
//Login
$smartmessages->login($user, $pass, $apikey, $baseurl);
//Manage campaign folders
$cid = $smartmessages->addCampaign('api test campaign');
if ($cid > 0) {
$smartmessages->updateCampaign($cid, 'updated api campaign');
$smartmessages->deleteCampaign($cid);
}
$campaigns = $smartmessages->getCampaigns();
$mailshots = $smartmessages->getCampaignMailshots(key($campaigns));
//Manage mailing lists
$tl = $smartmessages->getTestList();
echo "<p>Test list: id: {$tl['id']}, name: {$tl['name']}: description: {$tl['description']}</p>";
$mlid = $smartmessages->addList('new test', 'description', true);
$lists = $smartmessages->getLists(true); //Get all lists, not just visible ones
if ($mlid > 0) {
$r = $smartmessages->updateList($mlid, 'updated new test', 'updated description', false);
$smartmessages->deleteList($mlid);
}
echo "<p>Mailing lists:</p>\n<ul>\n";
foreach ($lists as $list) {
echo "<li>List ID {$list['id']}, {$list['name']}: {$list['description']}</li>\n";
}
echo "</ul>\n";
//Manage templates
$templates = $smartmessages->getTemplates(true);
$smartmessages->getTemplate(key($templates));
//Create a new template
$template1_id = $smartmessages->addTemplate(
'apitest',
'<html><head></head><body><h1>HTML</h1></body></html>',
'plain',
'subject',
'test template',
false,
'en',
false
);
//Create a new template by loading it from a URL
$template2_id = $smartmessages->addTemplateFromURL(
'apitest url',
'http://www.google.com/',
'mysubject',
'Grabbed from google'
);
//Update the template we created earlier by adding an image and asking it to import the image and rewrite its URL
$template1_id = $smartmessages->updateTemplate(
$template1_id,
'apitest',
'<html><head></head><body>'.
'<h1>HTML <img alt="Butterfly" src="http://www.smartmessages.net/images/butterfly.png">'.
'</h1></body></html>',
'plain',
'subject',
'test template',
false,
'en',
true
);
$smartmessages->getTemplate($template1_id);
$smartmessages->getTemplate($template2_id);
//Clean up
$smartmessages->deleteTemplate($template2_id);
$smartmessages->deleteTemplate($template1_id);
//Get / Set Callback URL
$url = $smartmessages->getCallbackURL();
echo "<p>Callback URL is $url</p>\n";
try {
$smartmessages->setCallbackURL('http://www.example.com/callback.php'); //Valid URL test
$smartmessages->setCallbackURL('blah'); //Invalid URL test
} catch (Smartmessages\ParameterException $e) {
echo "</p>Callback URL set failed (as expected)</p>";
}
$url = $smartmessages->getCallbackURL();
echo "<p>Callback URL is now $url</p>\n";
//Email address validation
$address = 'rubbish';
if ($smartmessages->validateAddress($address)) {
echo "'$address' is valid<br />";
} else {
echo "'$address' is not valid<br />";
}
$address = '[email protected]';
if ($smartmessages->validateAddress($address)) {
echo "'$address' is valid<br />";
} else {
echo "'$address' is not valid<br />";
}
//Subscribe
try {
$smartmessages->subscribe($user, $testlistid, 'The Dude');
echo "<p>Subscribed OK</p>";
$smartmessages->subscribe($user, $testlistid, 'The Dude'); //This will fail as it's already subscribed
echo "<p>Subscribed OK</p>";
} catch (Exception $e) {
echo "</p>Subscribe failed (as expected)</p>";
}
//Unsubscribe
try {
$smartmessages->unsubscribe($user, $testlistid);
echo "<p>Unsubscribed OK</p>";
$smartmessages->unsubscribe($user, $testlistid); //This will fail as it's already unsubscribed
echo "<p>Unsubscribed OK</p>";
} catch (Exception $e) {
echo "</p>Unsubscribe failed (as expected)</p>";
}
//Get unsubscribes
$unsubs = $smartmessages->getListUnsubs($testlistid);
echo "<p>Unsubscribes:</p>\n<ul>\n";
foreach ($unsubs as $unsub) {
echo "<li>{$unsub['address']}</li>\n";
}
echo "</ul>\n";
//Get spam reporters
$spamreporters = $smartmessages->getSpamReporters();
echo "<p>Spam reporters:</p>\n<ul>\n";
foreach ($spamreporters as $spamreporter) {
echo "<li>$spamreporter</li>\n";
}
echo "</ul>\n";
//Get/Set user info
$smartmessages->getUserInfo($user);
$smartmessages->setUserInfo(
$user,
array(
'firstname' => 'Joe',
'lastname' => 'User',
'country' => 'FR',
'jobtitle' => 'Chief Techie Lurker',
'custom10' => date('Y-m-d H:i:s'),
'custom3' => '1 2 3'
)
);
$smartmessages->getUserInfo($user);
//Get/Set import order
$fo = $smartmessages->getFieldOrder();
$smartmessages->setFieldOrder(array('emailaddress', 'firstname', 'lastname', 'custom1'));
$nfo = $smartmessages->getFieldOrder();
echo "<p>Field order:</p>\n<ul>\n";
foreach ($nfo as $f) {
echo "<li>$f</li>\n";
}
echo "</ul>\n";
$smartmessages->setFieldOrder($fo); //Reset to previous order
//Get info about pending list uploads for this list
$uploads = $smartmessages->getUploads($testlistid);
echo "<p>Uploads:</p>\n<ul>\n";
foreach ($uploads as $upload) {
echo "<li>" . var_dump($upload) . "</li>\n";
}
echo "</ul>\n";
//Upload a mailing list
$uploadid = $smartmessages->uploadList($testlistid, 'testlist.csv', 'API upload', true, true, true);
if ((integer)$uploadid > 0) {
echo "Uploaded list OK<br />";
//Get upload info
echo "<p>Upload info:</p>\n";
$uploadinfo = $smartmessages->getUploadInfo($testlistid, (integer)$uploadid);
var_dump($uploadinfo);
} else {
echo "List upload failed<br />";
}
//Get the complete contents of a mailing list as a CSV
$list = $smartmessages->getList($testlistid, true);
var_dump($list);
//Get unsubscribes
$unsubs = $smartmessages->getListUnsubs($testlistid);
echo "<p>Unsubscribes:</p>\n<ul>\n";
foreach ($unsubs as $unsub) {
echo "<li>{$unsub['address']}</li>\n";
}
echo "</ul>\n";
//Remove all subscribers from a list
//Disabled in this example to avoid accidental data loss
//$c = $sm->emptyList($testlistid);
//echo "Emptied list, deleted $c subscriptions<br />";
//Send a mailshot
$mailshotid = 0;
//This is commented out in this example code as it's dangerous to call it without being sure of your parameters
//- this single call could cause hundreds of thousands of messages to be sent!
/*
$mailshotid = $sm->sendMailshot(
$templates[0],
$testlistid,
'API Send',
$campaigns[0],
'API test subject',
'API Sender',
'now',
false,
array()
);
*/
//Get data relating to a mailshot as CSVs
if ($mailshotid > 0) {
$bounces = $smartmessages->getMailshotBounces($mailshotid, true);
var_dump($bounces);
$opens = $smartmessages->getMailshotOpens($mailshotid, true);
var_dump($opens);
$clicks = $smartmessages->getMailshotClicks($mailshotid, true);
var_dump($clicks);
$unsubs = $smartmessages->getMailshotUnsubs($mailshotid, true);
var_dump($unsubs);
}
//Log out
$smartmessages->logout();
} catch (Smartmessages\Exception $e) {
echo '<h1>Exception caught</h1><p>An error (' . $e->getCode() . ') occurred: ' . $e->getMessage() . "</p>\n";
}