Skip to content

[Mailer] Use idn encoded address otherwise Brevo throws an error #51808

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
Oct 5, 2023
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 @@ -34,7 +34,7 @@ public function testToString(BrevoApiTransport $transport, string $expected)
$this->assertSame($expected, (string) $transport);
}

public static function getTransportData()
public static function getTransportData(): \Generator
{
yield [
new BrevoApiTransport('ACCESS_KEY'),
Expand Down Expand Up @@ -143,4 +143,45 @@ public function testSend()

$this->assertSame('foobar', $message->getMessageId());
}

/**
* IDN (internationalized domain names) like kältetechnik-xyz.de need to be transformed to ACE
* (ASCII Compatible Encoding) e.g.xn--kltetechnik-xyz-0kb.de, otherwise brevo api answers with 400 http code.
*
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
public function testSendForIdnDomains()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.brevo.com:8984/v3/smtp/email', $url);
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]);

$body = json_decode($options['body'], true);
// to
$this->assertSame('kältetechnik@xn--kltetechnik-xyz-0kb.de', $body['to'][0]['email']);
$this->assertSame('Kältetechnik Xyz', $body['to'][0]['name']);
// sender
$this->assertSame('info@xn--kltetechnik-xyz-0kb.de', $body['sender']['email']);
$this->assertSame('Kältetechnik Xyz', $body['sender']['name']);

return new MockResponse(json_encode(['messageId' => 'foobar']), [
'http_code' => 201,
]);
});

$transport = new BrevoApiTransport('ACCESS_KEY', $client);
$transport->setPort(8984);

$mail = new Email();
$mail->subject('Hello!')
->to(new Address('kältetechnik@kältetechnik-xyz.de', 'Kältetechnik Xyz'))
->from(new Address('info@kältetechnik-xyz.de', 'Kältetechnik Xyz'))
->text('Hello here!')
->html('Hello there!');

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private function prepareHeadersAndTags(Headers $headers): array

private function formatAddress(Address $address): array
{
$formattedAddress = ['email' => $address->getAddress()];
$formattedAddress = ['email' => $address->getEncodedAddress()];

if ($address->getName()) {
$formattedAddress['name'] = $address->getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testToString(SendinblueApiTransport $transport, string $expected
$this->assertSame($expected, (string) $transport);
}

public static function getTransportData()
public static function getTransportData(): \Generator
{
yield [
new SendinblueApiTransport('ACCESS_KEY'),
Expand Down Expand Up @@ -149,4 +149,45 @@ public function testSend()

$this->assertSame('foobar', $message->getMessageId());
}

/**
* IDN (internationalized domain names) like kältetechnik-xyz.de need to be transformed to ACE
* (ASCII Compatible Encoding) e.g.xn--kltetechnik-xyz-0kb.de, otherwise SendinBlue api answers with 400 http code.
*
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
public function testSendForIdnDomains()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.sendinblue.com:8984/v3/smtp/email', $url);
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]);

$body = json_decode($options['body'], true);
// to
$this->assertSame('kältetechnik@xn--kltetechnik-xyz-0kb.de', $body['to'][0]['email']);
$this->assertSame('Kältetechnik Xyz', $body['to'][0]['name']);
// sender
$this->assertSame('info@xn--kltetechnik-xyz-0kb.de', $body['sender']['email']);
$this->assertSame('Kältetechnik Xyz', $body['sender']['name']);

return new MockResponse(json_encode(['messageId' => 'foobar']), [
'http_code' => 201,
]);
});

$transport = new SendinblueApiTransport('ACCESS_KEY', $client);
$transport->setPort(8984);

$mail = new Email();
$mail->subject('Hello!')
->to(new Address('kältetechnik@kältetechnik-xyz.de', 'Kältetechnik Xyz'))
->from(new Address('info@kältetechnik-xyz.de', 'Kältetechnik Xyz'))
->text('Hello here!')
->html('Hello there!');

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function prepareHeadersAndTags(Headers $headers): array

private function stringifyAddress(Address $address): array
{
$stringifiedAddress = ['email' => $address->getAddress()];
$stringifiedAddress = ['email' => $address->getEncodedAddress()];

if ($address->getName()) {
$stringifiedAddress['name'] = $address->getName();
Expand Down