Forums

Dynamically changing email's 'TO' field

ikelemen 31 Aug, 2011
Hi Everyone,

I am new to ChronoForms so I apologize for possibly asking questions about stuff might be obvious for someone else.

I have a Contact Us form on our website. After the form is filled in, I want to send an email to one or more recipients depending on checkboxes ticked in on the form. I tried several different ways to do it, but they all failed. Eventually I found FAQ #31 to be refferred to often as a solution to problems similar to my one. I copied and customized the code, but when I try to use it, server throws Error 500.

Please find the code below. It runs 'On submit' event before the email sending action.


<?php
$adrlist="";
if (isset($_POST["input_cbg_property"])){if (!empty($adrlist)){$adrlist=$adrlist.",";}$adrlist=$adrlist."user1@company.com";}
if (isset($_POST["input_cbg_services"])){if (!empty($adrlist)){$adrlist=$adrlist.",";}$adrlist=$adrlist."user2@company.com";}

//This code is taken from FAQ - START
$MyForm =& CFChronoForm::getInstance('Form_ContactUS');
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
$MyFormEmails->setEmailData(1,'to',$adrlist);
//This code is taken from FAQ - END
?>


If I run the code without the part taken from the FAQ, it works (obviously does not do too much, but no error is thrown). If I add the first line of the code taken from the FAQ, it does not throw any error. However if I put any code referring to $MyForm, it throws error 500. Obviously if I put all the 3 lines from FAQ, it throws error.

I could not get any further, as I am not very experienced PHP user either. What I noticed is that code of 3 lines is often quoted when I use Google, that is the why I do not get why it throws error 500.

Thank you for any kind of help in advance.

Kind regards,

Istvan
GreyHead 31 Aug, 2011
Hi ikelemen ,

Which version of ChronoForms are you using? You can find the version from Site Admin | Extensions | Install/Uninstall | Components in Joomla! 1.5 or Site Admin | Extensions | Extension Manager | Manage in Joomla! 1.6.

The code you have should be good for ChronoForms v3.

Please post a link to the form so I can take a quick look.

Bob
ikelemen 01 Sep, 2011
Dear GreyHead,

First of all, thank you for spending time helping me in this matter.

I currently use Joomla 1.5 and Chronoforms v4.0 RC1.9.

As for providing you with a link to the form itself, I can do it later on this day. This is because, in the meantime I have been trying a different method to accomplish the same task. Unfortunately, it does not seem to work either. So I will revert it back to the stage what I described in this topic and then I will send you a PM with the link to the form.

As for my new approach, I would like to ask you something, hoping it is a simple question for you.
My other approach is basically to use the Dynamic tab on the Email action window, and use 'fieldname' field to specify email addresses to send email to. I can change the value of the field used to specify recipient email address without any problem and it works with a single email address. However if I change the value of the field to a string containing two email addresses separated by a comme, it does not work. Hence my question is, whether 'dynamic to' field support multiple email addresses separated by commas the same way as 'static to' field does?

Thank you for your help.

Istvan
GreyHead 01 Sep, 2011
Hi Istvan,

You are using ChronoForms v4 so the CFv3 code will not work.

Please see this post to get a list into the Dynamic To box.

Bob
ikelemen 01 Sep, 2011
Dear Bob,

I have read the post you referred to. It seems pretty straightforward indeed. My only problem is that, in my case the number of recipients may change, depending on the tickboxes ticked in on the form. It makes me think that using the dynamic fields will not work for me, as it does not take multiple addresses from one single field on the form.

Would you be able to assist me how to change that code which is for CF v3 to be compatible with CF v4.

Also I do not expect you to do the coding for me and I really appreciate your efforts to help me. Could you please provide me with a source of documentation from which I can figure out how this new version of CF works and I can learn about properties and methods available to manipulate the behaviour of CF v4.

Thank you.

Istvan
ikelemen 05 Sep, 2011
Hi Everyone,

After having hard times solving this problem, I think I managed to sort it out for myself.

In case some will ever have similar problem, I share that how I solved this problem as follows.

I started dumping all sort of variables and object (using global variables, variables like $form and PHP's get_declared_classes, get_class_methods, get_class_vars and get_object_vars functions which revealed data available when my custom code action ran. I noticed a long value, which seemed to have all the properties needed for sending the email including 'to=', subject=','fromname=' and 'fromemail' as a string value.

I used the following code:

$instance=CFChronoForm::getInstance($YourFormNameHere);
print_r($instance->form_actions);


'form_actions' seems to be an array in which there are elements defined as 'stdClass Object'. The appropriate element has a property called 'params' which is long string value with all of the email parameters inside. The 'params' property exist in other elements as well inside 'form_actions'. You have to find the one with the value needed. Once I found it, I could refer to it as:

$data=$instance->form_actions[$key]->params;


The value of $key depends on the form actions used, and may change when you add or remove actions for the form.

Once I had the data available, I used PHP's str_replace to modify the the parameters. It is worth to mention that, I left the email parameters blank on the Email Actions admin interface.

I apologize for the wording, but I am not really good in OOP and keep confusing terms. However I think that, my explanation is sufficient to start sorting the problem with, even for a beginner.

Thanks for Bob who tried to help me.

Kind regards,

Istvan
GreyHead 05 Sep, 2011
Hi Istvan,

Well done - though it does sound like a very complicated fix for what should be a simple probelem :-(

Bob
ikelemen 05 Sep, 2011
Hi Bob,

I think, it sounds complicated as the way I did it was complicated. It was the first time, when I had to do do anything like this. I guess for a proper developer, all of this digging would have taken only for a couple of hours. For me it took two days. However from now on, I have got a little bit of clue how to start solving the problem.

I agree on that it would be far more easier if parameters could be dynamically changed by custom code from the interface. I could imagine a solution where the dynamic fields get their value from a return value of some custom code. However, this tool is available for us for free and I really appreciate this. Probably if I had paid for this product, I could have had proper support and the whole problem would have been solved by matter of few emails.

Thanks,

Istvan
GreyHead 05 Sep, 2011
Hi Istvan,

I could imagine a solution where the dynamic fields get their value from a return value of some custom code.

But you can do this, the only problem here is finding the correct format for a list of email addresses rather than a single one.

Bob

PS There is no difference in the support I give to licensed or non-licensed users. I can't tell the difference.
This topic is locked and no more replies can be posted.