1

I'm getting that warning in my log since updating to Craft4. I've traced it to a problem with my config/contact-form.php. This is a modified version of the suggested code for Dynamically adding email recipients. With help from the SE community I came up with this as a way to use the contact form but not reveal any email addresses in the public html code. Instead of using the email address in the submission it sends the 'contactKey' data field. Then in the config file it does an element query to get all the contacts section entries and then matches up the 'contactKey' field with the 'contactEmail' field and then passes that to $toEmail.

It still works, but I'm concerned that it may cause other issues and ideally should be fixed. But I lack the programming skills to work out what needs to change to prevent the query happening before Craft is initialised. There is a GitHub thread about this issue here.

Here is my cofig/contact-form.php code:

<?php
$request = Craft::$app->request;
if (!$request->getIsConsoleRequest()) {
    $toEmailId = $request->getBodyParam('toEmail');

$criteria = \craft\elements\Entry::find()
    ->section('contacts')
    ->all();

$allAddresses = [];
foreach($criteria as $entry){
    $allAddresses[$entry->contactKey] = $entry->contactEmail;
}
$toEmail = (array_key_exists($toEmailId, $allAddresses))? $allAddresses[$toEmailId] : null;
return [
    'toEmail' => ($toEmail ?: '[email protected]'),
    'prependSubject'      => 'Website:',
    'prependSender'       => '',
    'allowAttachments'    => false,
    'successFlashMessage' => 'Thanks for your message'
];
}

The public web page contact form has this in it:

<form method="post" action="" accept-charset="UTF-8">
        {{ csrfInput() }}
        {{ actionInput('contact-form/send') }}
        <input type="hidden" name="toEmail" value="{{ entry.contactKey }}">

1 Answer 1

1

You could move the logic to overwrite the recipient email out of the config and into a hook. This ensures that the element query is only executed later in the lifecycle, which avoids the warning you're seeing in the logs. It also avoids inefficiently running the query during every request.

First, set a reasonable default recipient e-mail – in your case [email protected] – in the config/contact-form.php so the settings validate (otherwise, the submission will be cancelled before any event is fired). Then use the beforeSend event to modify the recipient emails right before the e-mail is sent:

use craft\contactform\events\SendEvent;
use craft\contactform\Mailer;
use yii\base\Event;

// ...

Event::on(Mailer::class, Mailer::EVENT_BEFORE_SEND, function(SendEvent $event) {
    // your custom logic to find the correct recipient goes here
    $recipient = '…';

    // this must be an array of strings
    $event->toMails = [$recipient];
});

You can put this code in a custom module.

4
  • Thanks for that. I've created a module following the documents, but must have something wrong as it's not working. The email form submits fine and the email comes through, but it went to the default address in the config file, not overridden by the one from the entry. You can see my code in this gist link
    – Paul Frost
    Oct 21, 2022 at 17:23
  • I've now updated the code in the gist after a lot or trial and error. But I'm getting "Setting unknown property: craft\contactform\events\SendEvent::toEmail" when I send a message. link to gist
    – Paul Frost
    Oct 22, 2022 at 13:07
  • @PaulFrost Ah my bad, looks like SendEvent::toEmails (also note the s at the end) only allows an array of strings, not a single string. I've corrected the example code in my answer, setting $event->toMails = [$recipient] should work!
    – MoritzLost
    Oct 24, 2022 at 7:28
  • 1
    Brilliant, it's working now. Thanks.
    – Paul Frost
    Oct 24, 2022 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.