8000 [Mailer] Add option to enable Sandbox via dsn option sandbox=true by mdawart · Pull Request #49429 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mailer] Add option to enable Sandbox via dsn option sandbox=true #49429

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 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Mailer] Add option to enable Sandbox via dsn option sandbox=true
  • Loading branch information
mdawart authored and nicolas-grekas committed Feb 21, 2023
commit d60f400ed21e5c00b7f62f8fb7f61a5ad7cab028
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailjet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add sandbox option

5.2.0
-----

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailjet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Configuration examples:
```dotenv
# API
MAILER_DSN=mailjet+api://$PUBLIC_KEY:$PRIVATE_KEY@default
MAILER_DSN=mailjet+api://$PUBLIC_KEY:$PRIVATE_KEY@default?sandbox=true
# SMTP
MAILER_DSN=mailjet+smtp://$PUBLIC_KEY:$PRIVATE_KEY@default
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ public function testHeaderToMessage()

$transport = new MailjetApiTransport(self::USER, self::PASSWORD);
$method = new \ReflectionMethod(MailjetApiTransport::class, 'getPayload');
$method->setAccessible(true);
self::assertSame(
[
'Messages' => [
Expand Down Expand Up @@ -364,6 +363,59 @@ public function testHeaderToMessage()
'TrackOpen' => 'account_default',
],
],
'SandBoxMode' => false,
],
$method->invoke($transport, $email, $envelope)
);

$transport = new MailjetApiTransport(self::USER, self::PASSWORD, sandbox: true);
$method = new \ReflectionMethod(MailjetApiTransport::class, 'getPayload');
self::assertSame(
[
'Messages' => [
[
'From' => [
'Email' => 'foo@example.com',
'Name' => 'Foo',
],
'To' => [
[
'Email' => 'bar@example.com',
'Name' => '',
],
],
'Subject' => 'Sending email to mailjet API',
'Attachments' => [],
'InlinedAttachments' => [],
'ReplyTo' => [
'Email' => 'qux@example.com',
'Name' => 'Qux',
],
'Headers' => [
'X-authorized-header' => 'authorized',
],
'TemplateLanguage' => true,
'TemplateID' => 12345,
'TemplateErrorReporting' => [
'Email' => 'errors@mailjet.com',
'Name' => 'Error Email',
],
'TemplateErrorDeliver' => true,
'Variables' => [
'varname1' => 'value1',
'varname2' => 'value2',
'varname3' => 'value3',
],
'CustomID' => 'CustomValue',
'EventPayload' => 'Eticket,1234,row,15,seat,B',
'CustomCampaign' => 'SendAPI_campaign',
'DeduplicateCampaign' => true,
'Priority' => 2,
'TrackClick' => 'account_default',
'TrackOpen' => 'account_default',
],
],
'SandBoxMode' => true,
],
$method->invoke($transport, $email, $envelope)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ class MailjetApiTransport extends AbstractApiTransport

private string $privateKey;
private string $publicKey;
private bool $sandbox;

public function __construct(string $publicKey, string $privateKey, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
public function __construct(string $publicKey, string $privateKey, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null, bool $sandbox = false)
{
$this->publicKey = $publicKey;
$this->privateKey = $privateKey;
$this->sandbox = $sandbox;

parent::__construct($client, $dispatcher, $logger);
}
Expand Down Expand Up @@ -153,6 +155,7 @@ private function getPayload(Email $email, Envelope $envelope): array

return [
'Messages' => [$message],
'SandBoxMode' => $this->sandbox,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public function create(Dsn $dsn): TransportInterface
$user = $this->getUser($dsn);
$password = $this->getPassword($dsn);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$sandbox = filter_var($dsn->getOption('sandbox', false), \FILTER_VALIDATE_BOOL);

if ('mailjet+api' === $scheme) {
return (new MailjetApiTransport($user, $password, $this->client, $this->dispatcher, $this->logger))->setHost($host);
return (new MailjetApiTransport($user, $password, $this->client, $this->dispatcher, $this->logger, $sandbox))->setHost($host);
}

if (\in_array($scheme, ['mailjet+smtp', 'mailjet+smtps', 'mailjet'])) {
Expand Down
0