-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[mailgun-mailer] support EU-endpoint #31897
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
Closed
+130
−39
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Symfony/Component/Mailer/Bridge/Mailgun/MailgunRegionConfiguration.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Mailgun; | ||
|
||
use Symfony\Component\Mailer\Exception\RuntimeException; | ||
|
||
/** | ||
* @author Michael Garifullin <garifullin@gmail.com> | ||
* | ||
* @experimental in 4.3 | ||
*/ | ||
final class MailgunRegionConfiguration | ||
{ | ||
public const REGION_DEFAULT = self::REGION_US; | ||
public const REGION_EU = 'EU'; | ||
public const REGION_US = 'US'; | ||
|
||
private const SMTP_HOSTS = [ | ||
self::REGION_EU => 'smtp.eu.mailgun.org', | ||
self::REGION_US => 'smtp.mailgun.org', | ||
]; | ||
|
||
private const ENDPOINT_DOMAINS = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API_HOSTS for consistency with SMTP_HOSTS. |
||
self::REGION_EU => 'api.eu.mailgun.net', | ||
self::REGION_US => 'api.mailgun.net', | ||
]; | ||
|
||
private const HTTP_API_ENDPOINT = 'https://%s/v3/%s/messages'; | ||
private const HTTP_ENDPOINT = 'https://%s/v3/%s/messages.mime'; | ||
|
||
public static function resolveSmtpDomainByRegion(string $region = self::REGION_US): string | ||
{ | ||
return self::resolveRegionArray(self::SMTP_HOSTS, $region); | ||
} | ||
|
||
public static function resolveApiEndpoint(string $domain, string $region = self::REGION_US): string | ||
{ | ||
return sprintf(self::HTTP_API_ENDPOINT, self::resolveHttpDomainByRegion($region), urlencode($domain)); | ||
} | ||
|
||
public static function resolveHttpEndpoint(string $domain, string $region = self::REGION_US): string | ||
{ | ||
return sprintf(self::HTTP_ENDPOINT, self::resolveHttpDomainByRegion($region), urlencode($domain)); | ||
} | ||
|
||
private static function resolveHttpDomainByRegion(string $region = self::REGION_DEFAULT): string | ||
{ | ||
return self::resolveRegionArray(self::ENDPOINT_DOMAINS, $region); | ||
} | ||
|
||
private static function resolveRegionArray(array $regionMapping, string $region = self::REGION_DEFAULT): string | ||
{ | ||
if (empty($regionMapping[$region])) { | ||
throw new RuntimeException(sprintf('Region "%s" for Mailgun is incorrect', $region)); | ||
} | ||
|
||
return $regionMapping[$region]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the
MailgunRegionConfiguration
class is really needed.You can, just change the
ENDPOINT
constant in each Mailgun Transports (or add it for the smtp transport) to add the region endpoint, something like :private const ENDPOINT = 'https://%region_endpoint%.mailgun.net/v3/%domain%/messages'
And store the region the same way as the domain,
$this->regionEndpoint = 'us' === $region ? 'api' : 'api.eu';
Then (at line 51) replace the domain and the region endpoint at the same time
$endpoint = str_replace(['%domain%', '%region_endpoint%'], [urlencode($this->domain), $this->regionEndpoint], self::ENDPOINT);
What do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that might be become tricky as soon as new region for example Asia might have host name https://mailgun.asia/
Me too don't like that helper class but we have to resolve endpoint in 3 different transports so sooner or later we have met case at which that will require to be extracted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they would use the same pattern instead of using something different. But anyway, that's speculation at this point. I prefer #31998, which implements it the same as what we have in
SesTransport
, so it's more consistent across the board.