diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 849b981..0a67bbb 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -261,15 +261,27 @@ class RestClient protected function prepareFile($fieldName, $filePath, $fileIndex=0) { $filename = null; - // Backward compatibility code - if (is_array($filePath)) { - $filename = $filePath['remoteName']; - $filePath = $filePath['filePath']; - } + $resource = null; - // Remove leading @ symbol - if (strpos($filePath, '@') === 0) { - $filePath = substr($filePath, 1); + if (is_array($filePath) && isset($filePath['fileContent'])) { + // File from memory + $filename = $filePath['filename']; + $resource = fopen('php://temp', 'r+'); + fwrite($resource, $filePath['fileContent']); + rewind($resource); + } else { + // Backward compatibility code + if (is_array($filePath) && isset($filePath['filePath'])) { + $filename = $filePath['remoteName']; + $filePath = $filePath['filePath']; + } + + // Remove leading @ symbol + if (strpos($filePath, '@') === 0) { + $filePath = substr($filePath, 1); + } + + $resource = fopen($filePath, 'r'); } // Add index for multiple file support @@ -277,7 +289,7 @@ class RestClient return [ 'name' => $fieldName, - 'contents' => fopen($filePath, 'r'), + 'contents' => $resource, 'filename' => $filename, ]; } diff --git a/tests/Functional/FileFromMemoryTest.php b/tests/Functional/FileFromMemoryTest.php new file mode 100644 index 0000000..484f2e6 --- /dev/null +++ b/tests/Functional/FileFromMemoryTest.php @@ -0,0 +1,50 @@ + + */ +class FileFromMemoryTest extends \PHPUnit_Framework_TestCase +{ + public function testAddFileFromMemory() + { + $fileValidator = function($files) { + // Get only the attachments so the properties can be converted to a format we like + $attachments = array_filter($files, function($f) { + return strpos($f['name'], 'attachment') !== false; + }); + + // Convert resources to strings + $attachments = array_map(function($f) { + return [ + 'name' => $f['name'], + 'contents' => fread($f['contents'], 50), + 'filename' => $f['filename'], + ]; + }, $attachments); + + $this->assertContains(['name'=>'attachment[0]', 'contents'=>'File content', 'filename' => 'file1.txt'], $attachments); + $this->assertContains(['name'=>'attachment[1]', 'contents'=>'File content 2', 'filename' => 'file2.txt'], $attachments); + $this->assertContains(['name'=>'attachment[2]', 'contents'=>'Contents of a text file', 'filename' => 'text_file.txt'], $attachments); + }; + + $attachments = [ + ['filename' => 'file1.txt', 'fileContent' => "File content"], + ['filename' => 'file2.txt', 'fileContent' => "File content 2"], + ['filePath' => "./tests/TestAssets/text_file.txt", 'remoteName' => 'text_file.txt'] + ]; + + $mailgun = MockedMailgun::create($this, 'POST', 'domain/messages', [], $fileValidator); + + $mailgun->sendMessage('domain', array( + 'from' => 'bob@example.com', + 'to' => 'alice@example.com', + 'subject' => 'Foo', + 'text' => 'Bar'), array( + 'attachment' => $attachments + )); + } +} diff --git a/tests/TestAssets/text_file.txt b/tests/TestAssets/text_file.txt new file mode 100644 index 0000000..b1108c6 --- /dev/null +++ b/tests/TestAssets/text_file.txt @@ -0,0 +1 @@ +Contents of a text file \ No newline at end of file