From af53fd50ff8879e13c9abc0373f990641e92edcf Mon Sep 17 00:00:00 2001 From: Travis Swientek Date: Mon, 13 Jan 2014 22:53:34 +0000 Subject: [PATCH 1/2] Ability to rename attachments and inline images. --- CHANGELOG.md | 10 +++++++ README.md | 2 +- composer.json | 2 +- src/Mailgun/Connection/RestClient.php | 22 ++++++++++++-- src/Mailgun/Messages/MessageBuilder.php | 21 ++++++++----- .../Tests/Messages/MessageBuilderTest.php | 30 +++++++++++++++++-- 6 files changed, 72 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11b043e..f1a20be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +## 1.6 (2014-1-13) + +Enhancement: + - adjust file attachment/inline name (#21 @travelton) + +## 1.5 (2013-12-13) + +Enhancement: + - added ability to define non-https endpoint for debugging purposes (#23 @travelton) + ## 1.4 (2013-10-16) Bugfixes: diff --git a/README.md b/README.md index dc31d17..77d5338 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ composer and the Mailgun SDK. curl -sS https://getcomposer.org/installer | php # Add Mailgun as a dependency -php composer.phar require mailgun/mailgun-php:~1.5 +php composer.phar require mailgun/mailgun-php:~1.6 ``` **For shared hosts without SSH access, check out our [Shared Host Instructions](SharedHostInstall.md).** diff --git a/composer.json b/composer.json index 8eac3e3..bdb67a3 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "mailgun/mailgun-php", "description": "The Mailgun SDK provides methods for all API functions.", "require": { - "guzzle/guzzle": "3.7.*" + "guzzle/guzzle": "3.8.*" }, "require-dev": { "phpunit/phpunit": "3.7.*" diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 65cf7b9..95a0d6e 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -39,12 +39,28 @@ class RestClient{ } if(isset($files["attachment"])){ foreach($files["attachment"] as $attachment){ - $request->addPostFile("attachment", $attachment); + // Backward compatibility code + if (is_array($attachment)){ + $request->addPostFile("attachment", + $attachment['filePath'], null, + $attachment['remoteName']); + } + else{ + $request->addPostFile("attachment", $attachment); + } } } if(isset($files["inline"])){ - foreach($files["inline"] as $attachment){ - $request->addPostFile("inline", $attachment); + foreach($files["inline"] as $inline){ + // Backward compatibility code + if (is_array($inline)){ + $request->addPostFile("inline", + $inline['filePath'], null, + $inline['remoteName']); + } + else{ + $request->addPostFile("inline", $inline); + } } } diff --git a/src/Mailgun/Messages/MessageBuilder.php b/src/Mailgun/Messages/MessageBuilder.php index 6fa5a0d..62fbfed 100644 --- a/src/Mailgun/Messages/MessageBuilder.php +++ b/src/Mailgun/Messages/MessageBuilder.php @@ -136,13 +136,16 @@ class MessageBuilder{ return $this->message['html']; } - public function addAttachment($attachmentPath){ + public function addAttachment($attachmentPath, $attachmentName = null){ if(preg_match("/^@/", $attachmentPath)){ if(isset($this->files["attachment"])){ - array_push($this->files["attachment"], $attachmentPath); + $attachment = array('filePath' => $attachmentPath, + 'remoteName' => $attachmentName); + array_push($this->files["attachment"], $attachment); } else{ - $this->files["attachment"] = array($attachmentPath); + $this->files["attachment"] = array(array('filePath' => $attachmentPath, + 'remoteName' => $attachmentName)); } return true; } @@ -151,16 +154,18 @@ class MessageBuilder{ } } - public function addInlineImage($inlineImagePath){ + public function addInlineImage($inlineImagePath, $inlineImageName = null){ if(preg_match("/^@/", $inlineImagePath)){ if(isset($this->files['inline'])){ - array_push($this->files['inline'] , $inlineImagePath); - return true; + $inlineAttachment = array('filePath' => $inlineImagePath, + 'remoteName' => $inlineImageName); + array_push($this->files['inline'] , $inlineAttachment); } else{ - $this->files['inline'] = array($inlineImagePath); - return true; + $this->files['inline'] = array(array('filePath' => $inlineImagePath, + 'remoteName' => $inlineImageName)); } + return true; } else{ throw new InvalidParameter(INVALID_PARAMETER_INLINE); diff --git a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php index 2c593ee..18e89fe 100644 --- a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php +++ b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php @@ -117,14 +117,40 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase{ $message->addAttachment("@../TestAssets/mailgun_icon.png"); $message->addAttachment("@../TestAssets/rackspace_logo.png"); $messageObj = $message->getFiles(); - $this->assertEquals(array("attachment" => array("@../TestAssets/mailgun_icon.png", "@../TestAssets/rackspace_logo.png")), $messageObj); + $this->assertEquals(array(array('filePath' => "@../TestAssets/mailgun_icon.png", + 'remoteName' => null), + array('filePath' => "@../TestAssets/rackspace_logo.png", + 'remoteName' => null)), $messageObj["attachment"]); } public function testAddInlineImages(){ $message = $this->client->MessageBuilder(); $message->addInlineImage("@../TestAssets/mailgun_icon.png"); $message->addInlineImage("@../TestAssets/rackspace_logo.png"); $messageObj = $message->getFiles(); - $this->assertEquals(array("inline" => array("@../TestAssets/mailgun_icon.png", "@../TestAssets/rackspace_logo.png")), $messageObj); + $this->assertEquals(array(array('filePath' => "@../TestAssets/mailgun_icon.png", + 'remoteName' => null), + array('filePath' => "@../TestAssets/rackspace_logo.png", + 'remoteName' => null)), $messageObj['inline']); + } + public function testAddAttachmentsPostName(){ + $message = $this->client->MessageBuilder(); + $message->addAttachment('@../TestAssets/mailgun_icon.png', 'mg_icon.png'); + $message->addAttachment('@../TestAssets/rackspace_logo.png', 'rs_logo.png'); + $messageObj = $message->getFiles(); + $this->assertEquals(array(array('filePath' => '@../TestAssets/mailgun_icon.png', + 'remoteName' => 'mg_icon.png'), + array('filePath' => '@../TestAssets/rackspace_logo.png', + 'remoteName' => 'rs_logo.png')), $messageObj["attachment"]); + } + public function testAddInlineImagePostName(){ + $message = $this->client->MessageBuilder(); + $message->addInlineImage('@../TestAssets/mailgun_icon.png', 'mg_icon.png'); + $message->addInlineImage('@../TestAssets/rackspace_logo.png', 'rs_logo.png'); + $messageObj = $message->getFiles(); + $this->assertEquals(array(array('filePath' => '@../TestAssets/mailgun_icon.png', + 'remoteName' => 'mg_icon.png'), + array('filePath' => '@../TestAssets/rackspace_logo.png', + 'remoteName' => 'rs_logo.png')), $messageObj['inline']); } public function testsetTestMode(){ $message = $this->client->MessageBuilder(); From d3a0d6aed90ce03e9a0ff2c8bc0a82ceab3002db Mon Sep 17 00:00:00 2001 From: Travis Swientek Date: Tue, 14 Jan 2014 00:43:29 +0000 Subject: [PATCH 2/2] Fixed Guzzle post/put field aggregation. --- CHANGELOG.md | 3 +++ src/Mailgun/Connection/RestClient.php | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1a20be..8c9afc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ Enhancement: - adjust file attachment/inline name (#21 @travelton) +Bugfixes: + - fixed issue with unordered route actions (#23 @travelton) + ## 1.5 (2013-12-13) Enhancement: diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 95a0d6e..d32a92e 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -6,6 +6,7 @@ use Guzzle\Http\Client as Guzzle; use Mailgun\MailgunClient; use Mailgun\Connection\Exceptions\GenericHTTPError; +use Guzzle\Http\QueryAggregator\DuplicateAggregator; use Mailgun\Connection\Exceptions\InvalidCredentials; use Mailgun\Connection\Exceptions\NoDomainsConfigured; use Mailgun\Connection\Exceptions\MissingRequiredParameters; @@ -63,7 +64,8 @@ class RestClient{ } } } - + + $request->getPostFields()->setAggregator(new DuplicateAggregator()); $response = $request->send(); return $this->responseHandler($response); } @@ -87,6 +89,7 @@ class RestClient{ public function put($endpointUrl, $putData){ $request = $this->mgClient->put($endpointUrl, array(), $putData); + $request->getPostFields()->setAggregator(new DuplicateAggregator()); $response = $request->send(); return $this->responseHandler($response); }