<p>The problem is that this config file is for console commands as well as web requests. Console commands differ from web requests in that they don't have body parameters. For web requests, <code>Craft::$app-&gt;request</code> is an instance of <a href="https://docs.craftcms.com/api/v3/craft-web-request.html" rel="nofollow noreferrer"><code>craft\web\Request</code></a>. But for console commands, it's an instance of <a href="https://docs.craftcms.com/api/v3/craft-console-request.html" rel="nofollow noreferrer"><code>craft\console\Request</code></a>. This one doesn't have the <code>getBodyParam</code> method, so you get this error.</p>
<p>The solution is to check if the current request is a console request and don't execute the rest of the code if it is:</p>
<pre class="lang-php prettyprint-override"><code>$request = Craft::$app-&gt;request;
if (!$request-&gt;getIsConsoleRequest()) {
    $toEmailId = $request-&gt;getBodyParam('toEmail');
    // the rest of the configuration code goes here
}
</code></pre>
<p>You can see something similar in the <a href="https://github.com/craftcms/contact-form#overriding-plugin-settings" rel="nofollow noreferrer">documentation on overriding plugin settings</a> for the contact-form plugin.</p>
<h3>Update: Complete working code with added condition</h3>
<pre class="lang-php prettyprint-override"><code>&lt;?php
$request = Craft::$app-&gt;request;
if (!$request-&gt;getIsConsoleRequest()) {
    $toEmailId = $request-&gt;getBodyParam('toEmail');

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

    $allAddresses = [];
    foreach($criteria as $entry) {
        $allAddresses[$entry-&gt;contactKey] = $entry-&gt;contactEmail;
    }
    $toEmail = (array_key_exists($toEmailId, $allAddresses))? $allAddresses[$toEmailId] : null;
    return [
        'toEmail' =&gt; ($toEmail ?: null),
        'prependSubject'      =&gt; '',
        'prependSender'       =&gt; 'Orienteering website:',
        'allowAttachments'    =&gt; false,
        'successFlashMessage' =&gt; 'Thanks for your message'
    ];
}
</code></pre>

CC BY-SA 4.0
2021-12-07T18:22:05.487Z
Thanks for the help, but it's not working. I'm getting a Parse error "syntax error, unexpected end of file", you can see the result here: https://sos.pfweb.co.uk/contacts
I've added the updated code above.
Paul Frost
CC BY-SA 4.0
2021-12-08T08:48:31.230Z
@PaulFrost You're missing the last closing bracket (`}`) at the end. I've updated my answer with the full corrected code snippet.
MoritzLost
CC BY-SA 4.0
2021-12-08T11:17:22.257Z
Thanks so much for this help, it's all working now. I love Craft and what I can build out of the box, but these little tweaks that need php skills that I don't have just frustrate me so much.
Paul Frost
CC BY-SA 4.0
2021-12-07T11:19:55.017Z
2021-12-08T08:48:15.163Z
2021-12-08T08:48:15.163Z
MoritzLost
<p>After updating to 3.7.19 I ran the command as instructed in the changelog:</p>
<pre><code>php craft utils/repair/section-structure &lt;sectionHandle&gt;
</code></pre>
<p>But it returned the following:</p>
<pre><code>Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: craft\console\Request::getBodyParam()'

in /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/Component.php:301

Stack trace:
#0 /home/pfwebco/sos/sos_craft/config/contact-form.php(3): yii\base\Component-&gt;__call('getBodyParam', Array)
#1 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Config.php(205): include('/home/pfwebco/s...')
#2 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Plugins.php(967): craft\services\Config-&gt;getConfigFromFile('contact-form')
#3 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Plugins.php(233): craft\services\Plugins-&gt;createPlugin('contact-form', Array)
#4 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/base/ApplicationTrait.php(1479): craft\services\Plugins-&gt;loadPlugins()
#5 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/console/Application.php(48): craft\console\Application-&gt;_postInit()
#6 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\console\Application-&gt;init()
#7 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/Application.php(212): yii\base\BaseObject-&gt;__construct(Array)
#8 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/console/Application.php(90): yii\base\Application-&gt;__construct(Array)
#9 [internal function]: yii\console\Application-&gt;__construct(Array)
#10 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/di/Container.php(420): ReflectionClass-&gt;newInstanceArgs(Array)
#11 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/di/Container.php(171): yii\di\Container-&gt;build('craft\\console\\A...', Array, Array)
#12 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container-&gt;get('craft\\console\\A...', Array, Array)
#13 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/Craft.php(67): yii\BaseYii::createObject(Array, Array)
#14 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/bootstrap/bootstrap.php(242): Craft::createObject(Array)
#15 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/bootstrap/console.php(51): require('/home/pfwebco/s...')
#16 /home/pfwebco/sos/sos_craft/craft(22): require('/home/pfwebco/s...')
#17 {main}
</code></pre>
<p>Although I got that error, would it have done the command and repaired the table and how can I check?</p>
<p>It mentions the contact-form several times, so I'm wondering if there is something I need to edit in the config contact-form following updates. Or is there something else I'm missing?
Below is the code in that form.</p>
<pre><code>&lt;?php
$request = Craft::$app-&gt;request;
$toEmailId = $request-&gt;getBodyParam('toEmail');

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

$allAddresses = [];
foreach($criteria as $entry){
    $allAddresses[$entry-&gt;contactKey] = $entry-&gt;contactEmail;
}
$toEmail = (array_key_exists($toEmailId, $allAddresses))? $allAddresses[$toEmailId] : null;
return [
    'toEmail' =&gt; ($toEmail ?: null),
    'prependSubject'      =&gt; '',
    'prependSender'       =&gt; 'Orienteering website:',
    'allowAttachments'    =&gt; false,
    'successFlashMessage' =&gt; 'Thanks for your message'
];
</code></pre>
<p>The submit button on the public page sends the data field 'contactKey' as the 'toEmail' value. Then the config contact-form gets all the 'contacts' section entries and then matches the contactKey to the contactEmail which is then passed to the toEmail field.
This is so that the email address is not visible in the public html and so hidden from spammers.</p>
<p>That code came from a <a href="https://craftcms.stackexchange.com/questions/23238/how-to-encrypt-or-hide-variable-dynamic-email-address-in-contact-form">previous post</a> answered by <a href="https://craftcms.stackexchange.com/users/5557/robin-schambach">Robin Schambach</a></p>
<p>This is how the config/contact-form.php looks after adding the suggested code:</p>
<pre><code>&lt;?php
$request = Craft::$app-&gt;request;
if (!$request-&gt;getIsConsoleRequest()) {
    $toEmailId = $request-&gt;getBodyParam('toEmail');
    // the rest of the configuration code goes here
    
$criteria = \craft\elements\Entry::find()
    -&gt;section('contacts')
    -&gt;all();

$allAddresses = [];
foreach($criteria as $entry){
    $allAddresses[$entry-&gt;contactKey] = $entry-&gt;contactEmail;
}
$toEmail = (array_key_exists($toEmailId, $allAddresses))? $allAddresses[$toEmailId] : null;
return [
    'toEmail' =&gt; ($toEmail ?: null),
    'prependSubject'      =&gt; '',
    'prependSender'       =&gt; 'Orienteering website:',
    'allowAttachments'    =&gt; false,
    'successFlashMessage' =&gt; 'Thanks for your message'
];
</code></pre>

<error-message><plugin-contactform><updates><commands>
Exception 'yii\base\UnknownMethodException'
# Exception 'yii\base\UnknownMethodException'

(Asked by: username_0 on 2021-12-06)

After updating to 3.7.19 I ran the command as instructed in the changelog:
php craft utils/repair/section-structure 

But it returned the following:
Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: craft\console\Request::getBodyParam()'

in /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/Component.php:301

Stack trace:
#0 /home/pfwebco/sos/sos_craft/config/contact-form.php(3): yii\base\Component->__call('getBodyParam', Array)
#1 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Config.php(205): include('/home/pfwebco/s...')
#2 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Plugins.php(967): craft\services\Config->getConfigFromFile('contact-form')
#3 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Plugins.php(233): craft\services\Plugins->createPlugin('contact-form', Array)
#4 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/base/ApplicationTrait.php(1479): craft\services\Plugins->loadPlugins()
#5 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/console/Application.php(48): craft\console\Application->_postInit()
#6 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\console\Application->init()
#7 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/Application.php(212): yii\base\BaseObject->__construct(Array)
#8 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/console/Application.php(90): yii\base\Application->__construct(Array)
#9 [internal function]: yii\console\Application->__construct(Array)
#10 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/di/Container.php(420): ReflectionClass->newInstanceArgs(Array)
#11 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/di/Container.php(171): yii\di\Container->build('craft\\console\\A...', Array, Array)
#12 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\console\\A...', Array, Array)
#13 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/Craft.php(67): yii\BaseYii::createObject(Array, Array)
#14 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/bootstrap/bootstrap.php(242): Craft::createObject(Array)
#15 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/bootstrap/console.php(51): require('/home/pfwebco/s...')
#16 /home/pfwebco/sos/sos_craft/craft(22): require('/home/pfwebco/s...')
#17 {main}

Although I got that error, would it have done the command and repaired the table and how can I check?
It mentions the contact-form several times, so I'm wondering if there is something I need to edit in the config contact-form following updates. Or is there something else I'm missing?
Below is the code in that form.
request;
$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 ?: null),
    'prependSubject'      => '',
    'prependSender'       => 'Orienteering website:',
    'allowAttachments'    => false,
    'successFlashMessage' => 'Thanks for your message'
];

The submit button on the public page sends the data field 'contactKey' as the 'toEmail' value. Then the config contact-form gets all the 'contacts' section entries and then matches the contactKey to the contactEmail which is then passed to the toEmail field.
This is so that the email address is not visible in the public html and so hidden from spammers.
That code came from a previous post answered by Robin Schambach
This is how the config/contact-form.php looks after adding the suggested code:
request;
if (!$request->getIsConsoleRequest()) {
    $toEmailId = $request->getBodyParam('toEmail');
    // the rest of the configuration code goes here
    
$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 ?: null),
    'prependSubject'      => '',
    'prependSender'       => 'Orienteering website:',
    'allowAttachments'    => false,
    'successFlashMessage' => 'Thanks for your message'
];

**Tags:** error-message, plugin-contactform, updates, commands

---

## 1 answers

### Answer by username_1 on 2021-12-07. Score: 2 | Accepted answer

The problem is that this config file is for console commands as well as web requests. Console commands differ from web requests in that they don't have body parameters. For web requests, Craft::$app->request is an instance of craft\web\Request. But for console commands, it's an instance of craft\console\Request. This one doesn't have the getBodyParam method, so you get this error.
The solution is to check if the current request is a console request and don't execute the rest of the code if it is:
$request = Craft::$app->request;
if (!$request->getIsConsoleRequest()) {
    $toEmailId = $request->getBodyParam('toEmail');
    // the rest of the configuration code goes here
}

You can see something similar in the documentation on overriding plugin settings for the contact-form plugin.
Update: Complete working code with added condition
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 ?: null),
        'prependSubject'      => '',
        'prependSender'       => 'Orienteering website:',
        'allowAttachments'    => false,
        'successFlashMessage' => 'Thanks for your message'
    ];
}

> **Comments:**
> **username_0**: Thanks for the help, but it's not working. I'm getting a Parse error "syntax error, unexpected end of file", you can see the result here: https://sos.pfweb.co.uk/contacts I've added the updated code above.  
> **username_1**: @PaulFrost You're missing the last closing bracket (`}`) at the end. I've updated my answer with the full corrected code snippet.  
> **username_0**: Thanks so much for this help, it's all working now. I love Craft and what I can build out of the box, but these little tweaks that need php skills that I don't have just frustrate me so much.  

---

