[Mailer] Remove line breaks in email attachment content

This commit is contained in:
Stuart Fyfe 2019-09-23 13:39:21 +01:00 committed by Nicolas Grekas
parent be6a196f7d
commit a28a7f9dee
2 changed files with 49 additions and 1 deletions

View File

@ -115,7 +115,7 @@ class SendgridTransport extends AbstractApiTransport
$disposition = $headers->getHeaderBody('Content-Disposition');
$att = [
'content' => $attachment->bodyToString(),
'content' => str_replace("\r\n", '', $attachment->bodyToString()),
'type' => $headers->get('Content-Type')->getBody(),
'filename' => $filename,
'disposition' => $disposition,

View File

@ -58,4 +58,52 @@ class SendgridTransportTest extends TestCase
$mailer->send($email);
}
public function testLineBreaksInEncodedAttachment()
{
$email = new Email();
$email->from('foo@example.com')
->to('bar@example.com')
// even if content doesn't include new lines, the base64 encoding performed later may add them
->attach('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod', 'lorem.txt');
$response = $this->createMock(ResponseInterface::class);
$response
->expects($this->once())
->method('getStatusCode')
->willReturn(202);
$httpClient = $this->createMock(HttpClientInterface::class);
$httpClient
->expects($this->once())
->method('request')
->with('POST', 'https://api.sendgrid.com/v3/mail/send', [
'json' => [
'personalizations' => [
[
'to' => [['email' => 'bar@example.com']],
'subject' => null,
],
],
'from' => ['email' => 'foo@example.com'],
'content' => [],
'attachments' => [
[
'content' => 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2Q=',
'filename' => 'lorem.txt',
'type' => 'application/octet-stream',
'disposition' => 'attachment',
],
],
],
'auth_bearer' => 'foo',
])
->willReturn($response);
$mailer = new SendgridTransport('foo', $httpClient);
$mailer->send($email);
}
}