tags:

views:

407

answers:

3

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?

+1  A: 
 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 (>=)

soulmerge
is it possible for execute this code without cronjob?
venkatachalam
It is, you can execute it on every call of the script and store somewhere (in the db possibly) that you executed this query today.
soulmerge
Or store the fact that you've sent the email for each recipient, as stated in the answer. But there is no way to tell if you did something without any hints (like an entry in the db)
soulmerge
+4  A: 

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!

Bogdan Constantinescu
is it possible for execute this code without cronjob?
venkatachalam
Since i need send email once,those matching record
venkatachalam
Well, you can put it in a .php file and call it whenever you want by browsing it if you don't want to add it to crontab.
Bogdan Constantinescu
A: 

How would you modify this code to loop, so that it sends the emails one at a time instead of all at once to a string of addresses in the to or bcc field?

kidcobra