-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailgun.php
266 lines (234 loc) · 7.85 KB
/
Mailgun.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
<?php
/**
* Library for easy communication with MailGun REST API.
*
* @package Mailgun
* @author MailGun Inc
* @version 1.0
*/
/**
* Mailgun uses patched ActiveResource.php.
* Modifications:
* 1) Throw error in case when server returns not 2xx response.
* 2) Check content-type and parse only text/xml response
*/
require_once 'ActiveResource.php';
/**
* Initialize the ibrary
*
* @param string $api_key Your API key
* @param string $api_url API base URL. Must end with backslash. Default is http://maligunhq.com/api/
*/
function mailgun_init($api_key, $api_url = "https://mailgun.net/api/") {
global $_mailgun_api_url, $_maigun_api_key;
if ($api_url[strlen($api_url)-1] != "/")
$api_url .= '/';
$_mailgun_api_url = $api_url;
$_maigun_api_key = $api_key;
}
/**
* MailgunMessage class: sends messages via Mailgun HTTP gateway
*
* @package Mailgun
*/
define("MAILGUN_TAG", "X-Mailgun-Tag");
class MailgunMessage {
/**
* Send plain-text message
*
* @param string $sender sender specification
* @param string $recipients comma- or semicolon-separated list of recipients specifications.
* @param string $subject message subject
* @param string $text message text
* @param string $servername sending server (can be empty, use 'best' server)
* @param string $options JSON dictionary with objects, array("headers" => array(MAILGUN_TAG => "bounce"))
*/
static function send_text($sender, $recipients, $subject, $text, $servername="", $options = NULL) {
$curl = _mailgun_init_curl("messages.txt");
$params = 'sender='.urlencode($sender).'&recipients='.urlencode($recipients);
$params .= '&subject='.urlencode($subject).'&servername='.$servername;
$params .= '&body='.urlencode($text);
if($options != NULL) {
$params .= '&options='.urlencode(json_encode($options));
}
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HEADER, false);
_mailgun_exec_curl($curl, "Send text message failed");
}
/**
* Send MIME-formatted message, as it is
*
* @param string $sender sender specification. Mailgun will not add it to message "From" header.
* @param string $recipients comma- or semicolon-separated list of recipients specifications.
* @param string $raw_body valid MIME message.
*/
static function send_raw($sender, $recipients, $raw_body, $servername="") {
$curl = _mailgun_init_curl("messages.eml");
$params = '&servername='.urlencode($servername);
$req = $sender."\n".$recipients."\n\n".$raw_body;
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $req);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/plain"));
_mailgun_exec_curl($curl, "Send raw message failed");
}
}
/**
*
* Base class for Mailgun Resources
*
* @package Mailgun
* @author MailGun Inc.
*/
class MailgunResource extends ActiveResource {
var $user = 'api_key';
var $site = 'http://mailgun.net/api/';
var $password = 'my-api-key';
var $request_format = 'xml';
function __construct($data = array ()) {
global $_mailgun_api_url, $_maigun_api_key;
parent::__construct($data);
$this->site = $_mailgun_api_url;
$this->password = $_maigun_api_key;
}
/**
Get ID of REST resource. Same as $resource->id
*/
function id() {
return $this->id;
}
/**
* Create new resource or update it if resource already exist.
*
* There are 2 differences between upsert() and save().
* Upsert does not throw exception if object already exist.
* Upsert does not load id of the object.
*
* It ensures that resource exists on the server and does not syncronize client object instance.
* In order to modify "upserted" object, you need to find() it first.
* Example: <br>
* <code>
* Route route = new Route('*@example.com', 'http://example.com/reply');
* route->upsert();
* </code>
*/
function upsert() {
$this->post("upsert", $this->_data);
}
}
/**
* A Route has 2 properties: pattern and destination.
*
* There are 4 types of patterns:
*
* 1 '*' - match all
* 2 exact string match ([email protected])
* 3 a domain pattern, i.e. a string like "*@example.com" - matches all emails going to example.com
* 4 a regular expression
*
* Destination can be: <br>
* 1 An email address.
* 2 HTTP/HTTPS URL. A message will be HTTP POSTed there.
*
* @package Mailgun
*/
class Route extends MailgunResource {
/**
*
* @param string pattern The pattern for matching the recipient
*
*/
function __construct($pattern = "", $destination = "") {
parent::__construct(array('pattern' => $pattern, 'destination' => $destination));
}
/**
Get route pattern. Same as $route->pattern
*/
function pattern() {
return $this->pattern;
}
/**
Get route destination. Same as $route->destination
*/
function detination() {
return $this->destination;
}
}
/**
* All mail arriving to email addresses that have mailboxes associated
* will be stored on the server and can be later accessed via IMAP or POP3
* protocols.
*
* Mailbox has several properties:
*
* ^ ^
* | |
* user domain
*
* and a password
*
* user and domain can not be changed for an existing mailbox.
* @package Mailgun
*/
class Mailbox extends MailgunResource {
/**
*
* @param string user
* @param string domain
* @param string password
*
*/
function __construct($user = "", $domain = "", $password = "") {
parent::__construct(array('user' => $user, 'domain' => $domain, 'password' => $password));
}
/**
* Upsert mailboxes contained in a csv string,
* @param string $mailboxes CSV like string with mailboxes, like that:
* [email protected], password
* [email protected], password2
*/
static function upsert_from_csv($mailboxes) {
$curl = _mailgun_init_curl("mailboxes.txt");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $mailboxes);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/plain"));
_mailgun_exec_curl($curl, "Upsert from csv failed");
}
}
// Init curl with common parameters
function _mailgun_init_curl($suffix){
global $_mailgun_api_url, $_maigun_api_key;
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $_mailgun_api_url."".$suffix);
curl_setopt ($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE, 0);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_USERPWD, "api:" . $_maigun_api_key);
return $ch;
}
// Executes and close cURL session, return server response.
// Throw error if response is not 2xx.
// Error descrition will include server response.
//
// cURL must NOT return headers, or they will be added to error descrition!
//
function _mailgun_exec_curl($curl, $errmsg) {
$res = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($http_code < 200 || 299 < $http_code) {
throw new Exception($errmsg.". Server says: ".strip_tags($res));
}
return $res;
}
$_mailgun_api_url = 'https://mailgun.net/api/';
$_maigun_api_key = 'api-key-dirty-secret';
// error_reporting(E_ALL);
?>