Contact forms to custom recipient email address

Permalink
Hey everyone!

I have been searching and searching but have not been able to track down a viable answer.

I have a site that shows apartments available in many different areas. Each property has a different user that manages their content. So each user is assigned as the owner of that particular property. They manage all of this via Dashboard Page Manager/Composer. There is an Owner Contact Email Address attribute that they enter their email address in.

What i need is to have a contact form on the property page that a visitor can fill out and have it send to the Owner Contact Email Address attribute and the Super Admin.

I am fine hardcoding this into the page_type template as i am sure that's what I will have to do. I have seen ways of using a dropdown to select recipient, but that won't work. It HAS TO use the custom attribute defined.

Any help out there!? I should mention I am using 5.6.2.1

View Replies: View Best Answer
AndyJ replied on at Permalink Reply
Hi,
you have a separate page for each property?
Each page has this attribute embedded in it for the owners email address?

You are using the standard form block?

If all of the above are true you could override the form block or use a custom template, grab the attribute value from the page and add it to your outgoing emails.

Does that sound about right?
aaronmarp replied on at Permalink Reply
Yes..all of the above is true since it's a page being created by Dashboard Page Manager.

Just have not been successful in hardcoding the form or pulling the attribute. An example would be wonderful. I am kind of stuck.
AndyJ replied on at Permalink Reply
hello - you could override your form block controller and add something like the following:

$ownerEmail = Page::getCurrentPage()->getAttribute('my_attribute_handle');
if($ownerEmail)   {
   if( Config::get('concrete.email.form_block.address') && strstr(Config::get('concrete.email.form_block.address'),'@') ){
      $formFormEmailAddress = Config::get('concrete.email.form_block.address');
   }else{ 
      $adminUserInfo=UserInfo::getByID(USER_SUPER_ID);
      $formFormEmailAddress = $adminUserInfo->getUserEmail(); 
   }
   $mh = Loader::helper('mail');
   $mh->to( $ownerEmail ); 
   $mh->from( $formFormEmailAddress ); 
   $mh->replyto( $replyToEmailAddress ); 
   $mh->addParameter('formName', $this->surveyName);
   $mh->addParameter('questionSetId', $this->questionSetId);
   $mh->addParameter('questionAnswerPairs', $questionAnswerPairs);


add this directly above the following line:

if(intval($this->notifyMeOnSubmission)>0 && !$foundSpam){


caveats:

If you don't know what I mean by overriding the controller then please ask.

The above code is untested and has no guarantees but there should be enough there to push you in the right direction.

The above sample code only checks if the attribute is in the page and populated; depending on exactly how this should function you might also want to check the pagetype or template being used first. It will not work properly if the form is already set to send to another recipient, nor will it take into account any other forms you may have on the same page.

Someone brighter than me might come along and point out a far better way of doing this...
aaronmarp replied on at Permalink Reply
Thanks for diggin into this! I do need more detail though if possible. Am I just putting this in my page template?
AndyJ replied on at Permalink Reply
hello - I'd somehow assumed you are using Concrete 5.7 - is that correct or are you using 5.6?
aaronmarp replied on at Permalink Reply
I am using 5.6.2.1 as I am not familiar with the new 5.7
AndyJ replied on at Permalink Reply
hi, the code I posted is for 5.7 so it won't work. Don't try to use it.
The principles for 5.6 are the same - but the code is different. I'll post you some sample code a little later - unless someine else chimes in before I get back.
aaronmarp replied on at Permalink Reply
Hi Andy, did you have a chance to look at this anymore?
AndyJ replied on at Permalink Reply
hello,
got busy and sidetracked - the code for 5.6 is virtually the same - I'll post below. But, you don't embed it in the page - this is to add to an override of your form block controller.

$ownerEmail = Page::getCurrentPage()->getAttribute('my_attribute_handle');
         if($ownerEmail)   {
            if ($this->sendEmailFrom !== false) {
               $formFormEmailAddress = $this->sendEmailFrom;
            } else if( strlen(FORM_BLOCK_SENDER_EMAIL)>1 && strstr(FORM_BLOCK_SENDER_EMAIL,'@') ){
               $formFormEmailAddress = FORM_BLOCK_SENDER_EMAIL;
            }else{
               $adminUserInfo=UserInfo::getByID(USER_SUPER_ID);
               $formFormEmailAddress = $adminUserInfo->getUserEmail();
            }
            $mh = Loader::helper('mail');
            $mh->to( $ownerEmail );
            $mh->from( $formFormEmailAddress );
            $mh->replyto( $replyToEmailAddress );
            $mh->addParameter('formName', $this->surveyName);


Again the same caveats apply as before - this should point you in the right direction.

If you have questions please post them here. I would go through the documentation on overriding core files in the documentation to be fully informed.
aaronmarp replied on at Permalink Reply
Hey AndyJ, I forgot about this post till now. Could you give me a little more direction here? I tried to override the Form Block but am unable to get it to work.

Any guidance would be much appreciated.

Thank you!
timconsidine replied on at Permalink Reply
timconsidine
Thank you for this modification !
aaronmarp replied on at Permalink Reply
Were you able to make this modification work?
aaronmarp replied on at Permalink Reply
Did anyone actually get this to work? A step by step walk-through would be sweet!! I am using Advanced Forms on 5.6 and need to use an email_address custom page attribute as the recipient.
hutman replied on at Permalink Reply
hutman
You would just need to add something to the sendNotifications function in the /helpers/form/processor.php like this

$page = Page::getCurrentPage();
$pageEmail = $page->getAttribute('email_address');
if(strlen(trim($pageEmail)){
   $this->answerSet->sendNotification($pageEmail);
}
aaronmarp replied on at Permalink Reply
Thanks for that. Here is what I have but it is failing... this is in the processor for sixeightforms

public function sendNotifications() {
      ///CUSTOM RECIPIENT EMAIL ADDRESS
      $page = Page::getCurrentPage();
         $pageEmail = $page->getAttribute('property_contact_email');
         if(strlen(trim($pageEmail)){
                 $this->answerSet->sendNotification($pageEmail);
         }
      //Send main notification email
      if((($this->f->properties['mailTo'] != '') && ($this->f->properties['sendMail'] == '1')) || (($this->f->properties['mailTo'] != '') && ($this->f->properties['sendMail'] == '2') && ($this->mode == 'add'))) {
         $toAddresses = explode(',',$this->f->properties['mailTo']);
         foreach($toAddresses as $toAddress) {
            $toAddress = trim($toAddress);
            $adminUserInfo=UserInfo::getByID(USER_SUPER_ID);
            $this->answerSet->sendNotification($toAddress);
         }


Link:http://aptlivingguide.com/index.php?cID=6973...
hutman replied on at Permalink Reply
hutman
Can you provide more information about what is "failing"?
aaronmarp replied on at Permalink Reply
Parse error: syntax error, unexpected '{' in /home/aptlg/public_html/packages/sixeightforms/helpers/form/processor.php on line 333


It seems there is a missing ")" in the code example you posted but it still fails after adding that with this message...
Fatal error: Call to a member function getAttribute() on a non-object in /home/aptlg/public_html/packages/sixeightforms/helpers/form/processor.php on line 320
hutman replied on at Permalink Reply
hutman
if(strlen(trim($pageEmail)){ needs to be if(strlen(trim($pageEmail))){
aaronmarp replied on at Permalink Reply
Yes, that is what I changed it to but now receive this error..

Fatal error: Call to a member function getAttribute() on a non-object in /home/aptlg/public_html/packages/sixeightforms/helpers/form/processor.php on line 320
hutman replied on at Permalink Reply
hutman
Sorry, I guess this is called from a tools file so it won't have access to the current page, this would take quite a bit of customizing. If you would like you can contact me via PM and I could give you a quote to do it.
aaronmarp replied on at Permalink Reply
Bummer, ok. Unfortunately I can't afford that cost right now.
hutman replied on at Permalink Best Answer Reply
hutman
One thing you could try would be to add the cID into the form by adding this line into the blocks/sixeightforms/view.php

<input name="cID" type="hidden" value="<?php echo Page::getCurrentPage()->getCollectionID(); ?>" />


then change the code in the helpers/form/processor.php to

$page = Page::getByID($_POST['cID']);
if($page){
    $pageEmail = $page->getAttribute('form_email_address');
    if(strlen(trim($pageEmail))){
        $this->answerSet->sendNotification($pageEmail);
    }
}
aaronmarp replied on at Permalink Reply
Thanks for that. It's not error in out now, but didn't receive an email.
Will have to verify that the site is sending emails at all.

On Apr 5, 2017 10:41 AM, "concrete5 Community" <[email protected]>
wrote:
aaronmarp replied on at Permalink Reply
IT'S WORKING!! Thank you so much!! My issue was that we were trying to use SMTP method. When I switched to the default PHP method, it worked!
aaronmarp replied on at Permalink Reply
I assume it would be just as simple to pass another pageAttribute to the email that is sent? Like the property_name which is a pageAttribute?
aaronmarp replied on at Permalink Reply
I can get a page attribute to show in a hidden input in the form. Just not sure how to pass that to the email. Ideas?
aaronmarp replied on at Permalink Reply
Hi Hutman,

Thanks again for the help with the recipient email address from page attribute. I am trying to pass in another page attribute....
<input name="property_name" type="hidden" readonly="true" value="<?php echo Page::getCurrentPage()->getAttribute('property_name'); ?>" />


I added a hidden input to my form with the same handle and told it to send all html fields. It worked once, then stopped. Any help here?

I should mention I did this in public_html/packages/sixeightforms/blocks/sixeightforms/view.php

Thanks in advance!