Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-07T14:03:38.203Z
Answers.Text: I don't think this addresses the part about the email coming from the entry field?
Answers.UserDisplayName: triplethreat23
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-08T15:08:45.987Z
Answers.Text: 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`?
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-09T15:59:58.303Z
Answers.Text: 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.
Answers.UserDisplayName: triplethreat23
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-09T22:59:09.177Z
Answers.Text: 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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-07T10:36:20.913Z
Answers.LastActivityDate: 2024-12-07T10:36:20.913Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <plugin-contactform>
Title: Custom field value as toEmail with CraftCMS Contact Form
ThreadText: # 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:
  
  ```
  <input type="hidden" name="toEmail" value="{{ recipient|hash }}">
  
  ```
  
  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](https://github.com/craftcms/contact-form?tab=readme-ov-file#dynamically-adding-email-recipients) 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:
  
  ```php
  <?php
  
  // Contact Form config settings
  $config = [
      ...
  ];
  
  // Logic for adding the dynamic email recipient
  $request = Craft::$app->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.  
  
  ---
  
  