0

I would like to be able to send emails to everyone in my mysql database. As of now the code below will only send an email to the first row in my mysql table. The goal is to send one email to all the users in my database.

<?php
//Connect To Database
include "../connection_string.php";

mysql_connect($host,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($db_name);

$result = mysql_query("select property_notifications.email, crm.city, crm.state, crm.primary_fname, crm.primary_lname, crm.bedrooms, crm.baths, crm.assignee, userinfo.fname, userinfo.lname, userinfo.company, userinfo.homecontact, userinfo.clientid from inmobilmex_connect.property_notifications join inmobilmex_connect.crm on property_notifications.email=crm.primary_email join inmobilmex_connect.userinfo where property_notifications.sender=userinfo.clientid");

while($row = mysql_fetch_array($result)){

$recipients=$row['email'];
$prospect_city=$row['city'];
$prospect_state=$row['state'];
$prospect_fname=$row['primary_fname'];
$prospect_lname=$row['primary_lname'];
$bedrooms=$row['bedrooms'];
$baths=$row['baths'];
$realtor_assignee=$row['assignee'];
$fname=$row['fname'];
$lname=$row['lname'];
$company=$row['company'];
$homecontact=$row['homecontact'];
$clientid=$row['clientid'];

//sends email via CRON Jobs
$to = $recipients;
$headers .= 'From: INMOBILMEX <inmobilmex@inmobilmex.com>' . "\r\n";
$headers .= 'Reply-To: inmobilmex@inmobilmex.com'. "\r\n";
$subject = "INMOBILMEX - Nuevas propiedades disponibles en $prospect_city $prospect_state";
$body = "Hola $prospect_fname $prospect_lname,\n \nA continuacion aparece un enlace sobre las busquedas de propiedades que coinciden con su peticion. \n\n http://www.inmobilmex.com/home_search_results.php?optionsRadios=option1&state=&city=$prospect_city&bedrooms=$bedrooms&baths=$baths \n\nFavor de comunicarse conmigo si le interesa alguna propiedad.\n\nQuedo de usted,\n$fname $lname\n$company\n$homecontact\n$cellcontact\n$clientid";

mail($to, $subject, $body, $headers);
}
mysql_close();
?>

Any Help is appreciated!

adenis82
  • 35
  • 8
  • 3
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 19 '16 at 20:55
  • Hello Jay, Ill start looking into using PDO! – adenis82 Jan 19 '16 at 21:22

1 Answers1

1
$recipients=$row['email'];
$prospect_city=$row['city'];
$prospect_state=$row['state'];
$prospect_fname=$row['primary_fname'];
$prospect_lname=$row['primary_lname'];
$bedrooms=$row['bedrooms'];
$baths=$row['baths'];
$realtor_assignee=$row['assignee'];
$fname=$row['fname'];
$lname=$row['lname'];
$company=$row['company'];
$homecontact=$row['homecontact'];
$clientid=$row['clientid'];

put these lines inside the while loop

  • Hi Karl! Thanks for the response.. Unfortunately after making the change an email is still only sent to the email address on the first row of the table... any ideas? – adenis82 Jan 19 '16 at 21:21
  • Have you checked your sql query? Does it give you the desired rows? Also be careful because some free web hosting services don't allow the user to send a lot of emails in a short period of time. Other than that i don't see any errors. – Karl Von Bicycle Jan 19 '16 at 21:33
  • I'll Check! Thank you for the help! – adenis82 Jan 20 '16 at 17:22