Reputation: 102439
I am using a MySQL database and PHP.
My MySQL database contains the following records:
Name Address Data email Date
Joy Fortblair 10 [email protected] 1/22/2009
Bob Atlanta 15 [email protected] 2/22/2009
My intention is to send the email using PHP with the following conditions:
The data is 1 day old. (Current date - date = 1 day
)
It should mail all the email records at one time.
What is the best way to get started?
Upvotes: 1
Views: 15164
Reputation: 5356
The SQL query is pretty simple and it goes as following
SELECT *, TIMESTAMPDIFF(day, Date, NOW()) FROM `your_table_name` WHERE TIMESTAMPDIFF(day, Date, NOW()) = 1;
Now you have to get the contents of the result and put them in a string
<?php
$sql = " SELECT *, TIMESTAMPDIFF(day, Date, NOW()) FROM `your_table_name` WHERE TIMESTAMPDIFF(day, Date, NOW()) = 1";
$query = mysql_query($query);
$emailBody = "";
while($row = mysql_fetch_assoc($query))
{
$emailBody .= "Name: ".$row['Name']."; Address: ".$row['Address']."; Data: ".$row['Data']."; Email: ".$row['email']." \n";
}
mail("[email protected]", "Subject", $emailBody);
?>
Enjoy!
Upvotes: 5
Reputation: 75714
SELECT email FROM Table WHERE DATEDIFF(CURRENT_DATE(), Date) >= 1
As for sending only once: Either store the fact that you've sent an email in the database or call the script once a day with equality comparison (=) instead of greater-or-equal (>=)
Upvotes: 1