-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Increased upload size limit to 10M - Added option for static attachments - Added base64_decode filter to twig to allow sending base64 encoded attachments, useful for binary contents
- Loading branch information
1 parent
6d7a2ad
commit 79351cf
Showing
21 changed files
with
244 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Outstack/Enveloper/Resolution/Twig/TwigEnveloperExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Outstack\Enveloper\Resolution\Twig; | ||
|
||
class TwigEnveloperExtension extends \Twig_Extension | ||
{ | ||
public function getFilters() | ||
{ | ||
return [ | ||
'base64_decode' => new \Twig_SimpleFilter('base64_decode', 'base64_decode') | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace Outstack\Enveloper\Tests\Functional; | ||
|
||
use Outstack\Enveloper\SwiftMailerBridge\SwiftMailerInterface; | ||
use Http\Client\Exception\HttpException; | ||
use Outstack\Components\SymfonySwiftMailerAssertionLibrary\SwiftMailerAssertionTrait; | ||
use Zend\Diactoros\Request; | ||
use Zend\Diactoros\Stream; | ||
use Zend\Diactoros\Uri; | ||
|
||
class AttachmentHandlingFunctionalTest extends AbstractApiTestCase | ||
{ | ||
use SwiftMailerAssertionTrait; | ||
|
||
protected $mailerSpy; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->mailerSpy = self::$kernel->getContainer()->get(SwiftMailerInterface::class); | ||
} | ||
|
||
public function test_attachments_sent() | ||
{ | ||
$request = new Request( | ||
'/outbox', | ||
'POST', | ||
$this->convertToStream(json_encode([ | ||
'template' => 'message-with-attachments', | ||
'parameters' => [ | ||
'email' => '[email protected]', | ||
'attachments' => [ | ||
['contents' => base64_encode('This is a note'), 'filename' => 'note.txt'] | ||
] | ||
] | ||
])) | ||
); | ||
|
||
$response = $this->client->sendRequest($request); | ||
$this->assertEquals(204, $response->getStatusCode()); | ||
|
||
$this->assertCountSentMessages(1); | ||
$this->assertMessageSent( | ||
function(\Swift_Message $message) { | ||
$expectedContents = base64_encode('This is a note'); | ||
$expected = | ||
'Content-Type: application/octet-stream; name=note.txt' . "\r\n" . | ||
'Content-Transfer-Encoding: base64' . "\r\n" . | ||
'Content-Disposition: attachment; filename=note.txt' . "\r\n" . "\r\n" . | ||
$expectedContents . "\r\n" . "\r\n" . | ||
'--' | ||
; | ||
|
||
foreach (explode($message->getBoundary(), (string) $message) as $part) { | ||
if (trim($part) == trim($expected)) { | ||
return true; | ||
} | ||
} | ||
|
||
throw new \LogicException("No matching attachment found"); | ||
} | ||
); | ||
} | ||
|
||
public function test_large_attachment_sent() | ||
{ | ||
$largeAttachment = random_bytes(1048576 * 7); | ||
$request = new Request( | ||
'/outbox', | ||
'POST', | ||
$this->convertToStream(json_encode([ | ||
'template' => 'message-with-attachments', | ||
'parameters' => [ | ||
'email' => '[email protected]', | ||
'attachments' => [ | ||
['contents' => base64_encode($largeAttachment), 'filename' => 'random.txt'] | ||
] | ||
] | ||
])) | ||
); | ||
|
||
$response = $this->client->sendRequest($request); | ||
|
||
$this->assertEquals(204, $response->getStatusCode()); | ||
|
||
$this->assertCountSentMessages(1); | ||
$this->assertMessageSent( | ||
function(\Swift_Message $message) use ($largeAttachment) { | ||
$expectedContents = implode("\r\n", str_split(base64_encode($largeAttachment), 76)); | ||
$expected = | ||
'Content-Type: application/octet-stream; name=random.txt' . "\r\n" . | ||
'Content-Transfer-Encoding: base64' . "\r\n" . | ||
'Content-Disposition: attachment; filename=random.txt' . "\r\n" . "\r\n" . | ||
$expectedContents . "\r\n" . "\r\n" . | ||
'--' | ||
; | ||
|
||
foreach (explode($message->getBoundary(), (string) $message) as $part) { | ||
if (trim($part) == trim($expected)) { | ||
return true; | ||
} | ||
} | ||
|
||
throw new \LogicException("No matching attachment found"); | ||
} | ||
); | ||
} | ||
|
||
public function test_static_attachment_sent() | ||
{ | ||
$expectedAttachment = 'static attachment content'; | ||
$request = new Request( | ||
'/outbox', | ||
'POST', | ||
$this->convertToStream(json_encode([ | ||
'template' => 'message-with-static-attachments', | ||
'parameters' => [ | ||
'email' => '[email protected]' | ||
] | ||
])) | ||
); | ||
|
||
$response = $this->client->sendRequest($request); | ||
|
||
$this->assertEquals(204, $response->getStatusCode()); | ||
|
||
$this->assertCountSentMessages(1); | ||
$this->assertMessageSent( | ||
function(\Swift_Message $message) use ($expectedAttachment) { | ||
$expectedContents = implode("\r\n", str_split(base64_encode($expectedAttachment), 76)); | ||
$expected = | ||
'Content-Type: application/octet-stream; name=attachment.txt' . "\r\n" . | ||
'Content-Transfer-Encoding: base64' . "\r\n" . | ||
'Content-Disposition: attachment; filename=attachment.txt' . "\r\n" . "\r\n" . | ||
$expectedContents . "\r\n" . "\r\n" . | ||
'--' | ||
; | ||
|
||
foreach (explode($message->getBoundary(), (string) $message) as $part) { | ||
if (trim($part) == trim($expected)) { | ||
return true; | ||
} | ||
} | ||
|
||
throw new \LogicException("No matching attachment found"); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,45 +138,4 @@ function(\Swift_Message $message) { | |
); | ||
} | ||
|
||
public function test_attachments_sent() | ||
{ | ||
$request = new Request( | ||
'/outbox', | ||
'POST', | ||
$this->convertToStream(json_encode([ | ||
'template' => 'message-with-attachments', | ||
'parameters' => [ | ||
'email' => '[email protected]', | ||
'attachments' => [ | ||
['contents' => 'This is a note', 'filename' => 'note.txt'] | ||
] | ||
] | ||
])) | ||
); | ||
|
||
$response = $this->client->sendRequest($request); | ||
$this->assertEquals(204, $response->getStatusCode()); | ||
|
||
$this->assertCountSentMessages(1); | ||
$this->assertMessageSent( | ||
function(\Swift_Message $message) { | ||
$expectedContents = base64_encode('This is a note'); | ||
$expected = | ||
'Content-Type: application/octet-stream; name=note.txt' . "\r\n" . | ||
'Content-Transfer-Encoding: base64' . "\r\n" . | ||
'Content-Disposition: attachment; filename=note.txt' . "\r\n" . "\r\n" . | ||
$expectedContents . "\r\n" . "\r\n" . | ||
'--' | ||
; | ||
|
||
foreach (explode($message->getBoundary(), (string) $message) as $part) { | ||
if (trim($part) == trim($expected)) { | ||
return true; | ||
} | ||
} | ||
|
||
throw new \LogicException("No matching attachment found"); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
tests/data/templates/message-with-attachments/message-with-attachments.html.twig
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
tests/data/templates/message-with-attachments/message-with-attachments.mjml.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<mjml> | ||
<mj-body> | ||
<mj-container> | ||
<mj-text>Message with attachments</mj-text> | ||
</mj-container> | ||
</mj-body> | ||
</mjml> |
Oops, something went wrong.