<p>As <a href="https://github.com/craftcms/contact-form?tab=readme-ov-file#dynamically-adding-email-recipients" rel="nofollow noreferrer">described in the documentation</a> for Contact Form, in addition to the hidden <code>name=&quot;toEmail&quot;</code> input in your form, you'll need to add some logic to your <code>config/contact-form.php</code> file:</p>
<pre class="lang-php prettyprint-override"><code>&lt;?php

// Contact Form config settings
$config = [
    ...
];

// Logic for adding the dynamic email recipient
$request = Craft::$app-&gt;request;

if (
    !$request-&gt;getIsConsoleRequest() &amp;&amp;
    ($toEmail = $request-&gt;getValidatedBodyParam('toEmail')) !== null
) {
    $config['toEmail'] = $toEmail;
}

return $config;
</code></pre>

CC BY-SA 4.0
2024-12-07T14:03:38.203Z
I don't think this addresses the part about the email coming from the entry field?
triplethreat23
CC BY-SA 4.0
2024-12-08T15:08:45.987Z
No, because I didn't consider that part a problem – as long as the `recipient` variable contains an email address and you've added the necessary code to your `config/contact-form.php` as per the above, the way you've set this up should work. If it doesn't, you'll have to be more specific – do you get any error messages? What kind of field is `entry. recipientEmail`?
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2024-12-09T15:59:58.303Z
No specific error. The email just arrives at the fallback in the CP. I've tried it as a text field and a email field.
triplethreat23
CC BY-SA 4.0
2024-12-09T22:59:09.177Z
Hard to say what's wrong, then. The `recipient` variable value should just be a (hashed) string with an email address, so the way you're doing it should work. Maybe try adding a `\Craft::dd($toEmail);` inside the `if` statement in the config file to confirm that the recipient email address does end up in the Contact Form config.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2024-12-07T10:36:20.913Z
2024-12-07T10:36:20.913Z
Mats Mikkel Rummelhoff
<p>Is it possible to set a recipients email address via a custom field with the Contact Form plugin?</p>
<p>I have tried using
{% set recipient = entry.recipientEmail %}</p>
<p>And then hashing the recipient variable like:</p>
<pre><code>&lt;input type=&quot;hidden&quot; name=&quot;toEmail&quot; value=&quot;{{ recipient|hash }}&quot;&gt;
</code></pre>
<p>But it doesn't work.</p>

<plugin-contactform>
Custom field value as toEmail with CraftCMS Contact Form
# Custom field value as toEmail with CraftCMS Contact Form

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

Is it possible to set a recipients email address via a custom field with the Contact Form plugin?
I have tried using
{% set recipient = entry.recipientEmail %}
And then hashing the recipient variable like:


But it doesn't work.

**Tags:** plugin-contactform

---

## 1 answers

### Answer by username_1 on 2024-12-07. Score: 0

As described in the documentation for Contact Form, in addition to the hidden name="toEmail" input in your form, you'll need to add some logic to your config/contact-form.php file:
request;

if (
    !$request->getIsConsoleRequest() &&
    ($toEmail = $request->getValidatedBodyParam('toEmail')) !== null
) {
    $config['toEmail'] = $toEmail;
}

return $config;

> **Comments:**
> **username_0**: I don't think this addresses the part about the email coming from the entry field?  
> **username_1**: No, because I didn't consider that part a problem – as long as the `recipient` variable contains an email address and you've added the necessary code to your `config/contact-form.php` as per the above, the way you've set this up should work. If it doesn't, you'll have to be more specific – do you get any error messages? What kind of field is `entry. recipientEmail`?  
> **username_0**: No specific error. The email just arrives at the fallback in the CP. I've tried it as a text field and a email field.  
> **username_1**: Hard to say what's wrong, then. The `recipient` variable value should just be a (hashed) string with an email address, so the way you're doing it should work. Maybe try adding a `\Craft::dd($toEmail);` inside the `if` statement in the config file to confirm that the recipient email address does end up in the Contact Form config.  

---

