diff --git a/src/MailtrapTransport.php b/src/MailtrapTransport.php index ee36f6e..50b9246 100644 --- a/src/MailtrapTransport.php +++ b/src/MailtrapTransport.php @@ -68,6 +68,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul */ protected function payload(Swift_Mime_SimpleMessage $message): array { + // Process addresses for mail $payload = [ 'headers' => [ 'Api-Token' => $this->token, @@ -86,6 +87,7 @@ protected function payload(Swift_Mime_SimpleMessage $message): array $payload['json']['bcc'] = $this->processAddresses($message->getBcc()); } + // Process body content and attachments if (count($message->getChildren())) { // We're dealing with a multipart message if ($message->getBodyContentType() === 'text/html') { @@ -120,6 +122,12 @@ protected function payload(Swift_Mime_SimpleMessage $message): array $payload['json']['text'] = $message->getBody(); } + // Add category if it's available + if ($message->getHeaders()->has('X-Mailtrap-Category')) { + $payload['json']['category'] = $message->getHeaders()->get('X-Mailtrap-Category')->getFieldBody(); + $message->getHeaders()->remove('X-Mailtrap-Category'); + } + return $payload; } diff --git a/tests/MailtrapTransportTest.php b/tests/MailtrapTransportTest.php index 1f8b3d9..96b6ab4 100644 --- a/tests/MailtrapTransportTest.php +++ b/tests/MailtrapTransportTest.php @@ -21,6 +21,69 @@ public function testCanGetTransport() ); } + public function testCanSendPlainEmailWithCategory() + { + // Mock client + $client = \Mockery::mock(\GuzzleHttp\ClientInterface::class); + $driver = $this->app['swift.transport']->driver('mailtrap'); + $driver->setClient($client); + + $args = []; + $client + ->shouldReceive('request') + ->once() + ->andReturnUsing(function (...$params) use (&$args) { + $args = $params; + + $body = \Mockery::mock(\Psr\Http\Message\StreamInterface::class); + $body->shouldReceive('getContents')->andReturn(json_encode([ + 'success' => true, + 'message_ids' => [ + 123 + ], + ])); + + $response = \Mockery::mock(\Psr\Http\Message\ResponseInterface::class); + $response->shouldReceive('getBody')->andReturn($body); + + return $response; + }); + + /** @var \Illuminate\Mail\Mailer */ + $mailer = $this->app['mailer']; + + // Send test email + $mailer->raw('Hello World', function ($mail) { + $mail + ->to('ben@manageit.com.au') + ->subject('Test email'); + + $mail->getHeaders()->addTextHeader( + 'X-Mailtrap-Category', + 'test' + ); + }); + + // Check arguments passed to Guzzle + $this->assertEquals('POST', $args[0]); + $this->assertEquals('https://send.api.mailtrap.io/api/send', $args[1]); + + // Check payload + $payload = $args[2]; + $this->assertEquals('foo', $payload['headers']['Api-Token']); + $this->assertEquals([ + 'email' => 'example@manageit.com.au', + 'name' => 'Manage It', + ], $payload['json']['from']); + $this->assertEquals([ + 'email' => 'ben@manageit.com.au', + ], $payload['json']['to'][0]); + $this->assertEquals('Test email', $payload['json']['subject']); + $this->assertEquals('test', $payload['json']['category']); + $this->assertEquals('Hello World', $payload['json']['text']); + $this->assertArrayNotHasKey('html', $payload['json']); + } + public function testCanSendHtmlEmail() { // Mock client @@ -34,7 +97,19 @@ public function testCanSendHtmlEmail() ->once() ->andReturnUsing(function (...$params) use (&$args) { $args = $params; - return \Mockery::mock(\Psr\Http\Message\ResponseInterface::class); + + $body = \Mockery::mock(\Psr\Http\Message\StreamInterface::class); + $body->shouldReceive('getContents')->andReturn(json_encode([ + 'success' => true, + 'message_ids' => [ + 123 + ], + ])); + + $response = \Mockery::mock(\Psr\Http\Message\ResponseInterface::class); + $response->shouldReceive('getBody')->andReturn($body); + + return $response; }); /** @var \Illuminate\Mail\Mailer */ @@ -63,6 +138,7 @@ public function testCanSendHtmlEmail() ], $payload['json']['to'][0]); $this->assertEquals('Test email', $payload['json']['subject']); $this->assertEquals('

Hello World

', $payload['json']['html']); + $this->assertArrayNotHasKey('text', $payload['json']); } public function testCanSendMailable() @@ -78,7 +154,19 @@ public function testCanSendMailable() ->once() ->andReturnUsing(function (...$params) use (&$args) { $args = $params; - return \Mockery::mock(\Psr\Http\Message\ResponseInterface::class); + + $body = \Mockery::mock(\Psr\Http\Message\StreamInterface::class); + $body->shouldReceive('getContents')->andReturn(json_encode([ + 'success' => true, + 'message_ids' => [ + 123 + ], + ])); + + $response = \Mockery::mock(\Psr\Http\Message\ResponseInterface::class); + $response->shouldReceive('getBody')->andReturn($body); + + return $response; }); /** @var \Illuminate\Mail\Mailer */