Skip to content

Commit

Permalink
bug #33672 [Mailer] Remove line breaks in email attachment content (S…
Browse files Browse the repository at this point in the history
…tuart Fyfe)

This PR was squashed before being merged into the 4.3 branch.

Discussion
----------

[Mailer] Remove line breaks in email attachment content

Line breaks are not allowed in attachment content when sending over the
API.

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #33671, Closes #32645
| License       | MIT
| Doc PR        |

This is a fix for #33671. Send grid's API throws a 400 error when sending email attachments with default base64 encoding.
Removing the line breaks resolves this issue.

Commits
-------

a28a7f9 [Mailer] Remove line breaks in email attachment content
  • Loading branch information
nicolas-grekas committed Jan 4, 2020
2 parents 423d3dd + a28a7f9 commit eb5171f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Expand Up @@ -115,7 +115,7 @@ private function getAttachments(Email $email): array
$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,
Expand Down
Expand Up @@ -58,4 +58,52 @@ public function testSend()

$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);
}
}

0 comments on commit eb5171f

Please sign in to comment.