Forums

Can I change the e-mail recipient based on what the visitor selects in the form?

carramone 24 Feb, 2022
I have a contact form where people can choose what they want to ask about from a drop down. What I would like to do is send the filled out form to different recipients based on how the user fills out the form. For example, if you choose "Service" then the recipient is service@mydomain.com, and if you choose "Marketing" then the recipient is marketing@mydomain.com. And so on.

I have already made variable fields that are shown based on the selections made. For example, if you choose "service" I show info about different sub questions related to service, as well as specific input fields. But I can't find out how to change the recipient.


I am currently using:
Joomla 4.1
Chronoforms 7.09
PHP 8.0.16

Best regards and thanks in advance.


Colnem 24 Feb, 2022
1 Likes
Hi

Never do, but I think:

By PHP, calculate {var:email} with your variable.
In Setting Form/Recipients, try to use {var:email}

Bye
carramone 25 Feb, 2022
Thank you Colnem, your answer pointed me in correct direction, unfortunately I don't understand how to do this. I have used PHP to set different emails based on the language of the form. Each language is an array of addresses, and I have set the two variables[pre]$serviceQuestionEmail
$genericQuestionEmail[/pre]
And I have this code in place to fill the array with the correct emails.
$lang = JFactory::getLanguage()->getTag();
$emailSE = array("service@mydomain.se", "info@mydomain.se");
$emailNO = array("service@mydomain.no", "info@mydomain.no");
$emailDK = array("service@mydomain.dk", "info@mydomain.dk");
$emailFI = array("service@mydomain.fi", "info@mydomain.fi");
$emailEN = array("service@mydomain.com", "info@mydomain.com");
if ($lang=="sv-SE"){
$serviceQuestionEmail=$emailSE[0];
$genericQuestionEmail=$emailSE[1];
};
if ($lang=="nb-NO"){
$serviceQuestionEmail=$emailNO[0];
$genericQuestionEmail=$emailNO[1];
};
if ($lang=="da-DK"){
$serviceQuestionEmail=$emailDK[0];
$genericQuestionEmail=$emailDK[1];
};
if ($lang=="fi-FI"){
$serviceQuestionEmail=$emailFI[0];
$genericQuestionEmail=$emailFI[1];
};
if ($lang=="fi-FI"){
$serviceQuestionEmail=$emailEN[0];
$genericQuestionEmail=$emailEN[1];
};
[br]But anyway, now I want to set the recipient of the email based on the answers given by the visitor. But I don't know how. I can't find a way to add php code to a specific field. And I don't know how to retrieve forms data and process it in PHP either.

I have a drop down with the unit name "setemail", unit title "OurQuestion" and fields name "how_can_we_help_you". If the value from this dropdown is "10" then I want to set recipient email to $genericQuestionEmail. If it is any other value, I'd like to set it to $serviceQuestionEmail.

I thought that I could do something like this:
if (how_can_we_help_you=="10"){
$recipientemail = $genericQuestionEmail;}
else {
$recipientemail = $serviceQuestionEmail;
};

But where? and can it even be done like that?

What I would like to do then is set the recipient email field to use the value of $recipientemail. that is done with {var:recipientemail}, but first I need to make that variable availible for chronoforms and I don't know how to.


I'm happy for any thoughts and ideas on how to proceed.
Colnem 25 Feb, 2022
1 Likes
Hi
You have to build a PHP variable including your email text.

To set this PHP variable in a CF7 variable, use: $this->set("variable_CF7", $var_php);

Then, if you don't use Email Action but Setting Form email, add {var:variable_CF7} before (or after) {email_content}.
carramone 25 Feb, 2022
Thank you, I'm very happy for your help, but I'm sorry. I don't understand how to proceed😟

I have added this to my PHP block:
$this->set("emailrecipient_service", $serviceQuestionEmail);
$this->set("emailrecipient_generic", $genericQuestionEmail);
But I don't know how to call on these variables. I can't see that these variables are set in the debug. I have added {var:emailrecipient_service} before {email_content} but that renders nothing.

I hope that it is just something trivial I am getting wrong :/

So, say that I actually learn how to call on the variables. I still need to set which of them to use based on what have been selected in the "how_can_we_help_you" dropdown.

The debug information have this:
Array
(
[view] => form
[chronoform] => contact-form
[gpage] => end_page
[how_can_we_help_you] => 10
And I would like to use that [how_can_we_help_you] => 10 information to set the recipient to "emailrecipient_service" or "emailrecipient_generic"

I guess this won't do.
if (how_can_we_help_you=="10"){$recipientemail = $genericQuestionEmail;} else {$recipientemail = $serviceQuestionEmail;};
since I now will have two variables inside of the forms and I need to set a new variable {recipients} to either one of these two. Or should I still use PHP to set the variables? If that is the case, I need to intercept the information in the forms and set the variable between the "submit" and the actual send. My first thought was that I could use "page 2" in the forms to set a new PHP block with my logic. But that doesn't not seem to be the case. The variables seem to be emptied on page two, at least I get nothing when I try to echo them.

Best regards
Colnem 25 Feb, 2022
1 Likes
Hi

Sorry: my english is very bad and I'm no sure that I have good understood...

I think that you can do that without PHP...

1) Action / Basics / Variable
Set New Variable : Variable Name = recipient, Value ={var:emailrecipient_generic}
tab Data: Run conditions / Add New Condition / {var:how_can_we_help_you} == 10

So, if how_can_we_help_you == 10, {var:recipient} will be equal to {var:emailrecipient_generic}

2) Action / Basics / Variable
Set New Variable : Variable Name = recipient, Value ={var:emailrecipient_service}
tab Data: Run conditions / Add New Condition / {var:how_can_we_help_you} != 10

So, if how_can_we_help_you != 10, {var:recipient} will be equal to {var:emailrecipient_service}

Is it that you hope?
carramone 28 Feb, 2022
Thank you Colnem

This seems to be a good approach and it feels like I'm almost there. I get the correct language version of the recipient from my PHP code set as the recipient.

The problem is that {var:how_can_we_help_you} is not set. I see it in the debug, but it doesn't seem to be set as a variable that I can use. This means that the condition: {var:how_can_we_help_you} != 10 is always true.

Is there something I need to do to set the [how_can_we_help_you] as a variable?

Best regards
Colnem 28 Feb, 2022
Hi

But I see, up in your message, ...
Array(    [view] => form    [chronoform] => contact-form    [gpage] => end_page    [how_can_we_help_you] => 10
???
carramone 28 Feb, 2022
Hi
Yes, it is there, but {var:how_can_we_help_you} appears to be empty. I added it to my system message as well as after my {email_content}, and it doesn't carry any information. That is why I wonder if I need to do something else to get access to the information. If it is not set as a variable by default. I tried to access some other data, for example name through {var:name} and it was also empty, even though the debug says it carries "Name Namsson":
Array
( [option] => com_chronoforms7 [cont] => manager [chronoform] => contact-form [gpage] => end_page [how_can_we_help_you] => 10 [describe_your_question_as_thorough_as_possible] => test [name] => Name Namesson
Best regards
Colnem 28 Feb, 2022
Hi

I think that your Array is not a var array but a data array because I see option = chronoforms 7, etc.

If I'm right, you have to read how_can_we_help_you by {data:how_can_we_help_you}.
carramone 01 Mar, 2022
Hi Colnem. Thank you very much! That was it, {data:how_can_we_help_you} carries the value. So now I see it in my emails and message. but, for some reason I still can't use this value to set my variable "recipient". The condition {data:how_can_we_help_you} != 10 seems to always be true at runtime when my variable "recipient" is set.

It "feels" like I try to set my variable "recipient" before {data:how_can_we_help_you} have got any data. I tried to verify that by setting the variables with == conditions,

recipient {var:emailrecipient_generic}
{data:how_can_we_help_you} == 10

recipient {var:emailrecipient_service}
{data:how_can_we_help_you} == 1

When I do this and choose the option 1 the {var:recipient} is empty, the same is true for when have set the option 10. This must mean that {data:how_can_we_help_you} is emtpy when I try to set a run condition based on it.

So the question is. Do I need to add something before I set my variables? Something that will make the array data available. Or should the array data already be available, and I am still doing something wrong?

Best regards
Colnem 01 Mar, 2022
HI

What is the source of this data?

Have you an flow chart?
carramone 01 Mar, 2022
Hi Colnem. I'm very grateful for your help. I'll do my best to explain my form.

------------------------
VIEWS
------------------------
Page:1 start_page
Views
-Header
-Text node
-Group
--body
---dropdown (This is the dropdown from where i get the '10' for generic email, and all other values for service email)
---Segment
----body
-----textnode
-----textnode
-----textnode
-----textnode
-----textnode
-----textnode
-----textnode
-----textnode
-----radios
-Group
--body
---textarea
---file
---header
---multifield
----body
-----checkboxes
-----text
-----text
-Group
--body
---header
---text
---multifield
----body
-----text
-----text
---text
---multifield
----body
-----text
-----text
-Group
--body
---ReCaptcha
---Checkbox
---Button
---------------------------------
ACTIONS
---------------------------------
Actions
-PHP
-Variables (This is where I set recipient if {data:how_can_we_help_you} != 10)
-Variables (This is where I set recipient if {data:how_can_we_help_you} == 10)

I don't have anything in my PAGE 2, neither VIEWS nor ACTIONS.

for BEHAVIORS I have the following activated:
DATA: Check security fields, Upload files, Validate fields, Admin email, confirmation message, debug, log data, user email, redirect
INTERFACE/ADMIN: ---

Best regards
Colnem 01 Mar, 2022
1 Likes
Your Form had to be a sequential Form

I suppose that the name of you dropdown is how_can_I_help_you...

Button In page 1 have to submit datas in page 2.

In page 2, you add a message Action
In this message action, you write
<h1>My datas</h1>
{data:pr}

So, you see data how_can_I_help_you value.

Then, in page 2, you can set variable recipient.
carramone 01 Mar, 2022
Thank you Colnem. That was it!!!
I moved all my actions to page 2 and now it works like a charm.
carramone 01 Mar, 2022
TUTORIAL: CF7 - HOW TO CHANGE RECIPIENT BASED ON ACTIVE LANGUAGE AND SELECTION IN A DROPDOWN

PART 1

Setup a dropdown that you would like to use to set different recipients. Note the "field name" of this dropdown. in this tutorial it is "my_dropdown"
my_dropdown
Set any number of selections here. Note each selections' "value". These values will be used to set variables that we will use to change the recipient.
In this tutorial we have three.
value: serviceemail text: Service mail
value: marketingmail text: Marketing mail
value: financeemail text: Financial mail
Under behaviors, add the "Required" tag under "validation". This is just a precaution so that we don't end up with a form without a recipient. This can be avoided by setting a "catch all" recipient, but I have not done that in this tutorial.
carramone 01 Mar, 2022
PART 2 - Switch to the action editor for PAGE 2 of your form.

Drag a PHP action from the Advanced dropdown. Then insert the following code. (exchange the languages tags sv-SE, nb-NO, en-GB with languages that are relevant to your site). All of the actions MUST be placed on PAGE 2 of your form. This and the following ones.[pre]$lang = JFactory::getLanguage()->getTag();
$emailSE = array("service@mydomain.se", "marketing@mydomain.se", "finance@mydomain.se");
$emailNO = array("service@mydomain.no", "marketing@mydomain.no", "finance@mydomain.no");
$emailEN = array("service@mydomain.com", "marketing@mydomain.com", "finance@mydomain.com");
[br]if ($lang=="sv-SE"){[br]$serviceEmail=$emailSE[0];[br]$marketingEmail=$emailSE[1];[br]$financeEmail=$emailSE[2];[br]};[br][br]if ($lang=="nb-NO"){[br]$serviceEmail=$emailNO[0];[br]$marketingEmail=$emailNO[1];[br]$financeEmail=$emailNO[2];[br]};[br][br]if ($lang=="en-GB"){[br]$serviceEmail=$emailEN[0];[br]$marketingEmail=$emailEN[1];[br]$financeEmail=$emailEN[2];[br]}[br][br]$this->set("emailrecipient_service", $serviceEmail);[br]$this->set("emailrecipient_marketing", $marketingEmail);[br]$this->set("emailrecipient_finance", $financeEmail);[/pre]
The first part of this code fetches the active language and sets that languages tag as the variable $lang[br]The second part creates an array of recipients for each specified language. [br]The third part sets the variables: $serviceEmail, $marketingEmail and $financeEmail depending on the value of the variable $lang that we set in the first part[br]The fourth part ensures that we can retrieve the data from our newly set variables from the actual form.[br]
carramone 01 Mar, 2022
PART 3

Drag a Variables action from the Basics dropdown and place it after (below) the PHP action on page 2.
There are three fields that needs to be set. The first one is a dropdown, choose "Variable". The next one is name. write "recipient" and the third one is value, write {var:emailrecipient_service}
Variable recipient {var:emailrecipient_service}
Next add "Run conditions" under behaviors. Here we will make sure that the correct recipient is chosen based on what the visitor have selected in the dropdown that we named "my_dropdown" earlier. There are three fields to fill in here as well. "value 1" set this to "{data:my_dropdown}, next is a dropdown, choose "==" and last is "value 2", write "serviceemail" here.
{data:my_dropdown} == serviceemail

If the visitor have chosen "Service mail" in the dropdown, then the variable $recipient will be set to the value of {var:emailrecipient_service}. Now we need to copy this Variables action twice (or as many times as different recipients we added in the PHP action.

------Variables 2-------[pre]Variable recipient {var:emailrecipient_marketing}
{data:my_dropdown} == marketingeemail[/pre]
------Variables 3-------[pre]Variable recipient {var:emailrecipient_finance}
{data:my_dropdown} == financeeemail[/pre]

When you have a variable set for each of your potential recipients and dropdown choices it is time to go back to the SETTINGS of your form and scroll down to the Admin Email area. There you will set the recipients field to {var:recipient}. If you can't see Admin Email you probably haven't added it to the behaviors of your form. Just click on behavior and add it.
{var:recipient}

A tip.
Add this Who is the recipient: {var:recipient} to the Confirmation Message before you add it to the Admin Email Area. This way you can fill out the form using different languages and selections and see who the recipient for those selections is. It's a great way to make sure that everything is setup correctly.

And that's it, now you have a form that will take the active language into consideration, as well as the selection made in a dropdown when setting the recipient of the form.

Send a HUGE thanks to Colnem for sharing their wisdom and knowhow making this tutorial possible.
[br]Best regards[br]Carramone
Colnem 01 Mar, 2022
Thanks for your heart !!! 🙂
You need to login to be able to post a reply.