This repository has been archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
/
createAndScheduleCampaign.php
250 lines (215 loc) · 9.5 KB
/
createAndScheduleCampaign.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
<!DOCTYPE HTML>
<html>
<head>
<title>Constant Contact API v2 Create/Schedule Campaign Example</title>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
</head>
<!--
README: Create and schedule an email marketing campaign for delivery
This example flow illustrates how to create a new email campaign, and schedule it to be sent to the selected contact lists(s).
In order for this example to function properly, you must have a valid Constant Contact API Key as well as an access token. Both of these
can be obtained from http://constantcontact.mashery.com.
NOTE: This example expects that your account already has a physical address set up in your Constant Contact account settings.
For more information on this, please visit: http://support2.constantcontact.com/articles/FAQ/2801#future%20emails
-->
<?php
// require the autoloaders
require_once '../src/Ctct/autoload.php';
require_once '../vendor/autoload.php';
use Ctct\Components\EmailMarketing\Campaign;
use Ctct\Components\EmailMarketing\Schedule;
use Ctct\ConstantContact;
use Ctct\Exceptions\CtctException;
// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "ENTER YOUR API KEY");
define("ACCESS_TOKEN", "ENTER YOUR ACCESS TOKEN");
$cc = new ConstantContact(APIKEY);
$date = date('Y-m-d\TH:i:s\.000\Z', strtotime("+1 month"));
/**
* Create an email campaign with the parameters provided
* @param array $params associative array of parameters to create a campaign from
* @return Campaign updated by server
*/
function createCampaign(array $params = array()) {
$cc = new ConstantContact(APIKEY);
$campaign = new Campaign();
$campaign->name = $params['name'];
$campaign->subject = $params['subject'];
$campaign->from_name = $params['from_name'];
$campaign->from_email = $params['from_addr'];
$campaign->greeting_string = $params['greeting_string'];
$campaign->reply_to_email = $params['reply_to_addr'];
$campaign->text_content = $params['text_content'];
$campaign->email_content = $params['email_content'];
$campaign->email_content_format = $params['format'];
// add the selected list or lists to the campaign
if (isset($params['lists'])) {
if (count($params['lists']) > 1) {
foreach ($params['lists'] as $list) {
$campaign->addList($list);
}
} else {
$campaign->addList($params['lists'][0]);
}
}
return $cc->emailMarketingService->addCampaign(ACCESS_TOKEN, $campaign);
}
/**
* Create a schedule for a campaign - this is time the campaign will be sent out
* @param $campaignId - Id of the campaign to be scheduled
* @param $time - ISO 8601 formatted timestamp of when the campaign should be sent
* @return Schedule updated by server
*/
function createSchedule($campaignId, $time) {
$cc = new ConstantContact(APIKEY);
$schedule = new Schedule();
$schedule->scheduled_date = $time;
return $cc->campaignScheduleService->addSchedule(ACCESS_TOKEN, $campaignId, $schedule);
}
// check to see if the form was submitted
if (isset($_POST['name'])) {
// attempt to create a campaign with the fields submitted, displaying any errors that occur
try {
$campaign = createCampaign($_POST);
} catch (CtctException $ex) {
echo '<span class="label label-important">Error Creating Campaign</span>';
echo '<div class="container alert-error"><pre class="failure-pre">';
print_r($ex->getErrors());
echo '</pre></div>';
die();
}
// attempt to schedule a campaign with the fields submitted, displaying any errors that occur
try {
$schedule = createSchedule($campaign->id, $_POST['schedule_time']);
} catch (CtctException $ex) {
echo '<span class="label label-important">Error Scheduling Campaign</span>';
echo '<div class="container alert-error"><pre class="failure-pre">';
print_r($ex->getErrors());
echo '</pre></div>';
die();
}
}
// attempt to get the lists in this account, displaying any errors that occur
try {
$lists = $cc->listService->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
echo '<div class="container alert-error"><pre class="failure-pre">';
print_r($ex->getErrors());
echo '</pre></div>';
die();
}
?>
<body>
<div class="well">
<h3>Create and Schedule a Campaign</h3>
<form class="form-horizontal" name="emailForm" id="emailForm" method="POST" action="createAndScheduleCampaign.php">
<div class="span6">
<fieldset>
<div class="control-group">
<label class="control-label" for="name">Campaign Name</label>
<div class="controls">
<input type="text" id="name" name="name" class="required" placeholder="Campaign Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="subject">Subject</label>
<div class="controls">
<input type="text" id="subject" name="subject" placeholder="Subject">
</div>
</div>
<div class="control-group">
<label class="control-label" for="from_name">From Name</label>
<div class="controls">
<input type="text" id="from_name" name="from_name" placeholder="From Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="from_addr">From Email</label>
<div class="controls">
<input type="email" id="from_addr" name="from_addr" placeholder="From Email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="name">Text Content</label>
<div class="controls">
<textarea id="text_content" name="text_content" placeholder="Text Content"></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="email_content">Email Content</label>
<div class="controls">
<textarea id="email_content" name="email_content" placeholder="Email Content"></textarea>
</div>
</div>
</fieldset>
</div>
<div class="span6">
<fieldset>
<div class="control-group">
<label class="control-label" for="greeting_string">Greeting String</label>
<div class="controls">
<input type="text" id="greeting_string" name="greeting_string" placeholder="Greeting String">
</div>
</div>
<div class="control-group">
<label class="control-label" for="reply_to_addr">Reply-To Email</label>
<div class="controls">
<input type="email" id="reply_to_addr" name="reply_to_addr" placeholder="Reply To">
</div>
</div>
<div class="control-group">
<label class="control-label" for="format">Lists to send to: </label>
<div class="controls">
<select multiple="multiple" name="lists[]" size="8">
<?php
foreach ($lists as $list) {
echo '<option value="' . $list->id . '" >' . $list->name . '</option><br />';
}
?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="format">Send Time</label>
<div class="controls">
<input type="text" name="schedule_time" id="schedule_time"
value="<?php echo date('Y-m-d\TH:i:s\.000\Z', strtotime("+1 month"));; ?>"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="format">Email Content Format</label>
<div class="controls">
<input type="radio" id="name" name="format" value="HTML" checked> HTML
<input type="radio" id="name" name="format" value="XHTML"> XHTML
</div>
</div>
</fieldset>
</div>
<br clear="all"/>
<div class="control-group">
<label class="control-label">
<div class="controls">
<input type="submit" value="Create & Schedule" class="btn btn-primary"/>
</div>
</div>
</form>
</div>
<?php
// print the contents of the campaign to screen
if (isset($campaign)) {
echo '<span class="label label-success">Campaign Created!</span>';
echo '<div class="container alert-success"><pre class="success-pre">';
print_r($campaign);
echo '</pre></div>';
}
// print the contents of the schedule to screen
if (isset($schedule)) {
echo '<span class="label label-success">Campaign Scheduled!</span>';
echo '<div class="container alert-success"><pre class="success-pre">';
print_r($schedule);
echo '</pre></div>';
}
?>
</body>
</html>