From 31650e55c2a33ae9c79c5728978e21db65d81c92 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Tue, 11 Nov 2025 02:22:01 -0800 Subject: [PATCH] better config setup --- config/app.example.php | 11 +++--- src/Mailer/ContactUsFormSubmissionsMailer.php | 16 +++++++- .../Table/ContactUsFormSubmissionsTable.php | 4 +- ...ContactUsFormSubmissionsControllerTest.php | 37 ++++++++++++------- tests/bootstrap.php | 11 ++++-- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/config/app.example.php b/config/app.example.php index 46175fd..6333e65 100644 --- a/config/app.example.php +++ b/config/app.example.php @@ -8,16 +8,15 @@ return [ 'email' => true, 'captcha' => true, ], - 'sendConfirmationEmail' => true, - 'sendBackendEmail' => true, + 'clientIpHeader' => '', 'email' => [ 'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions', - 'confirmation' => [ - 'enabled' => true, - ], - 'backend' => [ + 'confirmation' => true, // true or false + 'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays 'enabled' => true, 'to' => 'bshipley@hipowered.dev', + 'cc' => '', // email string or array of emails + 'bcc' => '', // email string or array of emails ], ], 'addIdToRedirect' => true, diff --git a/src/Mailer/ContactUsFormSubmissionsMailer.php b/src/Mailer/ContactUsFormSubmissionsMailer.php index 9e82681..9187278 100644 --- a/src/Mailer/ContactUsFormSubmissionsMailer.php +++ b/src/Mailer/ContactUsFormSubmissionsMailer.php @@ -47,14 +47,28 @@ class ContactUsFormSubmissionsMailer extends Mailer $subject = __d('cake_contact_us', 'Contact Us Form Submitted'); $name = isset($contactUsFormSubmission['name']) ? ' by ' . $contactUsFormSubmission['name'] : ''; + $to = Configure::readOrFail('ContactUs.email.backend.to'); + $cc = Configure::read('ContactUs.email.backend.cc', []); + $bcc = Configure::read('ContactUs.email.backend.bcc', []); + $to = !is_array($to) ? [$to] : $to; + $this - ->setTo(Configure::readOrFail('ContactUs.fields.email.backend.to')) + ->setTo($to) ->setSubject($subject . $name) ->setEmailFormat(Message::MESSAGE_BOTH) ->setViewVars([ 'contactUsFormSubmission' => $contactUsFormSubmission, ]); + if ($cc) { + $cc = !is_array($cc) ? [$cc] : $cc; + $this->setCc($to); + } + if ($bcc) { + $bcc = !is_array($bcc) ? [$bcc] : $bcc; + $this->setBcc($bcc); + } + $this->viewBuilder() ->setTemplate('CakeContactUs.backend'); } diff --git a/src/Model/Table/ContactUsFormSubmissionsTable.php b/src/Model/Table/ContactUsFormSubmissionsTable.php index 03f2743..41490ad 100644 --- a/src/Model/Table/ContactUsFormSubmissionsTable.php +++ b/src/Model/Table/ContactUsFormSubmissionsTable.php @@ -128,7 +128,7 @@ class ContactUsFormSubmissionsTable extends Table } $now = DateTime::now()->format(CakeContactUsPlugin::CAKE_CONTACT_US_MYSQL_DATETIME); $updateData = []; - if (Configure::read('ContactUs.fields.email.confirmation') && isset($contactUsFormSubmission->email)) { + if (Configure::read('ContactUs.email.confirmation', false) && isset($contactUsFormSubmission->email)) { $mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions'); try { $this->getMailer($mailer)->send('confirmation', [$contactUsFormSubmission]); @@ -137,7 +137,7 @@ class ContactUsFormSubmissionsTable extends Table } } - if (Configure::readOrFail('ContactUs.fields.email.backend')) { + if (Configure::read('ContactUs.email.backend.enabled', false)) { $mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions'); try { $this->getMailer($mailer)->send('backend', [$contactUsFormSubmission]); diff --git a/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php b/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php index 19ec8e6..6615145 100644 --- a/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php +++ b/tests/TestCase/Controller/ContactUsFormSubmissionsControllerTest.php @@ -142,11 +142,14 @@ class ContactUsFormSubmissionsControllerTest extends TestCase Configure::write('ContactUs', [ 'fields' => [ 'captcha' => false, - 'email' => [ - 'confirmation' => true, - 'backend' => [ - 'to' => 'test@example.com', - ], + 'email' => true, + ], + 'email' => [ + 'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions', + 'confirmation' => true, // true or false + 'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays + 'enabled' => true, + 'to' => 'test@example.com', ], ], ]); @@ -193,9 +196,14 @@ class ContactUsFormSubmissionsControllerTest extends TestCase Configure::write('ContactUs', [ 'fields' => [ 'captcha' => false, - 'email' => [ - 'confirmation' => true, - 'backend' => false, + 'email' => true, + ], + 'email' => [ + 'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions', + 'confirmation' => true, // true or false + 'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays + 'enabled' => false, + 'to' => 'test@example.com', ], ], ]); @@ -236,11 +244,14 @@ class ContactUsFormSubmissionsControllerTest extends TestCase Configure::write('ContactUs', [ 'fields' => [ 'captcha' => false, - 'email' => [ - 'confirmation' => false, - 'backend' => [ - 'to' => 'test@example.com', - ], + 'email' => true, + ], + 'email' => [ + 'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions', + 'confirmation' => false, // true or false + 'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays + 'enabled' => true, + 'to' => 'test@example.com', ], ], ]); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 119c9d3..e83db15 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -63,9 +63,14 @@ Configure::write('debug', true); Configure::write('ContactUs', [ 'fields' => [ 'captcha' => false, - 'email' => [ - 'confirmation' => false, - 'backend' => false, + 'email' => true, + ], + 'email' => [ + 'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions', + 'confirmation' => false, // true or false + 'backend' => [ // array with enabled and the to/cc/bcc/ fields as strings or arrays + 'enabled' => false, + 'to' => 'test@example.com', ], ], ]);