Skip to content

[Mailer] Remove line breaks in email attachment content with Sendgrid API #33672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2020
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);
}
}