8000 Parameterize Mailgun's region · symfony/symfony@7439c8d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7439c8d

Browse files
committed
Parameterize Mailgun's region
1 parent 9865988 commit 7439c8d

File tree

6 files changed

+61
-12
lines changed

6 files changed

+61
-12
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@
2727
*/
2828
class MailgunTransport extends AbstractApiTransport
2929
{
30-
private const ENDPOINT = 'https://api.mailgun.net/v3/%domain%/messages';
30+
private const ENDPOINT = 'https://api.%region_dot%mailgun.net/v3/%domain%/messages';
3131

3232
private $key;
3333
private $domain;
34+
private $region;
3435

35-
public function __construct(string $key, string $domain, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
36+
public function __construct(string $key, string $domain, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
3637
{
3738
$this->key = $key;
3839
$this->domain = $domain;
40+
$this->region = $region;
3941

4042
parent::__construct($client, $dispatcher, $logger);
4143
}
@@ -48,7 +50,7 @@ protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void
4850
$headers[] = $header->toString();
4951
}
5052

51-
$endpoint = str_replace('%domain%', urlencode($this->domain), self::ENDPOINT);
53+
$endpoint = str_replace(['%domain%', '%region_dot%'], [urlencode($this->domain), 'us' !== ($this->region ?: 'us') ? $this->region.'.' : ''], self::ENDPOINT);
5254
$response = $this->client->request('POST', $endpoint, [
5355
'auth_basic' => 'api:'.$this->key,
5456
'headers' => $headers,

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
*/
2828
class MailgunTransport extends AbstractHttpTransport
2929
{
30-
private const ENDPOINT = 'https://api.mailgun.net/v3/%domain%/messages.mime';
30+
private const ENDPOINT = 'https://api.%region_dot%mailgun.net/v3/%domain%/messages.mime';
3131
private $key;
3232
private $domain;
33+
private $region;
3334

34-
public function __construct(string $key, string $domain, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
35+
public function __construct(string $key, string $domain, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
3536
{
3637
$this->key = $key;
3738
$this->domain = $domain;
39+
$this->region = $region;
3840

3941
parent::__construct($client, $dispatcher, $logger);
4042
}
@@ -49,7 +51,7 @@ protected function doSend(SentMessage $message): void
4951
foreach ($body->getPreparedHeaders()->getAll() as $header) {
5052
$headers[] = $header->toString();
5153
}
52-
$endpoint = str_replace('%domain%', urlencode($this->domain), self::ENDPOINT);
54+
$endpoint = str_replace(['%domain%', '%region_dot%'], [urlencode($this->domain), 'us' !== ($this->region ?: 'us') ? $this->region.'.' : ''], self::ENDPOINT);
5355
$response = $this->client->request('POST', $endpoint, [
5456
'auth_basic' => 'api:'.$this->key,
5557
'headers' => $headers,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323
class MailgunTransport extends EsmtpTransport
2424
{
25-
public function __construct(string $username, string $password, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
25+
public function __construct(string $username, string $password, string $region = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
2626
{
27-
parent::__construct('smtp.mailgun.org', 465, 'ssl', null, $dispatcher, $logger);
27+
parent::__construct('us' !== ($region ?: 'us') ? sprintf('smtp.%s.mailgun.org', $region) : 'smtp.mailgun.org', 465, 'ssl', null, $dispatcher, $logger);
2828

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

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
2424
use Symfony\Component\Mailer\Exception\LogicException;
2525
use Symfony\Component\Mailer\Transport;
26+
use Symfony\Component\Mime\Email;
2627
use Symfony\Contracts\HttpClient\HttpClientInterface;
28+
use Symfony\Contracts\HttpClient\ResponseInterface;
2729

2830
class TransportTest extends TestCase
2931
{
@@ -106,6 +108,15 @@ public function testFromDsnMailgun()
106108
$this->assertEquals('pa$s', $transport->getPassword());
107109
$this->assertProperties($transport, $dispatcher, $logger);
108110

111+
$transport = Transport::fromDsn('smtp://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, null, $logger);
112+
$this->assertEquals('smtp.mailgun.org', $transport->getStream()->getHost());
113+
114+
$transport = Transport::fromDsn('smtp://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=eu', $dispatcher, null, $logger);
115+
$this->assertEquals('smtp.eu.mailgun.org', $transport->getStream()->getHost());
116+
117+
$transport = Transport::fromDsn('smtp://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=us', $dispatcher, null, $logger);
118+
$this->assertEquals('smtp.mailgun.org', $transport->getStream()->getHost());
119+
109120
$client = $this->createMock(HttpClientInterface::class);
110121
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, $client, $logger);
111122
$this->assertInstanceOf(Mailgun\Http\MailgunTransport::class, $transport);
@@ -115,6 +126,25 @@ public function testFromDsnMailgun()
115126
'client' => $client,
116127
]);
117128

129+
$response = $this->createMock(ResponseInterface::class);
130+
$response->expects($this->any())->method('getStatusCode')->willReturn(200);
131+
$message = (new Email())->from('me@me.com')->to('you@you.com')->subject('hello')->text('Hello you');
132+
133+
$client = $this->createMock(HttpClientInterface::class);
134+
$client->expects($this->once())->method('request')->with('POST', 'https://api.mailgun.net/v3/pa%24s/messages.mime')->willReturn($response);
135+
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, $client, $logger);
136+
$transport->send($message);
137+
138+
$client = $this->createMock(HttpClientInterface::class);
139+
$client->expects($this->once())->method('request')->with('POST', 'https://api.eu.mailgun.net/v3/pa%24s/messages.mime')->willReturn($response);
140+
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=eu', $dispatcher, $client, $logger);
141+
$transport->send($message);
142+
143+
$client = $this->createMock(HttpClientInterface::class);
144+
$client->expects($this->once())->method('request')->with('POST', 'https://api.mailgun.net/v3/pa%24s/messages.mime')->willReturn($response);
145+
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=us', $dispatcher, $client, $logger);
146+
$transport->send($message);
147+
118148
$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, $client, $logger);
119149
$this->assertInstanceOf(Mailgun\Http\Api\MailgunTransport::class, $transport);
120150
$this->assertProperties($transport, $dispatcher, $logger, [
@@ -123,6 +153,21 @@ public function testFromDsnMailgun()
123153
'client' => $client,
124154
]);
125155

156+
$client = $this->createMock(HttpClientInterface::class);
157+
$client->expects($this->once())->method('request')->with('POST', 'https://api.mailgun.net/v3/pa%24s/messages')->willReturn($response);
158+
$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, $client, $logger);
159+
$transport->send($message);
160+
161+
$client = $this->createMock(HttpClientInterface::class);
162+
$client->expects($this->once())->method('request')->with('POST', 'https://api.eu.mailgun.net/v3/pa%24s/messages')->willReturn($response);
163+
$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=eu', $dispatcher, $client, $logger);
164+
$transport->send($message);
165+
166+
$client = $this->createMock(HttpClientInterface::class);
167+
$client->expects($this->once())->method('request')->with('POST', 'https://api.mailgun.net/v3/pa%24s/messages')->willReturn($response);
168+
$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=us', $dispatcher, $client, $logger);
169+
$transport->send($message);
170+
126171
$this->expectException(LogicException::class);
127172
Transport::fromDsn('foo://mailgun');
128173
}

src/Symfony/Component/Mailer/Transport.php

Lines 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, $dispatcher, $logger);
104+
return new Mailgun\Smtp\MailgunTransport($user, $pass, $query['region'] ?? null, $dispatcher, $logger);
105105
}
106106
if ('http' === $parsedDsn['scheme']) {
107-
return new Mailgun\Http\MailgunTransport($user, $pass, $client, $dispatcher, $logger);
107+
return new Mailgun\Http\MailgunTransport($user, $pass, $query['region'] ?? null, $client, $dispatcher, $logger);
108108
}
109109
if ('api' === $parsedDsn['scheme']) {
110-
return new Mailgun\Http\Api\MailgunTransport($user, $pass, $client, $dispatcher, $logger);
110+
return new Mailgun\Http\Api\MailgunTransport($user, $pass, $query['region'] ?? null, $client, $dispatcher, $logger);
111111
}
112112

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

src/Symfony/Component/Mailer/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"symfony/amazon-mailer": "^4.3",
2727
"symfony/google-mailer": "^4.3",
2828
"symfony/http-client-contracts": "^1.1",
29-
"symfony/mailgun-mailer": "^4.3",
29+
"symfony/mailgun-mailer": "^4.3.2",
3030
"symfony/mailchimp-mailer": "^4.3",
3131
"symfony/postmark-mailer": "^4.3",
3232
"symfony/sendgrid-mailer": "^4.3"

0 commit comments

Comments
 (0)
0