Henk Rensenbrink Henk Rensenbrink - 1 year ago 65
MySQL Question

Email is only send once PHP / MySQL

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);
$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;
return(substr($string, $pos+strlen($substring)));

function strbefore($string, $substring) {
$pos = strpos($string, $substring);
if ($pos === false)
return $string;
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 = "
<title>Test HTML email</title>
<p>Geachte " . $blsrowAZ['field_company_name_value'] . " De afstand is minder dan 100 kilometer en dus geschikt voor email</p>
<th>Latitude Aanvrager</th>
<th>Longitude Aanvrager</th>
<td>". $latitude1 ."</td>
<td>". $longitude1 ."</td>
<td>Latitude Bedrijf</td>
<td>Longitude Bedrijf</td>
<td>". $latitudecomp ."</td>
<td>". $longitudecomp ."</td>
<td>Totale Afstand</td>
<td>". $distance ."</td>

// 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();


Answer Source

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download