Current location: Hot Scripts Forums » Programming Languages » PHP » For the Code Gurus - Email Submit to Different Destinations per Field Value - HOW


For the Code Gurus - Email Submit to Different Destinations per Field Value - HOW

Reply
  #1 (permalink)  
Old 12-10-11, 12:29 PM
jaydeesmith8 jaydeesmith8 is offline
Newbie Coder
 
Join Date: May 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
For the Code Gurus - Email Submit to Different Destinations per Field Value - HOW

Hi to all,

I have a form that works fine and submits to one email address and Bccs two others; however, I want to be able to use their dropdown box choice for LOCATION and have it direct to that location's specific email.

The location field has assigned number values for: Cox Blvd = 1, Chisholm Rd = 2, etc..

What code do I change the destination email to below to make it depend on the 4 locations I have in the LOCATION field values for 1, 2, 3 and value 4?

Code:
# Email to Form Owner

$emailSubject = FilterCChars("Contact for xxxxx.com Web Site");

$emailBody = "first_name : $FTGfirst_name\n"
 . "last_name : $FTGlast_name\n"
 . "\n"
 . "phone_number : $FTGphone_number\n"
 . "email_address : $FTGemail_address\n"
 . "\n"
 . "unit_size : $FTGunit_size\n"
 . "climate_needed : $FTGclimate_needed\n"
 . "\n"
 . "location : $FTGlocation\n"
 . "\n"
 . "comments : $FTGcomments\n"
 . "\n"
 . "questions : $FTGquestions\n"
 . "";
 $emailTo = 'Rhonda <rhonda@xxxxxxx.com>';
  
 $emailFrom = FilterCChars("$FTGemail_address");
  
 $emailHeader = "From: $emailFrom\n"
  . 'Bcc: Office <all@xxxxxxx.com>, Debbie <debbie@xxxxxxx.com>' . "\n"
  . "MIME-Version: 1.0\n"
  . "Content-type: text/plain; charset=\"ISO-8859-1\"\n"
  . "Content-transfer-encoding: 8bit\n";
  
 mail($emailTo, $emailSubject, $emailBody, $emailHeader);
I know there is a simple way to have it go to cox@xxxx.com if value '1' is selected from the dropdown box (location) or chisholm@xxxx.com if value '2' is selected from the dropdown, etc.. on to the fourth location.

Does anyone know the code snippet that needs to replace the current $emailto = or how I would do this?

Thanks in advance,
JD
Reply With Quote
  #2 (permalink)  
Old 12-10-11, 01:37 PM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
well from what i'm seeing here you need is something simple. now first off instead of using the mail function(which for a list of reasons isn't really reliable) i suggest you take a look at phpmailer. now then back on the subject. why not do something like

Code:
if ($_POST['some_field']=='whatever'){
$send_this_shit_to='1@domain.com';
} elseif ($_POST['some_field']=='something else'){
$send_this_shit_to='2@domain.com';
} elseif ($_POST['some_field']=='yet more'){
$send_this_shit_to='3@domain.com';
} else {
$send_this_shit_to='4@domain.com';
}
Reply With Quote
  #3 (permalink)  
Old 12-10-11, 11:03 PM
jaydeesmith8 jaydeesmith8 is offline
Newbie Coder
 
Join Date: May 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the Quick Reply

I probably should have placed my entire form code as I have no idea where that type of code would be inserted. Can you look at the full code and tell me after/before what is where the code you suggested placed?

Code:
<?PHP

DEFINE('kOptional', true);
DEFINE('kMandatory', false);

DEFINE('kStringRangeFrom', 1);
DEFINE('kStringRangeTo', 2);
DEFINE('kStringRangeBetween', 3);
DEFINE('kYes', 'yes');
DEFINE('kNo', 'no');


error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('track_errors', true);

function DoStripSlashes($FieldValue) 
{ 
 if ( get_magic_quotes_gpc() ) { 
  if (is_array($FieldValue) ) { 
   return array_map('DoStripSlashes', $FieldValue); 
  } else { 
   return stripslashes($FieldValue); 
  } 
 } else { 
  return $FieldValue; 
 } 
}

#----------
# FilterCChars:

function FilterCChars($TheString)
{
 return preg_replace('/[\x00-\x1F]/', '', $TheString);
}

#----------
# Validate: String

function check_string($value, $low, $high, $mode, $limitalpha, $limitnumbers, $limitemptyspaces, $limitextrachars, $optional)
{
 if ($limitalpha == kYes) {
  $regexp = 'A-Za-z';
 }
 
 if ($limitnumbers == kYes) {
  $regexp .= '0-9'; 
 }
 
 if ($limitemptyspaces == kYes) {
  $regexp .= ' '; 
 }

 if (strlen($limitextrachars) > 0) {
 
  $search = array('\\', '[', ']', '-', '$', '.', '*', '(', ')', '?', '+', '^', '{', '}', '|');
  $replace = array('\\\\', '\[', '\]', '\-', '\$', '\.', '\*', '\(', '\)', '\?', '\+', '\^', '\{', '\}', '\|');

  $regexp .= str_replace($search, $replace, $limitextrachars);

 }

 if ( (strlen($regexp) > 0) && (strlen($value) > 0) ){
  if (preg_match('/[^' . $regexp . ']/', $value)) {
   return false;
  }
 }

 if ( (strlen($value) == 0) && ($optional === kOptional) ) {
  return true;
 } elseif ( (strlen($value) >= $low) && ($mode == kStringRangeFrom) ) {
  return true;
 } elseif ( (strlen($value) <= $high) && ($mode == kStringRangeTo) ) {
  return true;
 } elseif ( (strlen($value) >= $low) && (strlen($value) <= $high) && ($mode == kStringRangeBetween) ) {
  return true;
 } else {
  return false;
 }

}


#----------
# Validate: Email

function check_email($email, $optional)
{
 if ( (strlen($email) == 0) && ($optional === kOptional) ) {
  return true;
 } elseif ( eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email) ) {
  return true;
 } else {
  return false;
 }
}



if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
 $ClientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
 $ClientIP = $_SERVER['REMOTE_ADDR'];
}

$FTGfirst_name = DoStripSlashes( $_REQUEST['first_name'] );
$FTGlast_name = DoStripSlashes( $_REQUEST['last_name'] );
$FTGphone_number = DoStripSlashes( $_REQUEST['phone_number'] );
$FTGemail_address = DoStripSlashes( $_REQUEST['email_address'] );
$FTGunit_size = DoStripSlashes( $_REQUEST['unit_size'] );
$FTGclimate_needed = DoStripSlashes( $_REQUEST['climate_needed'] );
$FTGlocation = DoStripSlashes( $_REQUEST['location'] );
$FTGcomments = DoStripSlashes( $_REQUEST['comments'] );
$FTGquestions = DoStripSlashes( $_REQUEST['questions'] );
$FTGSubmit = DoStripSlashes( $_REQUEST['Submit'] );


# Fields Validations

$ValidationFailed = false;

if (!check_string($FTGfirst_name, 2, 99, kStringRangeBetween, kNo, kNo, kNo, '', kMandatory)) {
 $ValidationFailed = true;
}
if (!check_string($FTGlast_name, 2, 99, kStringRangeBetween, kNo, kNo, kNo, '', kMandatory)) {
 $ValidationFailed = true;
}
if (!check_string($FTGphone_number, 10, 22, kStringRangeBetween, kNo, kNo, kNo, '', kMandatory)) {
 $ValidationFailed = true;
}
if (!check_email($FTGemail_address, kMandatory)) {
 $ValidationFailed = true;
}


# Redirect user to the error page

if ($ValidationFailed === true) {

 header("Location: http://www.xxxxxxxxx.com/error.html");
 exit;

}
# Email to Form Owner

$emailSubject = FilterCChars("Contact for xxxxxxxxxxxx.com Web Site");

$emailBody = "first_name : $FTGfirst_name\n"
 . "last_name : $FTGlast_name\n"
 . "\n"
 . "phone_number : $FTGphone_number\n"
 . "email_address : $FTGemail_address\n"
 . "\n"
 . "unit_size : $FTGunit_size\n"
 . "climate_needed : $FTGclimate_needed\n"
 . "\n"
 . "location : $FTGlocation\n"
 . "\n"
 . "comments : $FTGcomments\n"
 . "\n"
 . "questions : $FTGquestions\n"
 . "";
 $emailTo = 'Rhonda <rhonda@xxxxxxxxxxxxx.com>';
  
 $emailFrom = FilterCChars("$FTGemail_address");
  
 $emailHeader = "From: $emailFrom\n"
  . 'Bcc: Office <all@xxxxxxxxxxxxxx.com>, Debbie <debbie@xxxxxxxxxx.com>' . "\n"
  . "MIME-Version: 1.0\n"
  . "Content-type: text/plain; charset=\"ISO-8859-1\"\n"
  . "Content-transfer-encoding: 8bit\n";
  
 mail($emailTo, $emailSubject, $emailBody, $emailHeader);


# Redirect user to success page

header("Location: http://www.xxxxxxxxxxxx.com/success.html");
exit;
?>
The main page with the form fields itself has the dropdown box for the LOCATION variable that will decide the email destination depending on which value is selected as:

Code:
<select name="location" id="location">
                                <option selected="selected">Select Location</option>
                                <option value="1">Athens</option>
                                <option value="2">Chisholm Rd</option>
                                <option value="3">Cox Blvd</option>
                                <option value="4">Mall Drive</option>
                              </select>
So with that being shown where would I place your sample code and would it call the '$emailTo' instead of POST?

Thanks in advance,
JD
Reply With Quote
  #4 (permalink)  
Old 12-11-11, 12:10 PM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
oh come on.... line 179 instead of what it is
Code:
if ($_POST['location']=='1'){
$emailTo = 'someone <1@xxxxxxxxxxxxx.com>';
} elseif ($_POST['location']=='2'){
$emailTo = 'someone else <2@xxxxxxxxxxxxx.com>';
} elseif ($_POST['location']=='yet 3'){
$emailTo = 'some other <3@xxxxxxxxxxxxx.com>';
} else {
$emailTo = 'eventually <4@xxxxxxxxxxxxx.com>';
}
Reply With Quote
  #5 (permalink)  
Old 12-11-11, 02:08 PM
jaydeesmith8 jaydeesmith8 is offline
Newbie Coder
 
Join Date: May 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks - but does not work

I tried, but line 179 is the second to last line that has:

exit; only on it and that did not work.

I apologize for your frustration and perhaps it is better someone else post an answer that might work to avoid any unecessary frustration on your part.

Can anyone actually tell this novice here between what to lines of code (using text) the propsed code would be inserted as line 179 at the very end of the code ('exit;'), per the suggested answer before is incorrect?

Thanks in advance,
JD
Reply With Quote
  #6 (permalink)  
Old 12-12-11, 04:23 AM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
find the line that says
Code:
 $emailTo = 'Rhonda <rhonda@xxxxxxxxxxxxx.com>';
and replace it with what i posted above
Reply With Quote
  #7 (permalink)  
Old 12-13-11, 09:25 PM
jaydeesmith8 jaydeesmith8 is offline
Newbie Coder
 
Join Date: May 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks

Thanks for the help and sorry for not understanding more easily..
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Country code in text field Godwin PHP 2 09-13-06 02:22 PM
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Email code DotComBlitz HTML/XHTML/XML 1 08-17-04 07:50 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
ASP JMAIL Form Email does not submit nor redirect progtalktn ASP 1 02-26-04 04:41 PM


All times are GMT -5. The time now is 08:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.