From 9035e2e126d7baa9295514f2afac494dd8202760 Mon Sep 17 00:00:00 2001 From: bencroker Date: Thu, 31 Oct 2024 15:14:51 +0100 Subject: [PATCH] Accept env variables in from names and emails --- src/Campaign.php | 4 ++-- src/helpers/SettingsHelper.php | 10 +++++----- src/models/SettingsModel.php | 26 ++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Campaign.php b/src/Campaign.php index 4fe349ab..d280383c 100644 --- a/src/Campaign.php +++ b/src/Campaign.php @@ -401,8 +401,8 @@ protected function createSettingsModel(): SettingsModel $settings->apiKey = StringHelper::randomString(16); $settings->fromNamesEmails = [ [ - App::parseEnv($mailSettings->fromName), - App::parseEnv($mailSettings->fromEmail), + $mailSettings->fromName, + $mailSettings->fromEmail, '', Craft::$app->getSites()->getPrimarySite()->id, ], diff --git a/src/helpers/SettingsHelper.php b/src/helpers/SettingsHelper.php index 3a2b0535..5aad9dcb 100755 --- a/src/helpers/SettingsHelper.php +++ b/src/helpers/SettingsHelper.php @@ -38,7 +38,7 @@ public static function getFromNameEmail(int $siteId = null): array { // Get first from name and email $firstFromNameEmail = []; - $fromNamesEmails = Campaign::$plugin->settings->fromNamesEmails; + $fromNamesEmails = Campaign::$plugin->settings->getFromNamesEmails(); foreach ($fromNamesEmails as $fromNameEmail) { if ($siteId === null || empty($fromNameEmail[3]) || $fromNameEmail[3] == $siteId) { @@ -57,9 +57,9 @@ public static function getFromNameEmail(int $siteId = null): array $mailSettings = App::mailSettings(); $firstFromNameEmail = [ - 'name' => $mailSettings->fromName, - 'email' => $mailSettings->fromEmail, - 'replyTo' => '', + 'name' => App::parseEnv($mailSettings->fromName), + 'email' => App::parseEnv($mailSettings->fromEmail), + 'replyTo' => App::parseEnv($mailSettings->replyToEmail), ]; } @@ -72,7 +72,7 @@ public static function getFromNameEmail(int $siteId = null): array public static function getFromNameEmailOptions(int $siteId = null): array { $fromNameEmailOptions = []; - $fromNamesEmails = Campaign::$plugin->settings->fromNamesEmails; + $fromNamesEmails = Campaign::$plugin->settings->getFromNamesEmails(); foreach ($fromNamesEmails as $fromNameEmail) { $fromSiteId = $fromNameEmail[3] ?? null; diff --git a/src/models/SettingsModel.php b/src/models/SettingsModel.php index 25ea97d8..e467366f 100644 --- a/src/models/SettingsModel.php +++ b/src/models/SettingsModel.php @@ -453,6 +453,26 @@ public function getTurnstileErrorMessage(): string return App::parseEnv($this->turnstileErrorMessage) ?? ''; } + /** + * Returns the parsed from names and emails. + * + * @since 3.5.6 + */ + public function getFromNamesEmails(): array + { + $fromNamesEmails = []; + foreach ($this->fromNamesEmails as $fromNameEmail) { + $fromNamesEmails[] = [ + App::parseEnv($fromNameEmail[0]) ?? '', + App::parseEnv($fromNameEmail[1]) ?? '', + App::parseEnv($fromNameEmail[2]) ?? '', + $fromNameEmail[3] ?? null, + ]; + } + + return $fromNamesEmails; + } + /** * Returns the contact field layout. */ @@ -505,12 +525,14 @@ public function getContactFields(): array */ public function validateFromNamesEmails(mixed $attribute): void { - if (empty($this->fromNamesEmails)) { + $fromNamesEmails = $this->getFromNamesEmails(); + + if (empty($fromNamesEmails)) { $this->addError($attribute, Craft::t('campaign', 'You must enter at least one name and email.')); return; } - foreach ($this->fromNamesEmails as $fromNameEmail) { + foreach ($fromNamesEmails as $fromNameEmail) { if ($fromNameEmail[0] === '' || $fromNameEmail[1] === '') { $this->addError($attribute, Craft::t('campaign', 'The name and email cannot be blank.')); return;