mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2025-04-06 14:43:34 +03:00
Compare commits
9 commits
Author | SHA1 | Date | |
---|---|---|---|
|
230b2ff67b | ||
|
b27bab7b9f | ||
|
5975310f0e | ||
|
aeb82f7ac1 | ||
|
58c03ac34b | ||
|
14346359f1 | ||
|
c086ea8b6f | ||
|
8390bdd803 | ||
|
16d0a04014 |
5 changed files with 92 additions and 11 deletions
|
@ -93,7 +93,7 @@ use Mailgun\Hydrator\ArrayHydrator;
|
||||||
$configurator = new HttpClientConfigurator();
|
$configurator = new HttpClientConfigurator();
|
||||||
$configurator->setApiKey('key-example');
|
$configurator->setApiKey('key-example');
|
||||||
|
|
||||||
$mg = Mailgun::configure($configurator, new ArrayHydrator());
|
$mg = new Mailgun($configurator, new ArrayHydrator());
|
||||||
$data = $mg->domains()->show('example.com');
|
$data = $mg->domains()->show('example.com');
|
||||||
|
|
||||||
foreach ($data['receiving_dns_records'] as $record) {
|
foreach ($data['receiving_dns_records'] as $record) {
|
||||||
|
@ -127,7 +127,7 @@ For example, the bin id in this URL (http://bin.mailgun.net/aecf68de) is `aecf68
|
||||||
$configurator = new HttpClientConfigurator();
|
$configurator = new HttpClientConfigurator();
|
||||||
$configurator->setEndpoint('http://bin.mailgun.net/aecf68de');
|
$configurator->setEndpoint('http://bin.mailgun.net/aecf68de');
|
||||||
$configurator->setDebug(true);
|
$configurator->setDebug(true);
|
||||||
$mg = Mailgun::configure($configurator);
|
$mg = new Mailgun($configurator);
|
||||||
|
|
||||||
# Now, compose and send your message.
|
# Now, compose and send your message.
|
||||||
$mg->messages()->send('example.com', [
|
$mg->messages()->send('example.com', [
|
||||||
|
|
|
@ -57,8 +57,11 @@ class Message extends HttpApi
|
||||||
}
|
}
|
||||||
|
|
||||||
$postDataMultipart = array_merge($this->prepareMultipartParameters($params), $postDataMultipart);
|
$postDataMultipart = array_merge($this->prepareMultipartParameters($params), $postDataMultipart);
|
||||||
$response = $this->httpPostRaw(sprintf('/v3/%s/messages', $domain), $postDataMultipart);
|
try {
|
||||||
$this->closeResources($postDataMultipart);
|
$response = $this->httpPostRaw(sprintf('/v3/%s/messages', $domain), $postDataMultipart);
|
||||||
|
} finally {
|
||||||
|
$this->closeResources($postDataMultipart);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->hydrateResponse($response, SendResponse::class);
|
return $this->hydrateResponse($response, SendResponse::class);
|
||||||
}
|
}
|
||||||
|
@ -91,8 +94,11 @@ class Message extends HttpApi
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$postDataMultipart[] = $this->prepareFile('message', $fileData);
|
$postDataMultipart[] = $this->prepareFile('message', $fileData);
|
||||||
$response = $this->httpPostRaw(sprintf('/v3/%s/messages.mime', $domain), $postDataMultipart);
|
try {
|
||||||
$this->closeResources($postDataMultipart);
|
$response = $this->httpPostRaw(sprintf('/v3/%s/messages.mime', $domain), $postDataMultipart);
|
||||||
|
} finally {
|
||||||
|
$this->closeResources($postDataMultipart);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->hydrateResponse($response, SendResponse::class);
|
return $this->hydrateResponse($response, SendResponse::class);
|
||||||
}
|
}
|
||||||
|
@ -128,12 +134,15 @@ class Message extends HttpApi
|
||||||
private function prepareFile(string $fieldName, array $filePath): array
|
private function prepareFile(string $fieldName, array $filePath): array
|
||||||
{
|
{
|
||||||
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
|
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
|
||||||
|
$deleteRequired = false;
|
||||||
|
|
||||||
if (isset($filePath['fileContent'])) {
|
if (isset($filePath['fileContent'])) {
|
||||||
// File from memory
|
// File from memory
|
||||||
$resource = fopen('php://temp', 'r+');
|
$filename = tempnam(sys_get_temp_dir(), "MAILGUN_TMP");
|
||||||
|
$resource = fopen($filename, 'r+');
|
||||||
fwrite($resource, $filePath['fileContent']);
|
fwrite($resource, $filePath['fileContent']);
|
||||||
rewind($resource);
|
rewind($resource);
|
||||||
|
$deleteRequired = true;
|
||||||
} elseif (isset($filePath['filePath'])) {
|
} elseif (isset($filePath['filePath'])) {
|
||||||
// File form path
|
// File form path
|
||||||
$path = $filePath['filePath'];
|
$path = $filePath['filePath'];
|
||||||
|
@ -152,6 +161,7 @@ class Message extends HttpApi
|
||||||
'name' => $fieldName,
|
'name' => $fieldName,
|
||||||
'content' => $resource,
|
'content' => $resource,
|
||||||
'filename' => $filename,
|
'filename' => $filename,
|
||||||
|
'deleteRequired' => $deleteRequired,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,6 +193,13 @@ class Message extends HttpApi
|
||||||
if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) {
|
if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) {
|
||||||
fclose($param['content']);
|
fclose($param['content']);
|
||||||
}
|
}
|
||||||
|
if (is_array($param)) {
|
||||||
|
$isFile = array_key_exists('filename', $param) && is_file($param['filename']);
|
||||||
|
$deleteRequired = $param['deleteRequired'] ?? false;
|
||||||
|
if ($isFile && $deleteRequired) {
|
||||||
|
unlink($param['filename']);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,17 +84,24 @@ class Unsubscribe extends HttpApi
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $domain Domain to delete unsubscribe for
|
* @param string $domain Domain to delete unsubscribe for
|
||||||
* @param string $address Unsubscribe address
|
* @param string $address Unsubscribe address
|
||||||
|
* @param string|null $tag Unsubscribe tag
|
||||||
*
|
*
|
||||||
* @return DeleteResponse
|
* @return DeleteResponse
|
||||||
*/
|
*/
|
||||||
public function delete(string $domain, string $address)
|
public function delete(string $domain, string $address, string $tag = null)
|
||||||
{
|
{
|
||||||
Assert::stringNotEmpty($domain);
|
Assert::stringNotEmpty($domain);
|
||||||
Assert::stringNotEmpty($address);
|
Assert::stringNotEmpty($address);
|
||||||
|
Assert::nullOrStringNotEmpty($tag);
|
||||||
|
|
||||||
$response = $this->httpDelete(sprintf('/v3/%s/unsubscribes/%s', $domain, $address));
|
$params = [];
|
||||||
|
if (!is_null($tag)) {
|
||||||
|
$params['tag'] = $tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $this->httpDelete(sprintf('/v3/%s/unsubscribes/%s', $domain, $address), $params);
|
||||||
|
|
||||||
return $this->hydrateResponse($response, DeleteResponse::class);
|
return $this->hydrateResponse($response, DeleteResponse::class);
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,6 +124,52 @@ class MessageTest extends TestCase
|
||||||
$api->sendMime('foo', ['mailbox@myapp.com'], $message, []);
|
$api->sendMime('foo', ['mailbox@myapp.com'], $message, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCloseResourcesOnSendRequestException()
|
||||||
|
{
|
||||||
|
$api = $this->getApiMock();
|
||||||
|
|
||||||
|
$api->expects($this->once())
|
||||||
|
->method('httpPostRaw')
|
||||||
|
->willThrowException(new \Exception('Something went wrong'));
|
||||||
|
|
||||||
|
$streamsCount = count(get_resources('stream'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$api->send('example.com', [
|
||||||
|
'from' => 'bob@example.com',
|
||||||
|
'to' => 'sally@example.com',
|
||||||
|
'subject' => 'Test file path attachments',
|
||||||
|
'text' => 'Test',
|
||||||
|
'attachment' => [
|
||||||
|
['filePath' => __DIR__.'/../TestAssets/mailgun_icon1.png', 'filename' => 'mailgun_icon1.png'],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->assertEquals('Something went wrong', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertCount($streamsCount, get_resources('stream'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCloseResourcesOnSendMimeRequestException()
|
||||||
|
{
|
||||||
|
$api = $this->getApiMock();
|
||||||
|
|
||||||
|
$api->expects($this->once())
|
||||||
|
->method('httpPostRaw')
|
||||||
|
->willThrowException(new \Exception('Something went wrong'));
|
||||||
|
|
||||||
|
$streamsCount = count(get_resources('stream'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$api->sendMime('foo', ['mailbox@myapp.com'], 'mime message', ['o:Foo' => 'bar']);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->assertEquals('Something went wrong', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertCount($streamsCount, get_resources('stream'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -65,6 +65,17 @@ class UnsubscribeTest extends TestCase
|
||||||
$api->delete('example.com', 'foo@bar.com');
|
$api->delete('example.com', 'foo@bar.com');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDeleteWithTag()
|
||||||
|
{
|
||||||
|
$this->setRequestMethod('DELETE');
|
||||||
|
$this->setRequestUri('/v3/example.com/unsubscribes/foo@bar.com');
|
||||||
|
$this->setRequestBody(['tag' => 'tag1']);
|
||||||
|
$this->setHydrateClass(DeleteResponse::class);
|
||||||
|
|
||||||
|
$api = $this->getApiInstance();
|
||||||
|
$api->delete('example.com', 'foo@bar.com', 'tag1');
|
||||||
|
}
|
||||||
|
|
||||||
public function testDeleteAll()
|
public function testDeleteAll()
|
||||||
{
|
{
|
||||||
$this->setRequestMethod('DELETE');
|
$this->setRequestMethod('DELETE');
|
||||||
|
|
Loading…
Add table
Reference in a new issue