# Dinamic fields on emailTo with formit

**URL:** <https://community.modx.com/t/dinamic-fields-on-emailto-with-formit/4169>\
**Category:** Development\
**Tags:** formit\
**Created:** [June 18, 2021, 11:31pm UTC](https://community.modx.com/t/dinamic-fields-on-emailto-with-formit/4169 "2021-06-18T23:31:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![camicase82](https://community.modx.com/user_avatar/community.modx.com/camicase82/32/2033_2.png) [@camicase82](https://community.modx.com/u/camicase82)\
**Post date:** [June 18, 2021, 11:31pm UTC](https://community.modx.com/t/dinamic-fields-on-emailto-with-formit/4169/1 "2021-06-18T23:31:00Z")

</div>

Hi there, I have a dynamic form where people can submit up to 5 email addresses, I’m storing those addresses both as a placeholder and as a hook value, but the email plugin keeps throwing “An error occurred while trying to send the email. You must provide at least one recipient email address.”

But if I just add the addresses as text in the emailTo param, it sends the mails as expected

My formit declaration is as follows

```
 <p>mails! [[+bartendersEmails]] </p>
 [[!FormIt?
    &hooks=`enrollBartendersFiHook,FormItAutoResponder,email,FormItSaveForm,redirect`
    &emailTpl=`formSingleBartender.tpl`
    &emailTo=`[[+bartendersEmails]]`
    &emailSubject=`[[++site_name]]`
    
    &fiarTpl=`fiarFormSingleBartender.tpl`
    &fiarToField=`detailsEmail`
    &fiarSubject=`[[++site_name]]`
    
    &redirectTo=`48`
    &redirectParams=`{"form":"[[*id]]sngl"}`
    &submitVar=`sBart`
    &customValidators=`customEmailHashValidate`
    &formName=`Single Bartenders`
 ]]

```

The mails are listed correctly on the paragraph, yet the mail won’t be sent

The last lines of the enrollBartendersFiHook hook are:

> $modx-\>log(modX::LOG\_LEVEL\_ERROR, "Lists are:\n $bartendersList \n$bartendersEmails ");
> 
> $hook-\>setValue(‘bartendersList’, $bartendersList);  
> $hook-\>setValue(‘bartendersEmails’, $bartendersEmails);
> 
> $modx-\>setPlaceholder(‘bartendersList’, $bartendersList);  
> $modx-\>setPlaceholder(‘bartendersEmails’, $bartendersEmails);
> 
> return true;

And in the MODx error log I’m getting the expected values.

Also checked [this](https://forums.modx.com/thread/83927/formit---dynamically-selected-email-recipients-based-on-user-input-with-checkboxes---emails-not-sent) but not getting the mails sent,  
and bascially added:

`<textarea cols="50" rows="4" id="addressto_all" name="addressto_all" value="[[+bartendersEmails]]" class="hidden"></textarea>`

and updated the email field to emailTo=`[[+addressTo\_all]]

But I’m still getting the same error and no emails are sent, what could I be missing?

**UPDATE:**

From this [thread](https://forums.modx.com/thread/101101/formit-emailto-set-through-hook-with-multiple-values-does-not-work) I added

`$hook->formit->options['emailTo'] = $bartendersEmails;`

and the new error is:

```
[FormIt] An error occurred while trying to send
                      the auto-responder email: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

```

Check the big space, like something was missing, we are in modx 2.8.1

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [June 19, 2021, 11:28am UTC](https://community.modx.com/t/dinamic-fields-on-emailto-with-formit/4169/2 "2021-06-19T11:28:53Z")

</div>

I think you have to use

```auto
$hook->formit->config['emailTo'] = $bartendersEmails;

```

in your hook to set the `emailTo` value. (Use `config` NOT `options`!)

* * *

In general: You can’t use placeholders in the properties of the call to `FormIt`, if these placeholders are set by the `FormIt` snippet itself. You try to use the value of a placeholder before it is set!

* * *

This error message

```auto
An error occurred while trying to send the auto-responder email: SMTP connect() failed.

```

is from the `FormItAutoResponder` hook. As this hook doesn’t use the `emailTo` value, I’m not sure how the error is connected to the changes you made in the hook.

---

<div class="post-metadata">

**Author:** ![camicase82](https://community.modx.com/user_avatar/community.modx.com/camicase82/32/2033_2.png) [@camicase82](https://community.modx.com/u/camicase82)\
**Post date:** [June 21, 2021, 2:24pm UTC](https://community.modx.com/t/dinamic-fields-on-emailto-with-formit/4169/3 "2021-06-21T14:24:12Z")

</div>

> [@halftrainedharry](#):
>
> `config`

Thanks a lot, changing ‘options’ to ‘config’ did the trick!
