Skip to content

Commit

Permalink
Add ability to define category of mail
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Aug 23, 2023
1 parent 2edc6e9 commit 7de5a9c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/MailtrapTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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') {
Expand Down Expand Up @@ -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;
}

Expand Down
92 changes: 90 additions & 2 deletions tests/MailtrapTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]')
->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' => '[email protected]',
'name' => 'Manage It',
], $payload['json']['from']);
$this->assertEquals([
'email' => '[email protected]',
], $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
Expand All @@ -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 */
Expand Down Expand Up @@ -63,6 +138,7 @@ public function testCanSendHtmlEmail()
], $payload['json']['to'][0]);
$this->assertEquals('Test email', $payload['json']['subject']);
$this->assertEquals('<h1>Hello World</h1>', $payload['json']['html']);
$this->assertArrayNotHasKey('text', $payload['json']);
}

public function testCanSendMailable()
Expand All @@ -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 */
Expand Down

0 comments on commit 7de5a9c

Please sign in to comment.