0

Good afternoon everyone,

I am currently working on an email system with distance calculation but it is only working half. The email is being send (it is also calculating the distance, only send email if same or lower then 100 kilometers).

But the email is only being send once, only the first MySQL row is being displayed in my email. Shouldn't I receive an email of every company in my database instead of only the first one? Currently I have my own email set as the $to variable, after I receive all the emails I want the $to to be a variable for all the other companies as a while setup.

Here is my code, hope someone can help me with it.

add_action('gform_after_submission_1', 'send_to_kitchen_companies', 10, 2);
function send_to_kitchen_companies($entry, $form) {

    $servername7 = 'localhost';
    $username7 = 'username';
    $password7 = 'password';
    $dbname7 = 'databasename';
    $conn7 = mysql_connect($servername7, $username7, $password7);
    mysql_select_db('databasename');
    $sqlAZ = 'SELECT * FROM creat_companies_zip_lat_long';
    $resultpieAZ = mysql_query ($sqlAZ);

    while($blsrowAZ = mysql_fetch_array($resultpieAZ, MYSQL_ASSOC)) {               

        function strafter($string, $substring) {
            $pos = strpos($string, $substring);
                if ($pos === false)
            return $string;
                else  
            return(substr($string, $pos+strlen($substring)));
        }

        function strbefore($string, $substring) {
            $pos = strpos($string, $substring);
                if ($pos === false)
            return $string;
                else  
            return(substr($string, 0, $pos));
        }   
        $mylatlong = $entry['94'];
        $mylat0 = strafter($mylatlong,'"');
        $mylat1 = strafter($mylat0,'"');
        $mylat2 = strafter($mylat1,'"');
        $latitude1 = strbefore($mylat2,'"');

        $mylong0 = strafter($mylat2,'"');
        $mylong1 = strafter($mylong0,'"');
        $mylong2 = strafter($mylong1,'"');
        $mylong3 = strafter($mylong2,'"');
        $longitude1 = strbefore($mylong3,'"');

        $latitudecomp = $blsrowAZ['latitude'];
        $longitudecomp = $blsrowAZ['longitude'];

        $latitudeFrom = $latitude1;
        $longitudeFrom = $longitude1;

        $latitudeTo = $latitudecomp;
        $longitudeTo = $longitudeFrom;

        //Calculate distance from latitude and longitude
        $theta = $longitudeFrom - $longitudeTo;
        $dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
        $dist = acos($dist);
        $dist = rad2deg($dist);
        $miles = $dist * 60 * 1.1515;

        $distance = ($miles * 1.609344);
        if ($distance <= 100) {

$to = "myemail@email.com";
$subject = "Een test email in HTML";

$message = "
            <html>
            <head>
            <title>Test HTML email</title>
            </head>
            <body>
            <p>Geachte " . $blsrowAZ['field_company_name_value'] . " De afstand is minder dan 100 kilometer en dus geschikt voor email</p>
            <table>
            <tr>
            <th>Latitude Aanvrager</th>
            <th>Longitude Aanvrager</th>
            </tr>
            <tr>
            <td>". $latitude1 ."</td>
            <td>". $longitude1 ."</td>
            </tr>
            <tr>
            <td>Latitude Bedrijf</td>
            <td>Longitude Bedrijf</td>
            </tr>
            <tr>
            <td>". $latitudecomp ."</td>
            <td>". $longitudecomp ."</td>
            </tr>
            <tr>
            <td>Totale Afstand</td>
            </tr>
            <tr>
            <td>". $distance ."</td>
            </tr>
            </table>
            </body>
            </html>
            ";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n".
       "Content-type:text/html;charset=UTF-8" . "\r\n".
       'From: info@keukenaanbod.nl' . "\r\n".
       'Reply-To: no-reply@keukenaanbod.nl' . "\r\n" .
       'X-Mailer: PHP/' . phpversion();

mail($to,$subject,$message,$headers);
        }
    }
}
mysql_close($conn7);
  • If you're working on new code please stop using the `mysql_*` functions. They were deprecated in PHP 5.5, which is so old it no longer even receives security updates, and completely removed in PHP 7. Instead, use PDO or `mysqli_*`. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Nov 23 '16 at 13:20
  • 1
    so many things wrong here the most serious of which is perhaps the use of mysql_* functions. Then you are looping through the whole table and doing the distance calculation in PHP. it's far more efficient to do that in the database. Specially using the mysql geospatial data types and functions. Thirdly you have defined a function inside the loop. While this is syntactically correct, it's unwieldy and not readble. – e4c5 Nov 23 '16 at 13:20
  • 1
    @e4c5 It also breaks if the loop runs more than once. – jeroen Nov 23 '16 at 13:23
  • Side note: don't use `mail()` except for extremely simple internal stuff, it's an awful function that does nothing to ensure your email is properly formatted as per standards. Use a third-party library like Swift Mailer or PhpMailer instead. – Álvaro González Nov 23 '16 at 13:23
  • Your MAIN issue is that you are declaring functions inside a loop, the second time it tries to declare the functions it will throw an error that the functions already exists. Move you functions declaration outside the loop – phobia82 Nov 23 '16 at 13:36

1 Answers1

1

Your MAIN issue is that you are declaring functions inside a loop, the second time it tries to declare the functions it will throw an error that the functions already exists. Move you functions declaration outside the loop

http://php.net/manual/en/functions.user-defined.php

phobia82
  • 1,257
  • 8
  • 10