venkatachalam
venkatachalam

Reputation: 102439

How can I send an auto email from MySQL records?

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:

What is the best way to get started?

Upvotes: 1

Views: 15164

Answers (2)

Bogdan Constantinescu
Bogdan Constantinescu

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

soulmerge
soulmerge

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

Related Questions