Add form email recipients from form itself

Permalink 3 users found helpful
Is it possible to add a field to a form, have the person filling out the form enter an email address, and the form submission is emailed to that address? Basically, I want the person filling out the form to have it's contents emailed to multiple recipients of their choice. I was trying to do this through a custom template, but couldn't figure out the code. Has anyone else tried this or have any suggestions on where to begin?

rdains
 
Mnkras replied on at Permalink Reply
Mnkras
i think extended form in the MP might do that, not sure
Fauxpas replied on at Permalink Reply
I did something similar with a select box field. I approached this by editing the controller and putting the email address in code to hide them from email harvesting scripts.

Step 1:

In the form controller, search for:

$answer=$txt->sanitize($_POST['Question'.$row['msqID']]);


Add this after the found line:

/* recipient mod */
if ($row['question'] == 'Recipient') {
  switch ($answer) {
    case 'Name of recipient':
      $recipient_email = 'email-of-recipient@domain.com';
      break;
    default:
      $recipient_email = $this->recipientEmail;
      break;
  }
}
/* end recipient mod */


Replace "Name of recipient" and "email-of-recipient@domain.com". Add more cases for each recipient.

Step 2:

In the form controller, search for:

$mh->to( $this->recipientEmail );


Replace the found line with:

/* recipient mod */
$mh->to( $recipient_email ); 
/* end recipient mod */


Step 3:

Add a select box field to the form block with "Recipient" as the Question. Each Answer Option line should correspond to the "Name of recipient" in the form controller code.
lukestratton replied on at Permalink Reply
Hi

Thanks for sharing your code with us. It works perfectly, however as this is a bit out of date now, I think they have changed the controller slightly, and as a result, I now get errors if I don't use a recipient dropdown.

I get the following error message in the log:

Mail Exception Occurred. Unable to send mail: Unable to send mail.


Do you have any ideas on what I may be able to do to get this working?

Thanks, Luke.
Fauxpas replied on at Permalink Reply
I'll do some tests this evening with the current version and report back here with an updated mod.
Fauxpas replied on at Permalink Reply 1 Attachment
Hi Luke,

The form block controller now uses a derived class that we can use to override the action_submit_form() method. After we add the method, the modifications are very similar.

Preparation:
1. Turn off all caching during development via "Dashboard->System & Settings->Optimization->Cache & Speed Settings".

Steps:
1. Create the block controller override. Copy /concrete/blocks/form/controller.php to /blocks/form/controller.php

2. Override the action_submit_form() method from the Concrete5_Controller_Block_Form class. Copy the method action_submit_form() from /concrete/core/controllers/blocks/form.php to the FormBlockController class defined in /blocks/form/controller.php

3. Modify action_submit_form() to translate the value passed in by a "Recipient" field to a hard-coded email address, gracefully. After line 171 add:
// recipient mod
$recipient_email = $this->recipientEmail;
if( $row['question'] == 'Recipient' ) {
   $answer = $txt->sanitize( $_POST['Question'.$row['msqID']] );
   switch( $answer ) {
      case 'Recipient 1':
         $recipient_email = 'email01@domain.com';
         break;
      case 'Recipient 2':
         $recipient_email = 'email02@domain.com';
         break;
   }
}
// end recipient mod


4. Modify action_submit_form() to use our email variable. Change line:
$mh->to( $this->recipientEmail );


To:
// recipient mod
$mh->to( $recipient_email );
// end recipient mod


Remember to create a select box field named "Recipient" with each answer line corresponding to cases in the switch statement.

Attached is a sample form block controller override.
tacktack replied on at Permalink Reply
tacktack
Hello,

Can you explain or specify Steps 2 please ?

Thank you for you help,

Brice
Fauxpas replied on at Permalink Reply
Hi tacktack,

I haven't developed in Concrete 5 in some time, so the newest version maybe different.

What part of Step 2 are you confused by? Did you see the attachment? The end result of Step 2 is that you copy the action_submit_form() method to your new controller file. This method should already exist in the core block code, as indicated in the instructions.

If you're still having a problem, I will need your Concrete 5 version to check these instructions are still valid.
tacktack replied on at Permalink Reply
tacktack
Thank you for your response.

It works but the email is not sent or notified to the recipient.
Do you have an idea to do this?
Fauxpas replied on at Permalink Reply
Hi tacktack,

Have you tried submitting the form without any modification? I'm concerned that the email issue could be environmental and unrelated to the form modifications.

Attach your form block controller.
sschildbach replied on at Permalink Reply 1 Attachment
sschildbach
Thank you so much Faux Pas for helping us out with this. I too need a form that allows the user to choose which person they want to send it to, in my case it is choosing from two subsections of a company, and behind the scenes the "Choose Recipient" activates a different email. But, when I made your changes, it did not work. Enclosed is my file. Can you tell me where I might have gone wrong?
Fauxpas replied on at Permalink Reply
Hi sschildbach,

I have time later today to review your file. What Concrete 5 version are you using?
sschildbach replied on at Permalink Reply
sschildbach
5.6.0.2 and I'll be updating to the latest version after I complete this contact form. I'm very familiar with Concrete5, but am a cut paste php coder. I understood what you were doing and was surprised to see it not working. The email entered in the field found in edit form > options is still the email the form is being sent to. I am using the included standard form that comes with Concrete5.
Fauxpas replied on at Permalink Reply
I just tested in 5.6.0.2 and your form block controller file is fine. I suspect your form configuration maybe the culprit.

Send a screenshot of the form edit tab and the "Choose Recipient" edit question view.
sschildbach replied on at Permalink Reply 2 Attachments
sschildbach
Another possible reason could be the shared server account I'm using has no domain yet. I'm just usinghttp://66.147.244.142/~landllaw/... for the url. And, my screenshots are enclosed.
Fauxpas replied on at Permalink Reply
After line 184, add:

var_dump( $recipient_email );exit;


On submission:
1. If it doesn't output an email address and exit, the file is probably in the wrong place and the controller override isn't being run - needs to be in /blocks/form/controller.php (if it is there, try clearing the Concrete 5 cache)
2. If it shows the wrong email address, var_dump( $answer ) and check that against the cases (the "&" maybe causing a problem, but my tests don't indicate that to be true). Finally, try deleting and remaking the "Choose Recipient" question.

Your file is working fine for me and the form looks configured correctly.
sschildbach replied on at Permalink Reply
sschildbach
Fauxpas, that did it! The var_dump. And, It accepted the "&". You are a generous person. Also, thank you Chad, but I wanted to learn how to do my own php for this.

There is no option for marking this as best answer, but I want to.

And, I assume that if I want one of the 'choose recipient" to go to 2 emails, I just do this?:

$recipient_email = 'joe@domain.com, sara@domain.com';
Fauxpas replied on at Permalink Reply
Glad I could help.

Yes, the $recipient_email will accept a comma-separated value.
RadiantWeb replied on at Permalink Reply
RadiantWeb
ProForms has an "autoresponder" QuestionType that can do this for you. It will take any "email" fieldtype and send the composed message to them. You can also include fields from that same form entry in the response by adding [[question_handle]] in your message.

http://www.concrete5.org/marketplace/addons/proforms/...

ChadStrat
Sadu replied on at Permalink Reply 1 Attachment
Sadu
Hi Everyone,

I had a similar problem but my customer wants to edit the list of recipients themselves.

So based on the discussion here I have made a modified controller that allows the list of recipient options to be edited in the form block edit screen.

It's not totally idiot-proof, but an improvement on hard-coding in the controller I think.

The attached controller.php contains instructions at the top on how to install - nothing difficult there, just upload the modified controller file and clear cache.

Then you add a select field to your form.
For the name of the field - if you want a field called "Department" you would enter "Department [recipient]". The [recipient] shortcode indicates that this field receives special treatment when the form is submitted.

For the select options, enter something like this...

Sales,sales@example.com
Support,support@example.com
Accounts,accounts@example.com

On the frontend, the user will see a field called "Department" with options "Sales, Support, Accounts" (the other stuff gets stripped out). The actual emails aren't made available to spammers, and if there's a problem with the custom address the email gets delivered to the default recipient (as specified on the options tab).

Hopefully this is of some use to someone. I have tested on 5.6.2.1 but not on any other versions. If you want to apply this to another version I have marked all the mods in the code with "//mod" so you can find the changes easy enough.

If you need support with this, post here. I'm not really available to provide free support but if I can give an answer I will, otherwise someone else might be able to.

Hope this helps someone out there.
steveftm replied on at Permalink Reply
Has anyone had any luck with this? I'm using C5 version 5.6.3.3 and can't seem to get it working? Know of a good add-on that supplies this function?
steveftm replied on at Permalink Reply
I managed to get this working with C5 v5.6.3.3

HOWEVER the emails that get sent are BLANK!

Can anyone offer help?