mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-22 04:26:02 +03:00
Add attachment from memory (#158)
This commit is contained in:
parent
18804d491f
commit
5d04bc63a7
@ -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,
|
||||
];
|
||||
}
|
||||
|
50
tests/Functional/FileFromMemoryTest.php
Normal file
50
tests/Functional/FileFromMemoryTest.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Mailgun\Tests\Functional;
|
||||
|
||||
/**
|
||||
* Add attachment with file from memory
|
||||
*
|
||||
* @author Wim Verstuyf <wim.verstuyf@codelicious.be>
|
||||
*/
|
||||
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
|
||||
));
|
||||
}
|
||||
}
|
1
tests/TestAssets/text_file.txt
Normal file
1
tests/TestAssets/text_file.txt
Normal file
@ -0,0 +1 @@
|
||||
Contents of a text file
|
Loading…
Reference in New Issue
Block a user