Skip to content

Commit

Permalink
Merge pull request #29 from travelton/RenameFiles
Browse files Browse the repository at this point in the history
Ability to rename attachments and inline images & Duplicate Post/Put Fields
  • Loading branch information
travelton committed Jan 15, 2014
2 parents ac23bec + d3a0d6a commit cfa0212
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 16 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 1.6 (2014-1-13)

Enhancement:
- adjust file attachment/inline name (#21 @travelton)

Bugfixes:
- fixed issue with unordered route actions (#23 @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:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
Expand Down
27 changes: 23 additions & 4 deletions src/Mailgun/Connection/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -39,15 +40,32 @@ public function post($endpointUrl, $postData = array(), $files = array()){
}
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);
}
}
}


$request->getPostFields()->setAggregator(new DuplicateAggregator());
$response = $request->send();
return $this->responseHandler($response);
}
Expand All @@ -71,6 +89,7 @@ public function delete($endpointUrl){

public function put($endpointUrl, $putData){
$request = $this->mgClient->put($endpointUrl, array(), $putData);
$request->getPostFields()->setAggregator(new DuplicateAggregator());
$response = $request->send();
return $this->responseHandler($response);
}
Expand Down
21 changes: 13 additions & 8 deletions src/Mailgun/Messages/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@ public function setHtmlBody($htmlBody){
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;
}
Expand All @@ -151,16 +154,18 @@ public function addAttachment($attachmentPath){
}
}

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);
Expand Down
30 changes: 28 additions & 2 deletions tests/Mailgun/Tests/Messages/MessageBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,40 @@ public function testAddAttachments(){
$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();
Expand Down

0 comments on commit cfa0212

Please sign in to comment.