<p>You can Create a custom array in which you store all valid email addresses and send the id which email to use</p>

<pre><code>&lt;select name="toEmail"&gt;
  &lt;option value="1"&gt;1st name&lt;/option&gt;
  &lt;option value="2"&gt;2nd name&lt;/option&gt;
&lt;/select&gt;
</code></pre>

<p>and in your config file</p>

<pre><code>namespace Craft;

$toEmailId = craft()-&gt;request-&gt;getPost('toEmail');
$yourArray = [
    1 =&gt; 'firstmail.com',
    2 =&gt; 'secondmail.com',
];
$toEmail = (array_key_exists($toEmailId, $yourArray))? $yourArray[$toEmailId]  : null;
return array(
    'toEmail' =&gt; $toEmail,
);
</code></pre>

CC BY-SA 3.0
2017-12-19T14:35:15.337Z
I've tried various combinations but I can't work out where to add your bit of backend code into my config file, which looks like this:
<?php
namespace Craft;
$toEmail = craft()->request->getPost('toEmail');
return array(
    'toEmail' => ($toEmail ?: null),
    'prependSubject'      => 'From website: ',
);

I've tried every permutation I can think of.
Paul Frost
CC BY-SA 3.0
2017-12-19T15:06:08.130Z
Ah, we are back to my limited programming skills again. I've looked at the ContactFormController in the P&T plugin but can't see anything like your code that I can edit. I had hoped there would be a simple plugin to solve what seemed like a common function on a website. But I should know by now that things I think of as normal everyday requirements are unusual and require programming skills outside of html & css.
Paul Frost
CC BY-SA 3.0
2017-12-19T15:07:18.680Z
I edited my answer, it is untested but I thing that should work. I thing this approach might be easier for you. A custom controller/service would have been a little bit better but that works as well
Robin Schambach
CC BY-SA 3.0
2017-12-19T15:18:15.363Z
Initial testing is working, so thanks for that, you have bailed me out again. Now I'll try and understand what's going on in that code.
Paul Frost
CC BY-SA 3.0
2017-12-19T15:22:43.107Z
It's actually pretty easy. `$toEmailId` is the value / the id the requests sends. `$yourArray` is a simple array map, so a simple pair of `key => emails` depending which key the user selects in the frontend the array "converts" it to an email address and returns this value to the service. Depending on how you create your select you can change the `$yourArray` as well. So when you have a set of global configs, you can check this as well.
Robin Schambach
CC BY-SA 3.0
2017-12-19T17:20:21.400Z
Now you have encouraged me to make it better. I have a section named contacts, with fields of contactTitle and contactEmail, so I'm thinking if I can create an array from the entries that I can insert between the square brackets $yourArray = [ inserthere ];   But I can't work out how to convert the entries loop into an array, I have this so far:
{% set entries = craft.entries.section('contacts').find %}
{% for entry in entries %}
'{{ entry.contactTitle }}' => '{{ entry.contactEmail }}',
{% endfor %}
Paul Frost
CC BY-SA 3.0
2017-12-19T17:29:40.527Z
You can't use twig in php files.. you have to fetch the entries in php. See that https://gist.github.com/Anubarak/aefc92c0281aaf9f04f5ae29d4c05c51
Robin Schambach
CC BY-SA 3.0
2017-12-19T13:18:26.433Z
2017-12-19T15:06:41.417Z
2017-12-19T15:06:41.417Z
Robin Schambach
<p>I've updated the information below to work with Craft 3.7</p>
<p>I wanted to allow people to chose which person/department to send a message to using the <a href="https://github.com/craftcms/contact-form" rel="nofollow noreferrer">P&amp;T Contact Form plugin</a>. <a href="https://craftcms.stackexchange.com/questions/19077/how-to-add-recipient-drop-down-selection-field-with-the-contact-form-plugin">I've seen one method to do this</a>, eg:</p>
<pre><code>    &lt;select name=&quot;toEmail&quot;&gt;
          &lt;option value=&quot;{{ '1stname@example.com'|hash }}&quot;&gt;1st name&lt;/option&gt;
          &lt;option value=&quot;{{ '2ndname@example.com'|hash }}&quot;&gt;2nd name&lt;/option&gt;
    &lt;/select&gt;
</code></pre>
<p>but it does mean that the actual email address is visible in the HTML code. Which would allow the spam robots to harvest it. Although it may be that the hash added makes it harder?</p>
<p>In the ideal world I would like the address to be completely hidden or encrypted in the form page html. I wondered if the name=&quot;toEmail&quot; value=&quot; &quot; could be a variable that is passed along with the value=&quot;contactForm/sendMessage&quot; action, that could then be referenced and replaced from a list in the <a href="https://github.com/craftcms/contact-form#overriding-plugin-settings" rel="nofollow noreferrer">contact form config file</a>.</p>
<p>It seems to me that not having plain text email addresses should be the standard way of working so I'm surprised that there isn't already a plugin or method to do this, but I couldn't find one.</p>
<p><strong>Edit to show the final solution I'm using.</strong></p>
<p>I'm very grateful to <a href="https://craftcms.stackexchange.com/users/5557/robin-schambach">Robin Schambach</a>, who provided all the clever code, I've just put it all together here to help anyone looking for a similar solution. The email addresses are never available or visible on the front end.</p>
<p>I created a Section named Contacts with plain text data fields of:</p>
<p>contactName</p>
<p>contactPosition</p>
<p>contactEmail</p>
<p>contactKey (this stores the name in camelCase, so &quot;johnSmith&quot; not &quot;John Smith&quot;) I use the preparse plugin to auto generate this.</p>
<p>This allows the end user to maintain/update the names and addresses of contacts without the developer needing to make changes on the server.</p>
<p>Install <a href="https://github.com/craftcms/contact-form" rel="nofollow noreferrer">P&amp;T Contact form plugin</a> and <a href="https://github.com/craftcms/contact-form#overriding-plugin-settings" rel="nofollow noreferrer">add a config file to overide the plugin settings</a>.
The contents of that file should be:</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; 'From website:',
    'allowAttachments'    =&gt; false,
    'successFlashMessage' =&gt; 'Thanks for your message'
];
}
</code></pre>
<p>On my contacts page that lists all the possible contacts, the submit to the contact form passes the id of the entry:</p>
<pre><code>&lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;{{ entry.id }}&quot;&gt;
</code></pre>
<p>In the contact form template I get that entry:</p>
<pre><code>{% set entry = craft.entries.id(craft.app.request.getBodyParam('id')).one() %}
</code></pre>
<p>Which enables you to be able to show/confirm the contact name/details of who the form will send to and get access to the contactKey.</p>
<pre><code>&lt;p&gt;&lt;strong&gt;You are sending this message to:&lt;/strong&gt;&lt;br&gt;
{{ entry.contactName }}, {{ entry.contactPosition }}&lt;/strong&gt;&lt;/p&gt;

</code></pre>
<p>The contact form template is based on the P&amp;T example. With the addition of this line to allow &quot;Dynamically adding email recipients&quot;, as detailed on the P&amp;T example.</p>
<pre><code>&lt;input type=&quot;hidden&quot; name=&quot;toEmail&quot; value=&quot;{{ entry.contactKey }}&quot;&gt;
</code></pre>
<p>I had problems updating the original method to work with Craft 3.7 and am grateful for the help of <a href="https://craftcms.stackexchange.com/users/11705/moritzlost">MoritzLost</a> in <a href="https://craftcms.stackexchange.com/questions/38736/exception-yii-base-unknownmethodexception">this thread</a> to get it working again.</p>

<emails><plugin-contactform>
How to encrypt or hide variable/dynamic email address in contact form
# How to encrypt or hide variable/dynamic email address in contact form

(Asked by: username_0 on 2017-12-19)

I've updated the information below to work with Craft 3.7
I wanted to allow people to chose which person/department to send a message to using the P&T Contact Form plugin. I've seen one method to do this, eg:
    
          1st name
          2nd name
    

but it does mean that the actual email address is visible in the HTML code. Which would allow the spam robots to harvest it. Although it may be that the hash added makes it harder?
In the ideal world I would like the address to be completely hidden or encrypted in the form page html. I wondered if the name="toEmail" value=" " could be a variable that is passed along with the value="contactForm/sendMessage" action, that could then be referenced and replaced from a list in the contact form config file.
It seems to me that not having plain text email addresses should be the standard way of working so I'm surprised that there isn't already a plugin or method to do this, but I couldn't find one.
Edit to show the final solution I'm using.
I'm very grateful to username_1, who provided all the clever code, I've just put it all together here to help anyone looking for a similar solution. The email addresses are never available or visible on the front end.
I created a Section named Contacts with plain text data fields of:
contactName
contactPosition
contactEmail
contactKey (this stores the name in camelCase, so "johnSmith" not "John Smith") I use the preparse plugin to auto generate this.
This allows the end user to maintain/update the names and addresses of contacts without the developer needing to make changes on the server.
Install P&T Contact form plugin and add a config file to overide the plugin settings.
The contents of that file should be:
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'       => 'From website:',
    'allowAttachments'    => false,
    'successFlashMessage' => 'Thanks for your message'
];
}

On my contacts page that lists all the possible contacts, the submit to the contact form passes the id of the entry:


In the contact form template I get that entry:
{% set entry = craft.entries.id(craft.app.request.getBodyParam('id')).one() %}

Which enables you to be able to show/confirm the contact name/details of who the form will send to and get access to the contactKey.
You are sending this message to:
{{ entry.contactName }}, {{ entry.contactPosition }}


The contact form template is based on the P&T example. With the addition of this line to allow "Dynamically adding email recipients", as detailed on the P&T example.


I had problems updating the original method to work with Craft 3.7 and am grateful for the help of MoritzLost in this thread to get it working again.

**Tags:** emails, plugin-contactform


---

## 1 answers

### Answer by username_1 on 2017-12-19. Score: 2 | Accepted answer

You can Create a custom array in which you store all valid email addresses and send the id which email to use

  1st name
  2nd name


and in your config file
namespace Craft;

$toEmailId = craft()->request->getPost('toEmail');
$yourArray = [
    1 => 'firstmail.com',
    2 => 'secondmail.com',
];
$toEmail = (array_key_exists($toEmailId, $yourArray))? $yourArray[$toEmailId]  : null;
return array(
    'toEmail' => $toEmail,
);

> **Comments:**
> **username_0**: I've tried various combinations but I can't work out where to add your bit of backend code into my config file, which looks like this: <?php namespace Craft; $toEmail = craft()->request->getPost('toEmail'); return array(     'toEmail' => ($toEmail ?: null),     'prependSubject'      => 'From website: ', );  I've tried every permutation I can think of.  
> **username_0**: Ah, we are back to my limited programming skills again. I've looked at the ContactFormController in the P&T plugin but can't see anything like your code that I can edit. I had hoped there would be a simple plugin to solve what seemed like a common function on a website. But I should know by now that things I think of as normal everyday requirements are unusual and require programming skills outside of html & css.  
> **username_1**: I edited my answer, it is untested but I thing that should work. I thing this approach might be easier for you. A custom controller/service would have been a little bit better but that works as well  
> **username_0**: Initial testing is working, so thanks for that, you have bailed me out again. Now I'll try and understand what's going on in that code.  
> **username_1**: It's actually pretty easy. `$toEmailId` is the value / the id the requests sends. `$yourArray` is a simple array map, so a simple pair of `key => emails` depending which key the user selects in the frontend the array "converts" it to an email address and returns this value to the service. Depending on how you create your select you can change the `$yourArray` as well. So when you have a set of global configs, you can check this as well.  
> **username_0**: Now you have encouraged me to make it better. I have a section named contacts, with fields of contactTitle and contactEmail, so I'm thinking if I can create an array from the entries that I can insert between the square brackets $yourArray = [ inserthere ];   But I can't work out how to convert the entries loop into an array, I have this so far: {% set entries = craft.entries.section('contacts').find %} {% for entry in entries %} '{{ entry.contactTitle }}' => '{{ entry.contactEmail }}', {% endfor %}  
> **username_1**: You can't use twig in php files.. you have to fetch the entries in php. See that https://gist.github.com/Anubarak/aefc92c0281aaf9f04f5ae29d4c05c51  

---

