Skip to content

Commit

Permalink
Merge pull request #2 from nlemoine/fix-read-env-vars
Browse files Browse the repository at this point in the history
Fix reading/getting env vars
  • Loading branch information
voronkovich authored Sep 10, 2024
2 parents 73dbab9 + 6a53270 commit 827e653
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/PHPMailerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPMailer\PHPMailer\DSNConfigurator;
use PHPMailer\PHPMailer\PHPMailer;

use function add_action;
use function constant;
use function defined;
use function explode;
Expand Down Expand Up @@ -92,14 +91,15 @@ private function configureDKIM(PHPMailer $mailer): void
$mailer->DKIM_identity = $identity;
}

if ($domain = $this->getConfig('MAILER_DKIM_DOMAIN', explode('@', $identity)[1] ?? null)) {
if ($domain = $this->getConfig('MAILER_DKIM_DOMAIN', explode('@', (string) $identity)[1] ?? null)) {
$mailer->DKIM_domain = $domain;
}
}

private function getConfig(string $key, $default = null)
{
if ($value = getenv($key)) {
$value = $_SERVER[$key] ?? $_ENV[$key] ?? getenv($key) !== false ?: null;
if ($value !== null) {
return $value;
}

Expand Down
35 changes: 18 additions & 17 deletions tests/PHPMailerConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use PopArtDesign\WordPressMailerDSN\PHPMailerConfigurator;


/**
* @covers PHPMailerConfigurator
* @runTestsInSeparateProcesses
Expand All @@ -20,7 +21,7 @@ public function testConfiguresUsingMailerDsnEnv()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_DSN=smtps://pop:art@popartdesign.ru:587?SMTPDebug=3&Timeout=1000');
$_SERVER['MAILER_DSN'] = 'smtps://pop:art@popartdesign.ru:587?SMTPDebug=3&Timeout=1000';

$configurator->configure($mailer);

Expand Down Expand Up @@ -51,7 +52,7 @@ public function testGivesPreferenceToEnvsOverConstants()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_DSN=smtps://pop:art@popartdesign.ru:587?SMTPDebug=3&Timeout=1000');
$_SERVER['MAILER_DSN'] = 'smtps://pop:art@popartdesign.ru:587?SMTPDebug=3&Timeout=1000';
define('MAILER_DSN', 'sendmail://localhost?Sendmail=/usr/sbin/sendmail%20-oi%20-t');

$configurator->configure($mailer);
Expand All @@ -64,8 +65,8 @@ public function testConfiguresDebugUsingEnvs()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_DEBUG=3');
putenv('MAILER_DEBUG_OUTPUT=error_log');
$_SERVER['MAILER_DEBUG'] = 3;
$_SERVER['MAILER_DEBUG_OUTPUT'] = 'error_log';

$configurator->configure($mailer);

Expand All @@ -78,9 +79,9 @@ public function testConfiguresCommonUsingEnvs()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_FROM=oleg-voronkovich@yandex.ru');
putenv('MAILER_FROM_NAME=Oleg Voronkovich');
putenv('MAILER_SENDER=no-reply@popartdesign.ru');
$_SERVER['MAILER_FROM'] = 'oleg-voronkovich@yandex.ru';
$_SERVER['MAILER_FROM_NAME'] = 'Oleg Voronkovich';
$_SERVER['MAILER_SENDER'] = 'no-reply@popartdesign.ru';

$configurator->configure($mailer);

Expand All @@ -94,12 +95,12 @@ public function testConfiguresDkimUsingEnvs()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_DKIM_PRIVATE=/tmp/private.key');
putenv('MAILER_DKIM_PRIVATE_STRING=private');
putenv('MAILER_DKIM_PASSPHRASE=secret');
putenv('MAILER_DKIM_SELECTOR=mailer');
putenv('MAILER_DKIM_DOMAIN=popartdesign.ru');
putenv('MAILER_DKIM_IDENTITY=no-reply@popartdesign.ru');
$_SERVER['MAILER_DKIM_PRIVATE'] = '/tmp/private.key';
$_SERVER['MAILER_DKIM_PRIVATE_STRING'] = 'private';
$_SERVER['MAILER_DKIM_PASSPHRASE'] = 'secret';
$_SERVER['MAILER_DKIM_SELECTOR'] = 'mailer';
$_SERVER['MAILER_DKIM_DOMAIN'] = 'popartdesign.ru';
$_SERVER['MAILER_DKIM_IDENTITY'] = 'no-reply@popartdesign.ru';

$configurator->configure($mailer);

Expand All @@ -118,10 +119,10 @@ public function testConfiguresDkimDomainAndIdentityWithDefaultValues()

$mailer->From = 'no-reply@popartdesign.ru';

putenv('MAILER_DKIM_PRIVATE=/tmp/private.key');
putenv('MAILER_DKIM_PRIVATE_STRING=private');
putenv('MAILER_DKIM_PASSPHRASE=secret');
putenv('MAILER_DKIM_SELECTOR=mailer');
$_SERVER['MAILER_DKIM_PRIVATE'] = '/tmp/private.key';
$_SERVER['MAILER_DKIM_PRIVATE_STRING'] = 'private';
$_SERVER['MAILER_DKIM_PASSPHRASE'] = 'secret';
$_SERVER['MAILER_DKIM_SELECTOR'] = 'mailer';

$configurator->configure($mailer);

Expand Down

0 comments on commit 827e653

Please sign in to comment.