-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add the Mailer component #30741
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
Add the Mailer component #30741
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions
28
src/Symfony/Bundle/FrameworkBundle/Resources/config/mailer.xml
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,28 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="mailer" class="Symfony\Component\Mailer\Mailer"> | ||
<argument type="service" id="mailer.transport" /> | ||
<argument type="service" id="message_bus" on-invalid="ignore" /> | ||
</service> | ||
<service id="Symfony\Component\Mailer\MailerInterface" alias="mailer" /> | ||
|
||
<service id="mailer.transport" class="Symfony\Component\Mailer\Transport\TransportInterface"> | ||
<factory class="Symfony\Component\Mailer\Transport" method="fromDsn" /> | ||
<argument /> <!-- env(MAILER_DSN) --> | ||
<argument type="service" id="event_dispatcher" /> | ||
<argument type="service" id="http_client" on-invalid="ignore" /> | ||
<argument type="service" id="logger" on-invalid="ignore" /> | ||
</service> | ||
<service id="Symfony\Component\Mailer\Transport\TransportInterface" alias="mailer.transport" /> | ||
|
||
<service id="mailer.messenger.message_handler" class="Symfony\Component\Mailer\Messenger\MessageHandler"> | ||
<argument type="service" id="mailer.transport" /> | ||
<tag name="messenger.message_handler" /> | ||
</service> | ||
</services> | ||
</container> |
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 ed
F438
itor 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="twig.mailer.message_listener" class="Symfony\Component\Mailer\EventListener\MessageListener"> | ||
<argument /> | ||
<argument type="service" id="twig.mime_body_renderer" /> | ||
</service> | ||
|
||
<service id="twig.mime_body_renderer" class="Symfony\Bridge\Twig\Mime\BodyRenderer"> | ||
<argument type="service" id="twig" /> | ||
</service> | ||
</services> | ||
</container> |
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,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
4.3.0 | ||
----- | ||
|
||
* added the bridge |
103 changes: 103 additions & 0 deletions
103
src/Symfony/Component/Mailer/Bridge/Amazon/Http/Api/SesTransport.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,103 @@ | ||
<?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\Amazon\Http\Api; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\Mailer\Exception\TransportException; | ||
use Symfony\Component\Mailer\SmtpEnvelope; | ||
use Symfony\Component\Mailer\Transport\Http\Api\AbstractApiTransport; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @experimental in 4.3 | ||
*/ | ||
class SesTransport extends AbstractApiTransport | ||
{ | ||
private const ENDPOINT = 'https://email.%region%.amazonaws.com'; | ||
|
||
private $accessKey; | ||
private $secretKey; | ||
private $region; | ||
|
||
/** | ||
* @param string $region Amazon SES region (currently one of us-east-1, us-west-2, or eu-west-1) | ||
*/ | ||
public function __construct(string $accessKey, string $secretKey, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null) | ||
{ | ||
$this->accessKey = $accessKey; | ||
$this->secretKey = $secretKey; | ||
$this->region = $region ?: 'eu-west-1'; | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
parent::__construct($client, $dispatcher, $logger); | ||
} | ||
|
||
protected function doSendEmail(Email $email, SmtpEnvelope $envelope): void | ||
{ | ||
$date = gmdate('D, d M Y H:i:s e'); | ||
$auth = sprintf('AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HmacSHA256,Signature=%s', $this->accessKey, $this->getSignature($date)); | ||
|
||
$endpoint = str_replace('%region%', $this->region, self::ENDPOINT); | ||
$response = $this->client->request('POST', $endpoint, [ | ||
'headers' => [ | ||
'X-Amzn-Authorization' => $auth, | ||
'Date' => $date, | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
], | ||
'body' => $this->getPayload($email, $envelope), | ||
]); | ||
|
||
if (200 !== $response->getStatusCode()) { | ||
$error = new \SimpleXMLElement($response->getContent(false)); | ||
|
||
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code)); | ||
} | ||
} | ||
|
||
private function getSignature(string $string): string | ||
{ | ||
return base64_encode(hash_hmac('sha256', $string, $this->secretKey, true)); | ||
} | ||
|
||
private function getPayload(Email $email, SmtpEnvelope $envelope): array | ||
{ | ||
if ($email->getAttachments()) { | ||
return [ | ||
'Action' => 'SendRawEmail', | ||
'RawMessage.Data' => \base64_encode($email->toString()), | ||
]; | ||
} | ||
|
||
$payload = [ | ||
'Action' => 'SendEmail', | ||
'Destination.ToAddresses.member' => $this->stringifyAddresses($this->getRecipients($email, $envelope)), | ||
'Message.Subject.Data' => $email->getSubject(), | ||
'Source' => $envelope->getSender()->toString(), | ||
]; | ||
|
||
if ($emails = $email->getCc()) { | ||
$payload['Destination.CcAddresses.member'] = $this->stringifyAddresses($emails); | ||
} | ||
if ($emails = $email->getBcc()) { | ||
$payload['Destination.BccAddresses.member'] = $this->stringifyAddresses($emails); | ||
} | ||
if ($email->getTextBody()) { | ||
$payload['Message.Body.Text.Data'] = $email->getTextBody(); | ||
} | ||
if ($email->getHtmlBody()) { | ||
$payload['Message.Body.Html.Data'] = $email->getHtmlBody(); | ||
} | ||
|
||
return $payload; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/Symfony/Component/Mailer/Bridge/Amazon/Http/SesTransport.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,75 @@ | ||
<?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\Amazon\Http; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Component\Mailer\Exception\TransportException; | ||
use Symfony\Component\Mailer\SentMessage; | ||
use Symfony\Component\Mailer\Transport\AbstractTransport; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @experimental in 4.3 | ||
*/ | ||
class SesTransport extends AbstractTransport | ||
{ | ||
private const ENDPOINT = 'https://email.%region%.amazonaws.com'; | ||
|
||
private $client; | ||
private $accessKey; | ||
private $secretKey; | ||
private $region; | ||
|
||
/** | ||
* @param string $region Amazon SES region (currently one of us-east-1, us-west-2, or eu-west-1) | ||
*/ | ||
public function __construct(string $accessKey, string $secretKey, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null) | ||
{ | ||
$this->client = $client ?? HttpClient::create(); | ||
10000 | $this->accessKey = $accessKey; | |
$this->secretKey = $secretKey; | ||
$this->region = $region ?: 'eu-west-1'; | ||
|
||
parent::__construct($dispatcher, $logger); | ||
} | ||
|
||
protected function doSend(SentMessage $message): void | ||
{ | ||
$date = gmdate('D, d M Y H:i:s e'); | ||
$auth = sprintf('AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HmacSHA256,Signature=%s', $this->accessKey, $this->getSignature($date)); | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$endpoint = str_replace('%region%', $this->region, self::ENDPOINT); | ||
$response = $this->client->request('POST', $endpoint, [ | ||
'headers' => [ | ||
'X-Amzn-Authorization' => $auth, | ||
'Date' => $date, | ||
], | ||
'body' => [ | ||
'Action' => 'SendRawEmail', | ||
'RawMessage.Data' => \base64_encode($message->toString()), | ||
], | ||
]); | ||
|
||
if (200 !== $response->getStatusCode()) { | ||
$error = new \SimpleXMLElement($response->getContent(false)); | ||
|
||
throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $error->Error->Message, $error->Error->Code)); | ||
} | ||
} | ||
|
||
private function getSignature(string $string): string | ||
{ | ||
return base64_encode(hash_hmac('sha256', $string, $this->secretKey, true)); | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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,19 @@ | ||
Copyright (c) 2019 Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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 chang 6090 e |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Amazon Mailer | ||
============= | ||
|
||
Provides Amazon SES integration for Symfony Mailer. | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.