Skip to content

Commit

Permalink
bug #31998 Parameterize Mailgun's region (jderusse)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.3 branch.

Discussion
----------

Parameterize Mailgun's region

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31810
| License       | MIT
| Doc PR        | TODO

Mailgun is available in 2 regions (US and EU), when registering a custom domain, users can choose one of the 2 regions and **have to** use the right the endpoint (see documentation https://documentation.mailgun.com/en/latest/api-intro.html?highlight=smtp.mailgun.org#mailgun-regions).

This PR make the endpoint/region configurable.

Commits
-------

7439c8d Parameterize Mailgun's region
  • Loading branch information
fabpot committed Jun 14, 2019
2 parents 9865988 + 7439c8d commit d7ba773
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
Expand Up @@ -27,15 +27,17 @@
*/
class MailgunTransport extends AbstractApiTransport
{
private const ENDPOINT = 'https://api.mailgun.net/v3/%domain%/messages';
private const ENDPOINT = 'https://api.%region_dot%mailgun.net/v3/%domain%/messages';

private $key;
private $domain;
private $region;

public function __construct(string $key, string $domain, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
public function __construct(string $key, string $domain, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->key = $key;
$this->domain = $domain;
$this->region = $region;

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

$endpoint = str_replace('%domain%', urlencode($this->domain), self::ENDPOINT);
$endpoint = str_replace(['%domain%', '%region_dot%'], [urlencode($this->domain), 'us' !== ($this->region ?: 'us') ? $this->region.'.' : ''], self::ENDPOINT);
$response = $this->client->request('POST', $endpoint, [
'auth_basic' => 'api:'.$this->key,
'headers' => $headers,
Expand Down
Expand Up @@ -27,14 +27,16 @@
*/
class MailgunTransport extends AbstractHttpTransport
{
private const ENDPOINT = 'https://api.mailgun.net/v3/%domain%/messages.mime';
private const ENDPOINT = 'https://api.%region_dot%mailgun.net/v3/%domain%/messages.mime';
private $key;
private $domain;
private $region;

public function __construct(string $key, string $domain, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
public function __construct(string $key, string $domain, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->key = $key;
$this->domain = $domain;
$this->region = $region;

parent::__construct($client, $dispatcher, $logger);
}
Expand All @@ -49,7 +51,7 @@ protected function doSend(SentMessage $message): void
foreach ($body->getPreparedHeaders()->getAll() as $header) {
$headers[] = $header->toString();
}
$endpoint = str_replace('%domain%', urlencode($this->domain), self::ENDPOINT);
$endpoint = str_replace(['%domain%', '%region_dot%'], [urlencode($this->domain), 'us' !== ($this->region ?: 'us') ? $this->region.'.' : ''], self::ENDPOINT);
$response = $this->client->request('POST', $endpoint, [
'auth_basic' => 'api:'.$this->key,
'headers' => $headers,
Expand Down
Expand Up @@ -22,9 +22,9 @@
*/
class MailgunTransport extends EsmtpTransport
{
public function __construct(string $username, string $password, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
public function __construct(string $username, string $password, string $region = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct('smtp.mailgun.org', 465, 'ssl', null, $dispatcher, $logger);
parent::__construct('us' !== ($region ?: 'us') ? sprintf('smtp.%s.mailgun.org', $region) : 'smtp.mailgun.org', 465, 'ssl', null, $dispatcher, $logger);

$this->setUsername($username);
$this->setPassword($password);
Expand Down
45 changes: 45 additions & 0 deletions src/Symfony/Component/Mailer/Tests/TransportTest.php
Expand Up @@ -23,7 +23,9 @@
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class TransportTest extends TestCase
{
Expand Down Expand Up @@ -106,6 +108,15 @@ public function testFromDsnMailgun()
$this->assertEquals('pa$s', $transport->getPassword());
$this->assertProperties($transport, $dispatcher, $logger);

$transport = Transport::fromDsn('smtp://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, null, $logger);
$this->assertEquals('smtp.mailgun.org', $transport->getStream()->getHost());

$transport = Transport::fromDsn('smtp://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=eu', $dispatcher, null, $logger);
$this->assertEquals('smtp.eu.mailgun.org', $transport->getStream()->getHost());

$transport = Transport::fromDsn('smtp://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=us', $dispatcher, null, $logger);
$this->assertEquals('smtp.mailgun.org', $transport->getStream()->getHost());

$client = $this->createMock(HttpClientInterface::class);
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, $client, $logger);
$this->assertInstanceOf(Mailgun\Http\MailgunTransport::class, $transport);
Expand All @@ -115,6 +126,25 @@ public function testFromDsnMailgun()
'client' => $client,
]);

$response = $this->createMock(ResponseInterface::class);
$response->expects($this->any())->method('getStatusCode')->willReturn(200);
$message = (new Email())->from('me@me.com')->to('you@you.com')->subject('hello')->text('Hello you');

$client = $this->createMock(HttpClientInterface::class);
$client->expects($this->once())->method('request')->with('POST', 'https://api.mailgun.net/v3/pa%24s/messages.mime')->willReturn($response);
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, $client, $logger);
$transport->send($message);

$client = $this->createMock(HttpClientInterface::class);
$client->expects($this->once())->method('request')->with('POST', 'https://api.eu.mailgun.net/v3/pa%24s/messages.mime')->willReturn($response);
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=eu', $dispatcher, $client, $logger);
$transport->send($message);

$client = $this->createMock(HttpClientInterface::class);
$client->expects($this->once())->method('request')->with('POST', 'https://api.mailgun.net/v3/pa%24s/messages.mime')->willReturn($response);
$transport = Transport::fromDsn('http://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=us', $dispatcher, $client, $logger);
$transport->send($message);

$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, $client, $logger);
$this->assertInstanceOf(Mailgun\Http\Api\MailgunTransport::class, $transport);
$this->assertProperties($transport, $dispatcher, $logger, [
Expand All @@ -123,6 +153,21 @@ public function testFromDsnMailgun()
'client' => $client,
]);

$client = $this->createMock(HttpClientInterface::class);
$client->expects($this->once())->method('request')->with('POST', 'https://api.mailgun.net/v3/pa%24s/messages')->willReturn($response);
$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun', $dispatcher, $client, $logger);
$transport->send($message);

$client = $this->createMock(HttpClientInterface::class);
$client->expects($this->once())->method('request')->with('POST', 'https://api.eu.mailgun.net/v3/pa%24s/messages')->willReturn($response);
$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=eu', $dispatcher, $client, $logger);
$transport->send($message);

$client = $this->createMock(HttpClientInterface::class);
$client->expects($this->once())->method('request')->with('POST', 'https://api.mailgun.net/v3/pa%24s/messages')->willReturn($response);
$transport = Transport::fromDsn('api://'.urlencode('u$er').':'.urlencode('pa$s').'@mailgun?region=us', $dispatcher, $client, $logger);
$transport->send($message);

$this->expectException(LogicException::class);
Transport::fromDsn('foo://mailgun');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Mailer/Transport.php
Expand Up @@ -101,13 +101,13 @@ private static function createTransport(string $dsn, EventDispatcherInterface $d
}

if ('smtp' === $parsedDsn['scheme']) {
return new Mailgun\Smtp\MailgunTransport($user, $pass, $dispatcher, $logger);
return new Mailgun\Smtp\MailgunTransport($user, $pass, $query['region'] ?? null, $dispatcher, $logger);
}
if ('http' === $parsedDsn['scheme']) {
return new Mailgun\Http\MailgunTransport($user, $pass, $client, $dispatcher, $logger);
return new Mailgun\Http\MailgunTransport($user, $pass, $query['region'] ?? null, $client, $dispatcher, $logger);
}
if ('api' === $parsedDsn['scheme']) {
return new Mailgun\Http\Api\MailgunTransport($user, $pass, $client, $dispatcher, $logger);
return new Mailgun\Http\Api\MailgunTransport($user, $pass, $query['region'] ?? null, $client, $dispatcher, $logger);
}

throw new LogicException(sprintf('The "%s" scheme is not supported for mailer "%s".', $parsedDsn['scheme'], $parsedDsn['host']));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mailer/composer.json
Expand Up @@ -26,7 +26,7 @@
"symfony/amazon-mailer": "^4.3",
"symfony/google-mailer": "^4.3",
"symfony/http-client-contracts": "^1.1",
"symfony/mailgun-mailer": "^4.3",
"symfony/mailgun-mailer": "^4.3.2",
"symfony/mailchimp-mailer": "^4.3",
"symfony/postmark-mailer": "^4.3",
"symfony/sendgrid-mailer": "^4.3"
Expand Down

0 comments on commit d7ba773

Please sign in to comment.