Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d5e48b6

Browse filesBrowse files
author
Michael Garifullin
committed
[mailgun-mailer] support EU-endpoint. fix notices
1 parent 8ba22cd commit d5e48b6
Copy full SHA for d5e48b6

File tree

Expand file treeCollapse file tree

6 files changed

+70
-68
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+70
-68
lines changed

‎src/Symfony/Component/Mailer/Bridge/Mailgun/Http/Api/MailgunTransport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailgun/Http/Api/MailgunTransport.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16-
use Symfony\Component\Mailer\Bridge\Mailgun\Mailgun;
16+
use Symfony\Component\Mailer\Bridge\Mailgun\MailgunRegionConfiguration;
1717
use Symfony\Component\Mailer\Exception\TransportException;
1818
use Symfony\Component\Mailer\SmtpEnvelope;
1919
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport;
@@ -32,7 +32,7 @@ class MailgunTransport extends AbstractApiTransport
3232
private $domain;
3333
private $region;
3434

35-
public function __construct(string $key, string $domain, string $region = Mailgun::REGION_US, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
35+
public function __construct(string $key, string $domain, string $region = MailgunRegionConfiguration::REGION_DEFAULT, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
3636
{
3737
$this->key = $key;
3838
$this->domain = $domain;
@@ -49,7 +49,7 @@ protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
4949
$headers[] = $header->toString();
5050
}
5151

52-
$response = $this->client->request('POST', Mailgun::resolveApiEndpoint($this->domain, $this->region), [
52+
$response = $this->client->request('POST', MailgunRegionConfiguration::resolveApiEndpoint($this->domain, $this->region), [
5353
'auth_basic' => 'api:'.$this->key,
5454
'headers' => $headers,
5555
'body' => $body->bodyToIterable(),

‎src/Symfony/Component/Mailer/Bridge/Mailgun/Http/MailgunTransport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailgun/Http/MailgunTransport.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16-
use Symfony\Component\Mailer\Bridge\Mailgun\Mailgun;
16+
use Symfony\Component\Mailer\Bridge\Mailgun\MailgunRegionConfiguration;
1717
use Symfony\Component\Mailer\Exception\TransportException;
1818
use Symfony\Component\Mailer\SentMessage;
1919
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
@@ -32,7 +32,7 @@ class MailgunTransport extends AbstractHttpTransport
3232
private $domain;
3333
private $region;
3434

35-
public function __construct(string $key, string $domain, string $region = Mailgun::REGION_US, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
35+
public function __construct(string $key, string $domain, string $region = MailgunRegionConfiguration::REGION_DEFAULT, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
3636
{
3737
$this->key = $key;
3838
$this->domain = $domain;
@@ -51,7 +51,7 @@ protected function doSend(SentMessage $message): void
5151
foreach ($body->getPreparedHeaders()->getAll() as $header) {
5252
$headers[] = $header->toString();
5353
}
54-
$response = $this->client->request('POST', Mailgun::resolveHttpEndpoint($this->domain, $this->region), [
54+
$response = $this->client->request('POST', MailgunRegionConfiguration::resolveHttpEndpoint($this->domain, $this->region), [
5555
'auth_basic' => 'api:'.$this->key,
5656
'headers' => $headers,
5757
'body' => $body->bodyToIterable(),

‎src/Symfony/Component/Mailer/Bridge/Mailgun/Mailgun.php renamed to ‎src/Symfony/Component/Mailer/Bridge/Mailgun/MailgunRegionConfiguration.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailgun/MailgunRegionConfiguration.php
+22-36Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,58 @@
1111

1212
namespace Symfony\Component\Mailer\Bridge\Mailgun;
1313

14+
use Symfony\Component\Mailer\Exception\RuntimeException;
15+
1416
/**
1517
* @author Michael Garifullin <garifullin@gmail.com>
1618
*
1719
* @experimental in 4.3
1820
*/
19-
class Mailgun
21+
class MailgunRegionConfiguration
2022
{
23+
public const REGION_DEFAULT = self::REGION_US;
2124
public const REGION_EU = 'EU';
2225
public const REGION_US = 'US';
2326

24-
private const SMTP_DOMAIN_US = 'smtp.mailgun.org';
25-
private const SMTP_DOMAIN_EU = 'smtp.eu.mailgun.org';
27+
private const SMTP_HOSTS = [
28+
self::REGION_EU => 'smtp.eu.mailgun.org',
29+
self::REGION_US => 'smtp.mailgun.org',
30+
];
2631

27-
private const ENDPOINT_DOMAIN_EU = 'api.eu.mailgun.net';
28-
private const ENDPOINT_DOMAIN_US = 'api.mailgun.net';
32+
private const ENDPOINT_DOMAINS = [
33+
self::REGION_EU => 'api.eu.mailgun.net',
34+
self::REGION_US => 'api.mailgun.net',
35+
];
2936

3037
private const HTTP_API_ENDPOINT = 'https://%s/v3/%s/messages';
3138
private const HTTP_ENDPOINT = 'https://%s/v3/%s/messages.mime';
3239

33-
/**
34-
* @param string $region
35-
*
36-
* @return string
37-
*/
3840
public static function resolveSmtpDomainByRegion(string $region = self::REGION_US): string
3941
{
40-
if (self::REGION_EU === $region) {
41-
return self::SMTP_DOMAIN_EU;
42-
}
43-
44-
return self::SMTP_DOMAIN_US;
42+
return self::resolveRegionArray(self::SMTP_HOSTS, $region);
4543
}
4644

47-
/**
48-
* @param string $domain
49-
* @param string $region
50-
*
51-
* @return string
52-
*/
5345
public static function resolveApiEndpoint(string $domain, string $region = self::REGION_US): string
5446
{
5547
return sprintf(self::HTTP_API_ENDPOINT, self::resolveHttpDomainByRegion($region), urlencode($domain));
5648
}
5749

58-
/**
59-
* @param string $domain
60-
* @param string $region
61-
*
62-
* @return string
63-
*/
6450
public static function resolveHttpEndpoint(string $domain, string $region = self::REGION_US): string
6551
{
6652
return sprintf(self::HTTP_ENDPOINT, self::resolveHttpDomainByRegion($region), urlencode($domain));
6753
}
6854

69-
/**
70-
* @param string $region
71-
*
72-
* @return string
73-
*/
74-
private static function resolveHttpDomainByRegion(string $region = self::ENDPOINT_DOMAIN_US): string
55+
private static function resolveHttpDomainByRegion(string $region = self::REGION_DEFAULT): string
56+
{
57+
return self::resolveRegionArray(self::ENDPOINT_DOMAINS, $region);
58+
}
59+
60+
private static function resolveRegionArray(array $regionMapping, string $region = self::REGION_DEFAULT): string
7561
{
76-
if (self::REGION_EU === $region) {
77-
return self::ENDPOINT_DOMAIN_EU;
62+
if (empty($regionMapping[$region])) {
63+
throw new RuntimeException(sprintf('Region "%s" for Mailgun is incorrect', $region));
7864
}
7965

80-
return self::ENDPOINT_DOMAIN_US;
66+
return $regionMapping[$region];
8167
}
8268
}

‎src/Symfony/Component/Mailer/Bridge/Mailgun/Smtp/MailgunTransport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailgun/Smtp/MailgunTransport.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16-
use Symfony\Component\Mailer\Bridge\Mailgun\Mailgun;
16+
use Symfony\Component\Mailer\Bridge\Mailgun\MailgunRegionConfiguration;
1717
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
1818

1919
/**
@@ -23,9 +23,9 @@
2323
*/
2424
class MailgunTransport extends EsmtpTransport
2525
{
26-
public function __construct(string $username, string $password, string $region = Mailgun::REGION_US, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
26+
public function __construct(string $username, string $password, string $region = MailgunRegionConfiguration::REGION_DEFAULT, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
2727
{
28-
parent::__construct(Mailgun::resolveSmtpDomainByRegion($region), 465, 'ssl', null, $dispatcher, $logger);
28+
parent::__construct(MailgunRegionConfiguration::resolveSmtpDomainByRegion($region), 465, 'ssl', null, $dispatcher, $logger);
2929

3030
$this->setUsername($username);
3131
$this->setPassword($password);

‎src/Symfony/Component/Mailer/Tests/TransportTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Tests/TransportTest.php
+36-20Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Mailer\Bridge\Sendgrid;
2323
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
2424
use Symfony\Component\Mailer\Exception\LogicException;
25+
use Symfony\Component\Mailer\Exception\RuntimeException;
2526
use Symfony\Component\Mailer\Transport;
2627
use Symfony\Contracts\HttpClient\HttpClientInterface;
2728

@@ -96,35 +97,50 @@ public function testFromDsnGmail()
9697
Transport::fromDsn('http://gmail');
9798
}
9899

99-
public function testFromDsnMailgun()
100+
/**
101+
* @dataProvider provideFromDsnMailgun
102+
*/
103+
public function testFromDsnMailgun(string $expectedInstance, string $protocol, $region = null)
100104
{
101105
$dispatcher = $this->createMock(EventDispatcherInterface::class);
106+
$client = $this->createMock(HttpClientInterface::class);
102107
$logger = $this->createMock(LoggerInterface::class);
103-
$transport = Transport::fromDsn('smtp://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=EU', $dispatcher, null, $logger);
104-
$this->assertInstanceOf(Mailgun\Smtp\MailgunTransport::class, $transport);
105-
$this->assertEquals('u$er', $transport->getUsername());
106-
$this->assertEquals('pa$s', $transport->getPassword());
108+
109+
$dsn = $protocol.'://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun'.$region;
110+
$transport = Transport::fromDsn($dsn, $dispatcher, $client, $logger);
111+
$this->assertInstanceOf($expectedInstance, $transport);
112+
if (Mailgun\Smtp\MailgunTransport::class === $expectedInstance) {
113+
$this->assertEquals('u$er', $transport->getUsername());
114+
$this->assertEquals('pa$s', $transport->getPassword());
115+
} else {
116+
$this->assertProperties($transport, $dispatcher, $logger, [
117+
'key' => 'u$er',
118+
'domain' => 'pa$s',
119+
'client' => $client,
120+
]);
121+
}
107122
$this->assertProperties($transport, $dispatcher, $logger);
108123

109-
$client = $this->createMock(HttpClientInterface::class);
110-
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=EU', $dispatcher, $client, $logger);
111-
$this->assertInstanceOf(Mailgun\Http\MailgunTransport::class, $transport);
112-
$this->assertProperties($transport, $dispatcher, $logger, [
113-
'key' => 'u$er',
114-
'domain' => 'pa$s',
115-
'client' => $client,
116-
]);
124+
$this->expectException(LogicException::class);
125+
Transport::fromDsn('foo://mailgun');
126+
}
117127

118-
$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=EU', $dispatcher, $client, $logger);
119-
$this->assertInstanceOf(Mailgun\Http\Api\MailgunTransport::class, $transport);
120-
$this->assertProperties($transport, $dispatcher, $logger, [
121-
'key' => 'u$er',
122-
'domain' => 'pa$s',
123-
'client' => $client,
124-
]);
128+
public function provideFromDsnMailgun()
129+
{
130+
yield [Mailgun\Smtp\MailgunTransport::class, 'smtp', null];
131+
yield [Mailgun\Http\MailgunTransport::class, 'http', null];
132+
yield [Mailgun\Http\Api\MailgunTransport::class, 'api', null];
133+
yield [Mailgun\Http\Api\MailgunTransport::class, 'api', '?region=EU'];
134+
yield [Mailgun\Http\Api\MailgunTransport::class, 'api', '?region=US'];
135+
}
125136

137+
public function testFromDsnMailgunWithExpection()
138+
{
126139
$this->expectException(LogicException::class);
127140
Transport::fromDsn('foo://mailgun');
141+
142+
$this->expectException(RuntimeException::class);
143+
Transport::fromDsn('api://user:password@mailgun?region=RU');
128144
}
129145

130146
public function testFromDsnPostmark()

‎src/Symfony/Component/Mailer/Transport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Transport.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ private static function createTransport(string $dsn, EventDispatcherInterface $d
101101
}
102102

103103
if ('smtp' === $parsedDsn['scheme']) {
104-
return new Mailgun\Smtp\MailgunTransport($user, $pass, $query['region'] ?? null, $dispatcher, $logger);
104+
return new Mailgun\Smtp\MailgunTransport($user, $pass, $query['region'] ?? Mailgun\MailgunRegionConfiguration::REGION_DEFAULT, $dispatcher, $logger);
105105
}
106106
if ('http' === $parsedDsn['scheme']) {
107-
return new Mailgun\Http\MailgunTransport($user, $pass, $query['region'] ?? null, $client, $dispatcher, $logger);
107+
return new Mailgun\Http\MailgunTransport($user, $pass, $query['region'] ?? Mailgun\MailgunRegionConfiguration::REGION_DEFAULT, $client, $dispatcher, $logger);
108108
}
109109
if ('api' === $parsedDsn['scheme']) {
110-
return new Mailgun\Http\Api\MailgunTransport($user, $pass, $query['region'] ?? null, $client, $dispatcher, $logger);
110+
return new Mailgun\Http\Api\MailgunTransport($user, $pass, $query['region'] ?? Mailgun\MailgunRegionConfiguration::REGION_DEFAULT, $client, $dispatcher, $logger);
111111
}
112112

113113
throw new LogicException(sprintf('The "%s" scheme is not supported for mailer "%s".', $parsedDsn['scheme'], $parsedDsn['host']));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.