Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no line-break in this content, so this is not testing your patch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof it does, line-breaks are added by the MIME-compliant base64 encoder:

return $firstLine.trim(chunk_split($encodedString, $maxLineLength, "\r\n"));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so confusing that we should add a comment before the ->attach() method. Something like:

// even it content doesn't include new lines, the Base64 encoding performed later may add them
->attach('...)


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