Skip to content

Commit dd21dd6

Browse files
Stuart Fyfenicolas-grekas
Stuart Fyfe
authored andcommittedJan 4, 2020
[Mailer] Remove line breaks in email attachment content
1 parent f48c722 commit dd21dd6

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('foo@example.com')
66+
->to('bar@example.com')
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' => 'bar@example.com']],
87+
'subject' => null,
88+
],
89+
],
90+
'from' => ['email' => 'foo@example.com'],
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)