I have a MySQL-Database which has one table that includes all the users (Names, eMail-Addresses, passwords and some other columns such as "last_login". Then, there is a table for every user named with the "LastnameFirstname". The "LastnameFirstname" tables include multiple eMail-Addresses and a message body corresponding to the email address.
What I want is to send eMails to the recipients which are found in the "LastnameFirstname" table.
Below is some code that works for me, but only for one table named "LastnameFirstname". I could send the messages with the corresponding bodies text just fine.
The eMails from the "LastnameFirstname" table should be sent out when a certain criteria is met such as "isActivated" or more than 7 days since last login. So, how can I accesss the multiple "LastnameFirstname" tables and send out the messages to the corresponding eMail-Addresses.
I hope I made my point clear. Please tell me if I should improve the question...google-fu was to no avail. I read something about join and implode but it didn't help much. In the end I would like to have a script which can be run as a cronjob on the server-side...
Here is my code so far:
<?php
mysql_connect("localhost", "root", "") or
die("No connection possible: " . mysql_error());
mysql_select_db("test");
//$result = mysql_query("SELECT emailadresse, message FROM StrohmeierStefan, table_LastnameFirstname");
$result = mysql_query("SELECT emailadresse, message FROM table_LastnameFirstname");
$headers = "From: test@test.de";
$subject = "Message Subject";
//$text = "Hallo Welt! Did it work?";
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("eMail: %s", $row[0]);
printf("<br/>");
$to = $row[0];
$text = $row[1];
mail($to, $subject, $text, $headers);
}
mysql_free_result($result);
?>
You should change your query like this:
$result = mysql_query("SELECT emailadresse, message FROM table_LastnameFirstname AS msg LEFT JOIN main_users_table AS usr ON msg.user_id = usr.id WHERE usr.isActive OR usr.last_login < DATE_SUB(NOW(),INTERVAL 1 WEEK)");