Choose recipient's mail in a random way in contact form with php

2

Good evening. It is the first time that I occupy this platform to make a query, I hope you can help me.

I have already created a form for a telephone company. It is assumed that the way it works is that depending on the selected city, the information is sent to the emails assigned to each city. I already have that functional.

The question is that my client asks me to select the city of Veracruz, the information is sent to an email chosen at random from an array containing 3 different emails.

<?php
   $to =       $_POST['to'];
   $correosver =  array("[email protected]", "[email protected]", "cotizaciones@$$$.com.mx");
   $randIndex  =  array_rand($correosver, 1);
   $subject =  $_POST['subject'];
   $headers  = 'MIME-Version: 1.0' . "\r\n";
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
   $headers .= "From: ".$_POST['nombre']."<".$_POST['email'].">\r\n"; 
   $headers .= "Reply-To: ".$_POST["email"]."\r\n";
   $ciudad =                $_POST['ciudad']; 
   $message = "<table>
               <tr><td>Quiero: </td><td>".$_POST['quiero']."</td></tr>
               <tr><td>Nombre: </td><td>".$_POST['nombre']."</td></tr>
               <tr><td>Ciudad: </td><td>".$_POST['ciudad']."</td></tr>
               <tr><td>Email: </td><td>".$_POST['email']."</td></tr>
               <tr><td>Teléfono: </td><td>".$_POST['tel']."</td></tr>
               <tr><td>Companía: </td><td>".$_POST['compania']."</td></tr>
               <tr><td>Contrato: </td><td>".$_POST['plan']."</td></tr>
               <tr><td>Equipo: </td><td>".$_POST['equipo']."</td></tr>
               <tr><td>Dudas: </td><td>".$_POST['dudas']."</td></tr>      
               </table>" ;



    if($ciudad == 'veracruz') { 

    $to = $randIndex;
    }

    else if($ciudad == 'alvarado') { 
    $to = 'cotizaciones@###.com.mx';
    }

    else if($ciudad == 'tuxtepec') { 
    $to = 'supervisorcuenca@###.com.mx, cotizaciones@###.com.mx';
    }

    else if($ciudad == 'poza_rica') { 
    $to = 'supervisorpozarica@###.com.mx, cotizaciones@###.com.mx';
    }

    else if($ciudad == 'xalapa') { 
    $to = 'supervisorxalapa@###.com.mx, cotizaciones@###.com.mx';
    }

    else if($ciudad == 'san_andres') { 
    $to = 'supervisorcuenca@###.com.mx, cotizaciones@###.com.mx';
    }

 if(mail($to, $subject, $message, $headers)){
      header("Location: /index.html");
 exit; // we are sending this text to the ajax request telling it that the mail is sent..      
    }else{
        echo 'failed';// ... or this one to tell it that it wasn't sent    
    }
?>

I'm occupying an array with the emails:

$correosver =  array("[email protected]", "[email protected]", "cotizaciones@$$$.com.mx");

And an array_rand with index to index and shuffle the elements of the array:

$randIndex  =  array_rand($correosver, 1);

My logic indicates that selecting veracruz the variable $to is equal to the random content of the variable $randIndex .

if($ciudad == 'veracruz') {             
    $to = $randIndex;
}

I do not know if my syntax is correct or that I am doing wrong, but the emails do not reach their recipients and I do not have any type of error. I do not know if they could help me or point me out any other way to address this problem. Greetings.

    
asked by Manuel Bravo 12.02.2016 в 04:22
source

1 answer

2

You are using the variable $randIndex that is only returning the index of your array. If you do a echo $randIndex you will be thrown 0 or 1 or 2 so you must first change the $to = $randIndex; by $correosver[ $randIndex]; to get the string of the emails.

On the other hand it strikes me that you are overwriting the variable $to since what arrives at the $_POST['to']; is eliminated in the $to = $randIndex; or $to = [email protected];

And finally a tip: Use if( isset($_POST["variable"]) ){} better for greater security in your code.

    
answered by 12.02.2016 в 04:44