Customized Form Mail Script

6 posts by 2 authors in: Forums > CMS Builder
Last Post: May 22, 2018   (RSS)

By Mohaukla - May 17, 2018

I am working on letting my client edit fields on their reservations form.

I have everything working properly in the form and the fields appear properly.

But the mail script is breaking.

Original Script

<?php



$tour=$_POST['tour'];
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$address=$_POST['address'];
$citystatezip=$_POST['citystatezip'];
$roomates=$_POST['roomates'];
$departure=$_POST['departure'];
$smoking=$_POST['smoking'];
$message=$_POST['message'];





$to='EMAIL ADDRESS';

$headers = 'From: '.$name."\r\n" .
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject ='Reservation From Rileytours.com';
$body='You have a reservation from Rileytours.com.'."\n\n";

$body.='Tour Name: '.$tour."\n";
$body.='Name: '.$name."\n";
$body.='Email: '.$email."\n";
$body.='Phone Number: '.$phone."\n";
$body.='Address: '.$address."\n";
$body.='City/State/Zip: '.$citystatezip."\n";
$body.='Roomate Name(s): '.$roomates."\n";
$body.='Departure City: '.$departure."\n";
$body.='Smoking Preference: '.$smoking."\n";
$body.='Special Request: '."\n".$message."\n";

if(mail($to, $subject, $body, $headers)) {
die("<img src=\"img/check green sm.png\" align=\"left\" hspace=\"5px\" /><strong>Thanks. Your reservation has been sent and someone will contact you with a confirmation shortly. <br/>
    If you need immediate assistance please contact us at the phone numbers below.</strong>"); 
} else {
die('Oops! Your email failed for some reason. Please try it again and see if it will go through.<br/>
If it does not work again please contact us directly.');
}

?>

This is what I have but I know that I have the code from the CMS assembled wrong

<?php
 
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('D:/HostingSpaces/rileytours/rileytours.com/wwwroot/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  list($reservation_fieldsRecords, $reservation_fieldsMetaData) = getRecords(array(
    'tableName'   => 'reservation_fields',
    'loadUploads' => false,
    'allowSearch' => false,
  ));
  
$email=$_POST['email'];
foreach ($reservation_fieldsRecords as $record): 
$echo htmlencode($record['title'])=$_POST ['echo htmlencode($record['form_field'])']; 
endforeach
$departure=$_POST['departure'];
$smoking=$_POST['smoking'];
$message=$_POST['message'];




$to='EMAIL ADDRESS';

$headers = 'From: '.$fullname1."\r\n" .
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject ='Reservation From Rileytours.com';
$body='You have a reservation from Rileytours.com.'."\n\n";


$body.='Email: '.$email."\n";
foreach ($reservation_fieldsRecords as $record): 
$body.='echo htmlencode($record['title']): '.$echo htmlencode($record['form_field']) ."\n";
endforeach
$body.='Departure City: '.$departure."\n";
$body.='Smoking Preference: '.$smoking."\n";
$body.='Special Request: '."\n".$message."\n";



if(mail($to, $subject, $body, $headers)) {
die("<img src=\"img/check green sm.png\" align=\"left\" hspace=\"5px\" /><strong>Thanks. Your reservation has been sent and someone will contact you with a confirmation shortly. <br/>
    If you need immediate assistance please contact us at the phone numbers below.</strong>"); 
} else {
die('Oops! Your email failed for some reason. Please try it again and see if it will go through.<br/>
If it does not work again please contact us directly.');
}

?>

How should these lines be written?

Thanks for your help

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By Mohaukla - May 19, 2018 - edited: May 19, 2018

The goal should be to allow the client to add/subtract/edit fields in the system and have them show in the form. The form on the page I'm working on works just fine. But the mail script needs to reflect the same fields.

I need correct code for getting the script to work with the CMS.

Thanks

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By leo - May 22, 2018 - edited: May 22, 2018

Hi Michael,

If I understand it right, you have a form that's generated from reservation_fields and will submit the values based on the dynamic field names. And you want to use those values to generate an email?

If that's the case here is what you need to put the post data into the email body:

foreach ($reservation_fieldsRecords as $record){
 $body .= htmlencode($record['title']) . ': ' . htmlencode($_POST[$record['form_field']]) . "\n";
}

Leo - PHP Programmer (in training)
interactivetools.com

By Mohaukla - May 22, 2018

Hey, thanks Leo!

I need to clarify here.

There are 2 sections that are calling for a loop

#1

foreach ($reservation_fieldsRecords as $record): 
$echo htmlencode($record['title'])=$_POST ['echo htmlencode($record['form_field'])']; 
endforeach

and #2

foreach ($reservation_fieldsRecords as $record): 
$body.='echo htmlencode($record['title']) : '.$echo htmlencode($record['form_field']) ."\n";
endforeach

in which your code seems to apply to the second loop but there is no "_post" in that one

Here is yours:

foreach ($reservation_fieldsRecords as $record){ $body .= htmlencode($record['title']) . ': ' . htmlencode($_POST[$record['form_field']]) . "\n";}

I took out the "_post" and came up with:

foreach ($reservation_fieldsRecords as $record){ $body .= htmlencode($record['title']) . ': ' . htmlencode($record['form_field']) . "\n";}

I tried to apply this logic to the first loop but my program keeps telling me its wrong. Can you format the first loop section and I will test the script?


And a couple questions I have are... 

Does the php ignore the whitespace between some characters? ...  "$body ." or " . htmlencode($record['form_field']) "


Is your code missing something to format the lines correctly in the second loop?...  $body .' to create $body.='Departure City: '.$departure."\n";
I did not see it in there but when I added it my program said there was an error.

Thanks

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By leo - May 22, 2018

Hi Michael,

If you use reservation_fieldsRecords to generate the form fields and their names, and you want to use the posted values to make an email out of that. You don't need the first loop. Just grab the post values and put them into the body variable. And that's what my code does.

If there's an error please post the error so I can figure out what's wrong.

Thanks,

Leo - PHP Programmer (in training)
interactivetools.com