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 = "[email protected]";
$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: [email protected]' . "\r\n".
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to,$subject,$message,$headers);
}
}
}
mysql_close($conn7);
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 ormysqli_*. See stackoverflow.com/q/12859942/354577 for details.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.