Page 1 of 1

Send a PHP web form to different email addresses in arrays

Posted: Wed Aug 15, 2012 2:35 am
by mlungisi
I am struggling to send a PHP web form to different email addresses that are defined in arrays. The form is only sent to the last email in the arrays definition section.

**Contact form code with arrays**

Code: Select all

  <?php
    require_once('src/config.php');
    $VarSecurity = new VarSecurity();
    
    $enquiry = NULL;
    $chk_Locate = FALSE;
    
    if((isset($_GET['enquiry']) && $_GET['enquiry']=='Locate a dealer/distributor')||(isset($_POST['enquiry']) && $_POST['enquiry']=='Locate a dealer/distributor')){
    	$chk_Locate=TRUE;
    }
    
    if(isset($_GET['enquiry']) && $_GET['enquiry'] == 'CC'){
    	$query_recipients = array(array('Complaint', 'csm@examplecompany.co.za'),
    							array('Suggestion', 'csm@examplecompany.co.za'),
    							array('Compliment', 'csm@examplecompany.co.za'));
    	$enquiry = '<input type="hidden" name="enquiry" id="enquiry" value="CC">';
    }
    
    else{
    	$query_recipients = array(array('Locate a dealer/distributor', 'sales@examplecompany.co.za'),
    							array('Technical support', 'technical@examplecompany.co.za'),
    							array('Back orders', 'backorders@examplecompany.co.za'),
    							array('Product enquiry', 'sales@examplecompany.co.za'),
    							array('Catalog request', 'sales@examplecompany.co.za'),
    							array('New customer enquiry', 'sales@examplecompany.co.za'),
    							array('Existing customers - logon', 'dealers@examplecompany.co.za'),
    							array('Existing customers - orders', 'weborder@examplecompany.co.za'),
    							array('Report web problems', 'webmaster@examplecompany.co.za'));
    }
**Config code**

Code: Select all

 <?php
    $db_server = 'www.examplecompany.co.za';
    $db = *********
    $db_username = ************
    $db_password = **********
    
    $title = 'Company ABC';
    $env_repo = getenv('php_repository');
    if(!empty($env_repo)){
    	$classes = $env_repo;
    } else {
    	$classes = 'src/Classes/';
    }
    $pagination = array('items_per_page'=>300);
    $default_page = 'Home';
    $sales_email = 'weborder@examplecompany.co.za';
    $contact_email = 'info@examplecompany.co.za';
    $image_path = 'http://examplecompany.co.za/src/img/email/';
    ?>
**Complete contact page code**

Code: Select all

    <?php
    require_once('src/config.php');
    $VarSecurity = new VarSecurity();
    
    $enquiry = NULL;
    $chk_Locate = FALSE;
    
    if((isset($_GET['enquiry']) && $_GET['enquiry']=='Locate a dealer/distributor')||(isset($_POST['enquiry']) && $_POST['enquiry']=='Locate a dealer/distributor')){
    	$chk_Locate=TRUE;
    }
    
    if(isset($_GET['enquiry']) && $_GET['enquiry'] == 'CC'){
    	$query_recipients = array(array('Complaint', 'csm@Examplecompany.co.za'),
    							array('Suggestion', 'csm@Examplecompany.co.za'),
    							array('Compliment', 'csm@Examplecompany.co.za'));
    	$enquiry = '<input type="hidden" name="enquiry" id="enquiry" value="CC">';
    }
    
    else{
    	$query_recipients = array(array('Locate a dealer/distributor', 'sales@Examplecompany.co.za'),
    							array('Technical support', 'technical@Examplecompany.co.za'),
    							array('Back orders', 'backorders@Examplecompany.co.za'),
    							array('Product enquiry', 'sales@Examplecompany.co.za'),
    							array('Catalog request', 'sales@Examplecompany.co.za'),
    							array('New customer enquiry', 'sales@Examplecompany.co.za'),
    							array('Existing customers - logon', 'dealers@Examplecompany.co.za'),
    							array('Existing customers - orders', 'weborder@Examplecompany.co.za'),
    							array('Report web problems', 'webmaster@Examplecompany.co.za'));
    }
    
    $enquiry_options = '';
    $selected_notes = '';
    $item = NULL;
    if(isset($_GET['item'])){
    	$item=$_GET['item'];
    }
    foreach($query_recipients as $key => $val){
    	if((isset($_POST['enquiry']) && $_POST['enquiry'] == $val[0])||(isset($_GET['enquiry']) && $_GET['enquiry'] == $val[0])){
    		$selected = $val[0].' : '.$item;
    		if($val[0]=='Existing customers - logon'||$val[0]=='Technical support'||$val[0]=='Catalog request'||$val[0]=='Product enquiry'||$val[0]=='New customer enquiry'||$val[0]=='Existing customers - orders'||$val[0]=='Catalog request'){
    			$selected_notes = '<br><p>&bull; If you are an EXISTING Examplecompany dealer please enter you Examplecompany account number so that we can correctly deal with your enquiry. Please enter your username and password for verification purposes (your login details are not sent with the enquiry). If you are already logged in you will not need to enter this information</p>
    							<p>&bull; Please enter </p><br>
    							<p>Examplecompany only supplies to appointed distributors, and not direct to the public.</p>
    							<p>However, our showrooms are open to the public daily, where you can view our product range and get assistance with your queries.</p>';
    		}elseif($val[0]=='Locate a dealer/distributor'){
    			$selected_notes = '<p>&bull; Please supply your area details in the message body, as well as surrounding suburbs</p><br>';
    		}
    	}
    	$enquiry_options .= '<option>'.$val[0].'</option>';
    	$recpt_email = $val[1];
    }
    if(empty($selected)){
    	$selected = 'Select option';
    	$selected_notes = '<br><p>&bull; If you are an EXISTING Examplecompany dealer please enter you Examplecompany account number so that we can correctly deal with your enquiry. Please enter your username and password for verification purposes (your login details are not sent with the enquiry). If you are already logged in you will not need to enter this information</p><br>
    							<p>Examplecompany only supplies to appointed distributors, and not direct to the public.</p>
    							<p>However, our showrooms are open to the public daily, where you can view our product range and get assistance with your queries.</p>';
    }
    
    $cont_ref = NULL;
    $ref_number = NULL;
    
    if(isset($_POST['name'])){
    	$VarSecurity->protectform();
    	
    	if ($Session->ID()==NULL){
    		$query = @mysql_query('SELECT Accnr FROM WEB_Cust WHERE Uname="'.$_POST['dealer_username'].'" AND Pword="'.md5($_POST['dealer_password']).'" AND active=1 AND AXblocked!=2 LIMIT 0,1');
    		$login = @mysql_num_rows($query);
    		if($login==1){
    			$cust_verify = 'Dealer is verified';
    			$contact_email = 'info2@Examplecompany.co.za';
    			$Session->Logout(NULL);
    		} else {
    			$cust_verify = 'Dealer not verfied: username/password supplied but did not match our records.';
    		}
    	}elseif($Session->ID()!=NULL){
    			$cust_verify = 'Dealer is verified';
    			$contact_email = 'info2@Examplecompany.co.za';
    	}else{
    		$cust_verify = 'Dealer not verfied: username/password not supplied.';
    	}
    	
    	if(isset($_POST['enquiry']) && $_POST['enquiry'] == 'CC'){
    		$done = false;
    		while(!$done){
    			$ref_number = 'web'.strtolower(substr(str_replace(' ', '', $_POST['company']), 0, 1)).substr(md5(microtime()), 0, 5);
    			
    			$unique_query = @mysql_query('SELECT * FROM web_cms_feedback WHERE ref_number="'.$ref_number.'"');
    			
    			if(@mysql_num_rows($unique_query) == 0){
    				$done = true;
    			}
    		}
    		
    		$insert_query = @mysql_query('INSERT INTO web_cms_feedback (Web_ref, Name, Surname, Company, Accnr, Email, Tel_cell, Enquiry, Message)
    								VALUES ("'.$ref_number.'", "'.$_POST['name'].'", "'.$_POST['surname'].'", "'.$_POST['company'].'", "'.$_POST['accnr'].'", "'.$_POST['email'].'", "'.$_POST['tel'].'", "'.$_POST['enquiry'].'", "'.$_POST['message'].'")');
    		$insert_data = @mysql_fetch_array($insert_query);
    		$cont_ref = '<br>
    					<h4>Reference: '.$ref_number.'</h4>
    					You will receive an automated response to your email address shortly';
    	}
    	
    	$_POST['recipient_email'] = $contact_email.','.$recpt_email;
    	if(isset($ref_number)){
    		$_POST['subject'] = 'Examplecompany Group: '.$ref_number.': '.$_POST['enquiry'];
    	}else{
    		$_POST['subject'] = 'Examplecompany Group: '.$_POST['enquiry'];
    	}
    	$_POST['sender_email'] = $_POST['email'];
    	$_POST['cc'] = $_POST['email'].', Examplecompany Group <Examplecompany.orders@gmail.com>';
    	$_POST['body'] = '<p>Name: ' . $_POST['name'] . '</p>'
    						. '<p>Surname: ' . $_POST['surname'] . '</p>'
    						. '<p>Company: ' . $_POST['company'] . '</p>'
    						. '<p>Examplecompany Account No.: ' . $_POST['accnr'] . '</p>'
    						. '<p>Account No. verification: ' . $cust_verify . '</p>'
    						. '<p>Email: ' . $_POST['email'] . '</p>'
    						. '<p>Tel/cell: ' . $_POST['tel'] . '</p>'
    						. $_POST['enquiry'] . ': ' . $_POST['message'];
    						
    	if(Email::Send($_POST, NULL)){
    		$message = 'Your feedback has been sent'.$cont_ref;
    	} else {
    		$message = 'There was an error sending your feedback';
    	}
    }
    
    		$Newproducts = new NewProducts();
    		
    		global $body;
    		$body = '<div class="column-310 float-left">
    					<div class="block-khaki home-highlights">
    						<div class="padding-10">
    							<h1 class="text-white">Welcome to Examplecompany</h1>
    							<br/>
    							<p>We are committed to a brighter future by creating lighting and electrical solutions which are beautiful by design and inspired by the environment. We are certain that you will find lighting and electrical products that compliment your life space and enlighten you.</p>
    						</div>
    					</div>
    					<div class="gap-hoz-small"></div>
    					<div class="block-khaki home-newprod">
    						<div class="padding-10">
    						
    							</div>
    
    
    	<table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td align="center"><img src="src/img/contact/Examplecompany-arial-view.jpg" width="288" height="228" /></td>
      </tr>
    </table>
    
    
    
    					</div>
    				</div>';
    		echo $body;
    ?>
            <div class="gap-vert float-left"></div>
            <div class="column-630 float-left">
            	<h1 class="text-grey">Contact feedback</h1>
                <br/>
                <p class="message"><?php echo (isset($message) ? $message : NULL); ?></p>
                <br/>
                <form id="form" action="?page=contact-feedback" method="post">
                    <div class="form order-form">
                    	<?php if($Session->ID()==NULL && $chk_Locate==FALSE):?>
                        <div><label>Examplecompany Account No.</label><div><input type="text" name="accnr" id="accnr" class="input" maxlength="100" value="&nbsp;"/></div></div>
                        <p class="clear"></p>
                        <div><label>User name</label><div><input type="text" name="dealer_username" id="dealer_username" class="input dealer-input"/></div></div>
                        <p class="clear"></p>
                        <div><label>Password</label><div><input type="password" name="dealer_password" id="dealer_password" class="input dealer-input"/></div></div>
                        <p class="clear"></p>
                        <?php elseif($chk_Locate==FALSE):?>
                        <input type="hidden" name="accnr" id="accnr" class="input" maxlength="100" value="<?php echo $Session->ID();?>"/>
                        <?php endif?>
                        <p class="clear"></p><br />
                        <div><label>Name</label><div><input type="text" name="name" id="name" value="" class="input"/></div></div>
                        <p class="clear"></p>
                        <div><label>Surname</label><div><input type="text" name="surname" id="surname" value="" class="input"/></div></div>
                        <p class="clear"></p>
                        <div><label>Company</label><div><input type="text" name="company" id="company" value="" class="input"/></div></div>
                        <p class="clear"></p>
                        <div><label>Email</label><div><input type="text" name="email" id="email" class="input" maxlength="100"/></div></div>
                        <p class="clear"></p>
                        <div><label>Tel/cell</label><div><input type="text" name="tel" id="tel" class="input" maxlength="50"/></div></div>
                        <p class="clear"></p>
                        <div><label>Enquiry</label><div><select name="enquiry" id="enquiry" class="select"><option selected="selected"><?php echo $selected ?></option><?php echo $enquiry_options ?></select></div></div>
                        <p class="clear"></p>
                        <div><label>Message</label><div><textarea name="message" id="message" rows="5" cols="10" class="textarea"></textarea></div></div>
                    </div>
                    <div>
                    <?php echo $selected_notes?>
                        <br/>
                        <?php echo $enquiry?>
                        <input type="submit" id="submit" value="submit" class="submit-khaki" onclick="javascript:return mail.Send();"/>
                        <p class="clear"></p>
                        <br/>
                    </div>
                </form>
               <br />
              <p><input type="submit" value="Back" class="submit-khaki float-right" onclick="javascript:window.history.back();"/></p>
            </div>
The email goes to info@examplecompany.co.za email and the last email (Webmaster@examplecompany.co.za) on arrays group.

Please help.

Re: Send a PHP web form to different email addresses in arra

Posted: Wed Aug 15, 2012 1:46 pm
by califdon
Since you don't provide any documentation comments in your script, it makes it quite difficult for someone else to follow your intentions, but for starters, within your foreach loop, you assign values to the string variable $val, but then you refer to array variable $val[0], which has not been set.

Re: Send a PHP web form to different email addresses in arra

Posted: Thu Aug 16, 2012 12:58 am
by mlungisi
@Califdon, some developed the website for us and no comments at all, I took over from the said person and been struggling with the contact page.

Re: Send a PHP web form to different email addresses in arra

Posted: Thu Aug 16, 2012 1:02 am
by mlungisi
HI, so do you think assigning arrays to Val[*] will help?
califdon wrote:Since you don't provide any documentation comments in your script, it makes it quite difficult for someone else to follow your intentions, but for starters, within your foreach loop, you assign values to the string variable $val, but then you refer to array variable $val[0], which has not been set.

Re: Send a PHP web form to different email addresses in arra

Posted: Thu Aug 16, 2012 9:38 am
by califdon
No. The foreach loop already gives you the 2 variables you want, there is no need for an array at that point. Just use those variables.

Re: Send a PHP web form to different email addresses in arra

Posted: Thu Aug 16, 2012 9:42 am
by mlungisi
But the email is only sent to the last email address on arrays,

$query_recipients = array(array('Locate a dealer/distributor', 'sales@Examplecompany.co.za'),
array('Technical support', 'technical@Examplecompany.co.za'),
array('Back orders', 'backorders@Examplecompany.co.za'),
array('Product enquiry', 'sales@Examplecompany.co.za'),
array('Catalog request', 'sales@Examplecompany.co.za'),
array('New customer enquiry', 'sales@Examplecompany.co.za'),
array('Existing customers - logon', 'dealers@Examplecompany.co.za'),
array('Existing customers - orders', 'weborder@Examplecompany.co.za'),
array('Report web problems', 'webmaster@Examplecompany.co.za'));

What should I do to send email to sales@Examplecompany.co.za if a customer selects "Catalog request" from the options?

Re: Send a PHP web form to different email addresses in arra

Posted: Thu Aug 16, 2012 1:55 pm
by califdon
You should follow this logic:
  1. Problem: email is only sent to the last item in my array.
  2. The problem must be that I am not sending an email each time in the loop, but only using the last value assigned to the variable.
  3. I better take a look at where I'm sending the email.

Re: Send a PHP web form to different email addresses in arra

Posted: Fri Aug 17, 2012 2:08 am
by mlungisi
Hehehe help me bro :)

Re: Send a PHP web form to different email addresses in arra

Posted: Fri Aug 17, 2012 12:38 pm
by califdon
Do you understand the difference between a string variable and a string array? If you do, just apply that understanding to your foreach loop. If you don't, you're asking me to rewrite your entire script, which I don't have the time or inclination to do.