Skip to content

Commit 72b41cd

Browse files
committed
Added the dry run mode for sendMessage()
1 parent 8fdc9a0 commit 72b41cd

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

MailgunApi.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class MailgunApi
1616
private $_clicksTrackingMode;
1717
private $_enableOpensTracking;
1818

19+
private $_enableDryRun = false;
20+
1921
private $_domain;
2022
private $_key;
2123

@@ -50,6 +52,10 @@ public function newMessage()
5052
*/
5153
public function sendMessage(MailgunMessage $message)
5254
{
55+
if ($this->_enableDryRun) {
56+
return '';
57+
}
58+
5359
$response = $this->_performRequest('POST', $this->_url . $this->_domain . '/messages', $message);
5460
return $response['id'];
5561
}
@@ -107,7 +113,7 @@ public function updateMailingList($listAddress, MailgunList $mailingList)
107113
public function deleteMailingList($listAddress)
108114
{
109115
$response = $this->_performRequest('DELETE', $this->_url . 'lists/' . $listAddress);
110-
return $response['message'] == 'Mailing list has been deleted';
116+
return $response['message'] === 'Mailing list has been deleted';
111117
}
112118

113119
/**
@@ -161,7 +167,7 @@ public function addMemberToMailingList($listAddress, MailgunListMember $member,
161167
*/
162168
public function addMultipleMembersToMailingList($listAddress, $members)
163169
{
164-
if (count($members) == 0) {
170+
if (count($members) === 0) {
165171
throw new MailgunException('The members list is empty.');
166172
}
167173

@@ -206,7 +212,7 @@ public function deleteMailingListMember($listAddress, $memberAddress)
206212
{
207213
$response = $this->_performRequest('DELETE', $this->_url . 'lists/' . $listAddress
208214
. '/members/' . $memberAddress);
209-
return $response['message'] == 'Mailing list member has been deleted';
215+
return $response['message'] === 'Mailing list member has been deleted';
210216
}
211217

212218
/**
@@ -256,7 +262,7 @@ public function getUserUnsubscribes($userAddress)
256262
public function createUnsubscribe(MailgunUnsubscribe $unsubscribe)
257263
{
258264
$response = $this->_performRequest('POST', $this->_url . $this->_domain . '/unsubscribes', $unsubscribe);
259-
return $response['message'] == 'Address has been added to the unsubscribes table';
265+
return $response['message'] === 'Address has been added to the unsubscribes table';
260266
}
261267

262268
/**
@@ -266,7 +272,7 @@ public function createUnsubscribe(MailgunUnsubscribe $unsubscribe)
266272
public function deleteUnsubscribe($id)
267273
{
268274
$response = $this->_performRequest('DELETE', $this->_url . $this->_domain . '/unsubscribes/' . $id);
269-
return $response['message'] == 'Unsubscribe event has been removed';
275+
return $response['message'] === 'Unsubscribe event has been removed';
270276
}
271277

272278
/**
@@ -276,7 +282,7 @@ public function deleteUnsubscribe($id)
276282
public function deleteUserUnsubscribes($userAddress)
277283
{
278284
$response = $this->_performRequest('DELETE', $this->_url . $this->_domain . '/unsubscribes/' . $userAddress);
279-
return $response['message'] == 'Unsubscribe event has been removed';
285+
return $response['message'] === 'Unsubscribe event has been removed';
280286
}
281287

282288
/**
@@ -312,7 +318,7 @@ public function getComplaint($userAddress)
312318
public function createComplaint(MailgunComplaint $complaint)
313319
{
314320
$response = $this->_performRequest('POST', $this->_url . $this->_domain . '/complaints', $complaint);
315-
return $response['message'] == 'Address has been added to the complaints table';
321+
return $response['message'] === 'Address has been added to the complaints table';
316322
}
317323

318324
/**
@@ -322,7 +328,7 @@ public function createComplaint(MailgunComplaint $complaint)
322328
public function deleteComplaint($userAddress)
323329
{
324330
$response = $this->_performRequest('DELETE', $this->_url . $this->_domain . '/complaints/' . $userAddress);
325-
return $response['message'] == 'Spam complaint has been removed';
331+
return $response['message'] === 'Spam complaint has been removed';
326332
}
327333

328334
/**
@@ -358,7 +364,7 @@ public function getBounce($userAddress)
358364
public function createBounce(MailgunBounce $bounce)
359365
{
360366
$response = $this->_performRequest('POST', $this->_url . $this->_domain . '/bounces', $bounce);
361-
return $response['message'] == 'Address has been added to the bounces table';
367+
return $response['message'] === 'Address has been added to the bounces table';
362368
}
363369

364370
/**
@@ -368,7 +374,7 @@ public function createBounce(MailgunBounce $bounce)
368374
public function deleteBounce($userAddress)
369375
{
370376
$response = $this->_performRequest('DELETE', $this->_url . $this->_domain . '/bounces/' . $userAddress);
371-
return $response['message'] == 'Bounced address has been removed';
377+
return $response['message'] === 'Bounced address has been removed';
372378
}
373379

374380
/**
@@ -424,7 +430,7 @@ public function updateRoute($id, MailgunRoute $route)
424430
public function deleteRoute($id)
425431
{
426432
$response = $this->_performRequest('DELETE', $this->_url . 'routes/' . $id);
427-
return $response['message'] == 'Route has been deleted';
433+
return $response['message'] === 'Route has been deleted';
428434
}
429435

430436
/**
@@ -587,6 +593,28 @@ public function getIsOpensTrackingEnabled()
587593
return $this->_enableOpensTracking;
588594
}
589595

596+
/**
597+
* You can use dry run to test your code without making real API requests
598+
* Affects only sendMessage()!
599+
*/
600+
public function enableDryRun()
601+
{
602+
$this->_enableDryRun = true;
603+
}
604+
605+
public function disableDryRun()
606+
{
607+
$this->_enableDryRun = false;
608+
}
609+
610+
/**
611+
* @return bool
612+
*/
613+
public function getIsDryRunEnabled()
614+
{
615+
return $this->_enableDryRun;
616+
}
617+
590618
/**
591619
* @return resource cURL instance
592620
*/
@@ -636,7 +664,7 @@ protected function _performRequest($method, $url, MailgunObject $object = null,
636664
throw new MailgunException('Mailgun server error', $responseStatus);
637665
} else {
638666
$responseArray = json_decode($response, true);
639-
if ($responseStatus == 200) {
667+
if ($responseStatus === 200) {
640668
return $responseArray;
641669
} else {
642670
throw new MailgunException(!empty($responseArray['message']) ? $responseArray['message'] : $response,

MailgunRoute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function addStopAction()
159159
public function removeAction($action)
160160
{
161161
foreach ($this->_actions as $key => $value) {
162-
if ($value == $action) {
162+
if ($value === $action) {
163163
unset($this->_actions[$key]);
164164
}
165165
}

MailgunYii.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class MailgunYii extends CApplicationComponent
7676
public $clicksTrackingMode;
7777
public $enableOpensTracking;
7878

79+
public $enableDryRun;
80+
7981
protected $_api;
8082

8183
public function __call($name, $parameters)
@@ -114,6 +116,9 @@ public function init()
114116
if (isset($this->enableOpensTracking)) {
115117
$this->enableOpensTracking ? $this->_api->enableOpensTracking() : $this->_api->disableOpensTracking();
116118
}
119+
if (isset($this->enableDryRun)) {
120+
$this->enableDryRun ? $this->_api->enableDryRun() : $this->_api->disableDryRun();
121+
}
117122
}
118123

119124
/**

MailgunYii2.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class MailgunYii2 extends \yii\base\Component
7676
public $clicksTrackingMode;
7777
public $enableOpensTracking;
7878

79+
public $enableDryRun;
80+
7981
protected $_api;
8082

8183
public function __call($name, $parameters)
@@ -115,6 +117,9 @@ public function init()
115117
if (isset($this->enableOpensTracking)) {
116118
$this->enableOpensTracking ? $this->_api->enableOpensTracking() : $this->_api->disableOpensTracking();
117119
}
120+
if (isset($this->enableDryRun)) {
121+
$this->enableDryRun ? $this->_api->enableDryRun() : $this->_api->disableDryRun();
122+
}
118123
}
119124

120125
/**

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The library requires PHP 5.2 compiled with [cURL extension](http://www.php.net/m
1818
0. Add php-mailgun dependency:
1919
2020
```
21-
php composer.phar require baibaratsky/php-mailgun:1.1.*
21+
php composer.phar require baibaratsky/php-mailgun:1.2.*
2222
```
2323
2424
@@ -39,6 +39,9 @@ $message->addTag('test'); // All the Mailgun-specific attributes, such as tags,
3939
4040
$message->enableTestMode(); // Don’t forget to remove this string if you really want the message to be sent
4141
42+
// You can also use dry run to test your code without making real API requests (only for sendMessage())
43+
// $mailgun->enableDryRun();
44+
4245
echo $message->send();
4346
```
4447

0 commit comments

Comments
 (0)