Closed jamiematrix closed 2 years ago
May have found a fix, hoping it's a usable one.
Here's my edited contact-form.php
:
<?php
$config = [];
$request = Craft::$app->request;
// Get the 'toEmail' field from POST
$toEmailField = $request->getBodyParam('toEmail');
// Check it's not a console request and the field is not null
if ($toEmailField !== null && !$request->getIsConsoleRequest()) {
// Hash the email provided
$toEmailField = Craft::$app->getSecurity()->hashData($toEmailField, null);
// Verify (may not be needed);
$isValid = Craft::$app->getSecurity()->validateData($toEmailField);
}
// If not a console request, the 'toEmail' field is not null and the field has ben validated
if (
!$request->getIsConsoleRequest() &&
$toEmailField !== null &&
$isValid !== null
) {
// set the 'toEmail' config to the new hashed email
$config['toEmail'] = $toEmailField;
} elseif (
!$request->getIsConsoleRequest() &&
($toEmail = $request->getValidatedBodyParam('toEmail')) !== null
) {
$config['toEmail'] = $toEmail;
}
return $config;
Seems like I was massively over complicating the form.
Ended up with:
<?php
$config = [];
$request = Craft::$app->request;
// Get the 'toEmail' field from POST
$toEmail = $request->getBodyParam('toEmail');
// Check it's not a console request and the field is not null
if ($toEmail !== null && !$request->getIsConsoleRequest()) {
$config['toEmail'] = $toEmail;
}
return $config;
Which appears to be working so far
Is there a way to have dynamic 'toEmail' address (or address') for a form?
Use case is a kind of 'refer a friend' email where someone fills a form in with their name and email along with a friends name and email. I'm using the
contact-form.php
file to change the toEmail address, but I get the error ofRequest contained an invalid body param
. My guess is because the toEmail element isn't being hashed.Would it be possible to do this as part of the
contact-form.php
config or something using JavaScript?