Skip to content

Commit 4b19c6e

Browse files
bug #33672 [Mailer] Remove line breaks in email attachment content (Stuart 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 ------- a28a7f9dee [Mailer] Remove line breaks in email attachment content
2 parents 9805bc5 + dd21dd6 commit 4b19c6e

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

Http/Api/SendgridTransport.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private function getAttachments(Email $email): array
115115
$disposition = $headers->getHeaderBody('Content-Disposition');
116116

117117
$att = [
118-
'content' => $attachment->bodyToString(),
118+
'content' => str_replace("\r\n", '', $attachment->bodyToString()),
119119
'type' => $headers->get('Content-Type')->getBody(),
120120
'filename' => $filename,
121121
'disposition' => $disposition,

Tests/Http/Api/SendgridTransportTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,52 @@ public function testSend()
5858

5959
$mailer->send($email);
6060
}
61+
62+
public function testLineBreaksInEncodedAttachment()
63+
{
64+
$email = new Email();
65+
$email->from('[email protected]')
66+
67+
// even if content doesn't include new lines, the base64 encoding performed later may add them
68+
->attach('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod', 'lorem.txt');
69+
70+
$response = $this->createMock(ResponseInterface::class);
71+
72+
$response
73+
->expects($this->once())
74+
->method('getStatusCode')
75+
->willReturn(202);
76+
77+
$httpClient = $this->createMock(HttpClientInterface::class);
78+
79+
$httpClient
80+
->expects($this->once())
81+
->method('request')
82+
->with('POST', 'https://api.sendgrid.com/v3/mail/send', [
83+
'json' => [
84+
'personalizations' => [
85+
[
86+
'to' => [['email' => '[email protected]']],
87+
'subject' => null,
88+
],
89+
],
90+
'from' => ['email' => '[email protected]'],
91+
'content' => [],
92+
'attachments' => [
93+
[
94+
'content' => 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2Q=',
95+
'filename' => 'lorem.txt',
96+
'type' => 'application/octet-stream',
97+
'disposition' => 'attachment',
98+
],
99+
],
100+
],
101+
'auth_bearer' => 'foo',
102+
])
103+
->willReturn($response);
104+
105+
$mailer = new SendgridTransport('foo', $httpClient);
106+
107+
$mailer->send($email);
108+
}
61109
}

0 commit comments

Comments
 (0)