Skip to content

Commit e175a0f

Browse files
Merge branch '4.4' into 5.0
* 4.4: (26 commits) [HttpClient] NativeHttpClient should not send >1.1 protocol version [HttpClient] fix support for non-blocking resource streams [Mailer] Make sure you can pass custom headers to Mailgun [Mailer] Remove line breaks in email attachment content Update links to documentation [Validator] Add the missing translations for the Arabic (ar) locale ensure to expect no validation for the right reasons [Security-Guard] fixed 35203 missing name tag in param docblock [HttpClient] fix casting responses to PHP streams [PhpUnitBridge] Add test case for @expectedDeprecation annotation [PhpUnitBridge][SymfonyTestsListenerTrait] Remove $testsWithWarnings stack [Mailer] Fix addresses management in Sendgrid API payload [Mailer][MailchimpBridge] Fix missing attachments when sending via Mandrill API [Mailer][MailchimpBridge] Fix incorrect sender address when sender has name [HttpClient] fix capturing SSL certificates with NativeHttpClient Update year in license files [TwigBridge][Form] Added missing help messages in form themes Update year in license files Update year in license files fix version when "anonymous: lazy" was introduced ...
2 parents ad2b0eb + 96a6c30 commit e175a0f

File tree

3 files changed

+93
-9
lines changed

3 files changed

+93
-9
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2019 Fabien Potencier
1+
Copyright (c) 2019-2020 Fabien Potencier
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

Tests/Transport/SendgridApiTransportTest.php

+80-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridApiTransport;
16+
use Symfony\Component\Mailer\Envelope;
17+
use Symfony\Component\Mime\Address;
1618
use Symfony\Component\Mime\Email;
1719
use Symfony\Contracts\HttpClient\HttpClientInterface;
1820
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -48,8 +50,8 @@ public function getTransportData()
4850
public function testSend()
4951
{
5052
$email = new Email();
51-
$email->from('[email protected]')
52-
53+
$email->from(new Address('[email protected]', 'Ms. Foo Bar'))
54+
->to(new Address('[email protected]', 'Mr. Recipient'))
5355
5456
->text('content');
5557

@@ -73,12 +75,18 @@ public function testSend()
7375
'json' => [
7476
'personalizations' => [
7577
[
76-
'to' => [['email' => '[email protected]']],
78+
'to' => [[
79+
'email' => '[email protected]',
80+
'name' => 'Mr. Recipient',
81+
]],
7782
'subject' => null,
7883
'bcc' => [['email' => '[email protected]']],
7984
],
8085
],
81-
'from' => ['email' => '[email protected]'],
86+
'from' => [
87+
'email' => '[email protected]',
88+
'name' => 'Ms. Foo Bar',
89+
],
8290
'content' => [
8391
['type' => 'text/plain', 'value' => 'content'],
8492
],
@@ -90,4 +98,72 @@ public function testSend()
9098
$mailer = new SendgridApiTransport('foo', $httpClient);
9199
$mailer->send($email);
92100
}
101+
102+
public function testLineBreaksInEncodedAttachment()
103+
{
104+
$email = new Email();
105+
$email->from('[email protected]')
106+
107+
// even if content doesn't include new lines, the base64 encoding performed later may add them
108+
->attach('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod', 'lorem.txt');
109+
110+
$response = $this->createMock(ResponseInterface::class);
111+
112+
$response
113+
->expects($this->once())
114+
->method('getStatusCode')
115+
->willReturn(202);
116+
$response
117+
->expects($this->once())
118+
->method('getHeaders')
119+
->willReturn(['x-message-id' => '1']);
120+
121+
$httpClient = $this->createMock(HttpClientInterface::class);
122+
123+
$httpClient
124+
->expects($this->once())
125+
->method('request')
126+
->with('POST', 'https://api.sendgrid.com/v3/mail/send', [
127+
'json' => [
128+
'personalizations' => [
129+
[
130+
'to' => [['email' => '[email protected]']],
131+
'subject' => null,
132+
],
133+
],
134+
'from' => ['email' => '[email protected]'],
135+
'content' => [],
136+
'attachments' => [
137+
[
138+
'content' => 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2Q=',
139+
'filename' => 'lorem.txt',
140+
'type' => 'application/octet-stream',
141+
'disposition' => 'attachment',
142+
],
143+
],
144+
],
145+
'auth_bearer' => 'foo',
146+
])
147+
->willReturn($response);
148+
149+
$mailer = new SendgridApiTransport('foo', $httpClient);
150+
151+
$mailer->send($email);
152+
}
153+
154+
public function testCustomHeader()
155+
{
156+
$email = new Email();
157+
$email->getHeaders()->addTextHeader('foo', 'bar');
158+
$envelope = new Envelope(new Address('[email protected]'), [new Address('[email protected]')]);
159+
160+
$transport = new SendgridApiTransport('ACCESS_KEY');
161+
$method = new \ReflectionMethod(SendgridApiTransport::class, 'getPayload');
162+
$method->setAccessible(true);
163+
$payload = $method->invoke($transport, $email, $envelope);
164+
165+
$this->assertArrayHasKey('headers', $payload);
166+
$this->assertArrayHasKey('foo', $payload['headers']);
167+
$this->assertEquals('bar', $payload['headers']['foo']);
168+
}
93169
}

Transport/SendgridApiTransport.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,19 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6363

6464
private function getPayload(Email $email, Envelope $envelope): array
6565
{
66-
$addressStringifier = function (Address $address) {return ['email' => $address->toString()]; };
66+
$addressStringifier = function (Address $address) {
67+
$stringified = ['email' => $address->getAddress()];
68+
69+
if ($address->getName()) {
70+
$stringified['name'] = $address->getName();
71+
}
72+
73+
return $stringified;
74+
};
6775

6876
$payload = [
6977
'personalizations' => [],
70-
'from' => ['email' => $envelope->getSender()->toString()],
78+
'from' => $addressStringifier($envelope->getSender()),
7179
'content' => $this->getContent($email),
7280
];
7381

@@ -96,7 +104,7 @@ private function getPayload(Email $email, Envelope $envelope): array
96104
continue;
97105
}
98106

99-
$payload['headers'][$name] = $header->toString();
107+
$payload['headers'][$name] = $header->getBodyAsString();
100108
}
101109

102110
return $payload;
@@ -124,7 +132,7 @@ private function getAttachments(Email $email): array
124132
$disposition = $headers->getHeaderBody('Content-Disposition');
125133

126134
$att = [
127-
'content' => $attachment->bodyToString(),
135+
'content' => str_replace("\r\n", '', $attachment->bodyToString()),
128136
'type' => $headers->get('Content-Type')->getBody(),
129137
'filename' => $filename,
130138
'disposition' => $disposition,

0 commit comments

Comments
 (0)