From af53fd50ff8879e13c9abc0373f990641e92edcf Mon Sep 17 00:00:00 2001 From: Travis Swientek Date: Mon, 13 Jan 2014 22:53:34 +0000 Subject: [PATCH] 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();