-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailerClass.php
132 lines (116 loc) · 3.46 KB
/
MailerClass.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
<?php
class GetResponse {
private $url = 'https://api.getresponse.com/v3';
private $auth;
private $jsonDecode = false;
function __construct($auth = false) {
if($auth) {
$this->auth = $auth;
} else {
throw new Exception('{ No auth key specified. Please provide an auth key. }');
}
}
public function getCampaign($campaign = false) {
if(!$campaign) {
return $this->getResponse($this->url . '/campaigns', ['page' => 1, 'perPage' => 100]);
} else {
return $this->getResponse($this->url . '/campaigns/' . $campaign, []);
}
}
public function createCampaign($name = false) {
// Campaign name must be between 3-64 characters, only a-z (lower case), numbers and "_".
$campaignDetails = [
'name' => $name
];
if(!$name) {
return false;
} else {
return $this->getResponse($this->url . '/campaigns', $campaignDetails, 'POST');
}
}
public function getContacts($id = false, $email = false, $page = 1, $perPage = 30) {
if($id) {
$page = (!is_numeric($page)) ? 1 : $page;
$perPage = (!is_numeric($page)) ? 30 : $perPage;
if(!$email) {
return $this->getResponse($this->url . '/campaigns/' . $id . '/contacts', ['page' => $page, 'perPage' => $perPage]);
} else {
return $this->getResponse($this->url . '/campaigns/' . $id . '/contacts', ['query' => ['email' => $email]]);
}
} else {
return false;
}
}
public function subscribe($list = false, $name = false, $email = false, $ip = false) {
if($list && $name && $email && $ip) {
$postData = [
'name' => $name,
'email' => $email,
'campaign' => [
'campaignId' => $list
]
];
return $this->getResponse($this->url . '/contacts', $postData, 'POST');
} else {
return false;
}
}
public function unsubscribe($list = false, $email = false) {
if($email && $list) {
$user = $this->getContacts($list, $email);
$user = (is_object($user)) ? $user : json_decode($user);
if(count($user) > 1) {
return false;
} else {
$contactId = @$user[0]->contactId;
$this->getResponse($this->url . '/contacts/' . $contactId, [], 'DELETE');
return true;
}
} else {
return false;
}
}
public function getResponse($url, $data, $method = 'GET') {
$apiKey = $this->auth;
if($method === 'POST') {
$sp = json_encode($data);
$ux = $url;
} elseif($method == 'GET') {
$sp = http_build_query($data);
$ux = $url . '?' . $sp;
} else {
$ux = $url;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ux);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($method === 'POST'){
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $sp);
}
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
if($method == 'POST') {
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-Auth-Token: api-key ' . $apiKey,
'Content-Type: application/json',
'Content-Length: ' . strlen($sp)
]);
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-Auth-Token: api-key ' . $apiKey
]);
}
if($method == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
//debugging
curl_setopt($ch, CURLINFO_HEADER_OUT, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$rt = curl_exec($ch);
curl_close($ch);
return $rt;
}
}
?>