user1816133
user1816133

Reputation:

php script cron job after 2 weeks send reminder message to specific email address

Ok, I have a php script that sends a random code to an e-mail address which the user entered.

I need to make that after 2 weeks the user will get a reminder message to the same e-mail address. This should only happen once.

I have created a database table where I store the user's e-mail and the date they were added.

How can I write a script so that after 2 weeks the user will be reminded by email.

I know I need to use a cron Job, my setup looks like: enter image description here

As I understand mailform.php will be used every day at 23 hour, true? And I need to make that cron Job used script everyday for checking if user passed 2 weeks or no.

My database table looks like: enter image description here

Everything is fine, I just don't know how to check if two weeks have passed.

Upvotes: 4

Views: 7026

Answers (3)

Anthony Hatzopoulos
Anthony Hatzopoulos

Reputation: 10557

As I understand mailform.php will be used every day at 23 hour, true?

No. You need 0 23 * * * /path/to/script so it runs at 11pm every day.

This MySQL will get you only the users that who registered two weeks ago (so where regDate was exactly two weeks ago)

SELECT * 
FROM `Your_Users_Table_Name`
WHERE `regDate` = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL -2 WEEK), '%Y-%m-%d')

Then in your cron script you can use that query to get your users then loop through the rows sending out your reminder or code or whatever.

Crude Example

$sql = "
    SELECT * 
    FROM `Your_Users_Table_Name`
    WHERE `regDate` = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL -2 WEEK), '%Y-%m-%d')
";
if (false === ($result = mysqli_query($connection, $sql)) {
    trigger_error('there was a query/sql problem', E_USER_ERROR);
}
if ($result->num_rows > 0) {
    // ooo we found users from two weeks ago.
    while ($row = mysqli_fetch_row($result)) {
        // send email to user
        mail($row['email'], 'The email Subject', 'The email message body');
        // update the users table and set remind = 1
        if (false === (mysqli_query($connection, "UPDATE `Your_Users_Table_Name` SET `remind` = 1 WHERE `email` = '".$row['email']."'"))) {
            trigger_error('there was a query update problem!', E_USER_ERROR);
        }
        // sleep for 2 second so not to hammer mail systems and get flagged as abusive/spammer
        sleep(2);
    }
    mysqli_free_result($result);
}
else {
    // nothing to do today
}

Upvotes: 0

UnholyRanger
UnholyRanger

Reputation: 1971

You can use the following in your script to determine if it has been 2 weeks: Today = March 14th 2013

<?php
$regDate = "2013-02-28";
$today = date("Y-m-d");

$date = date("Y-m-d",strtotime("+2 weeks", strtotime($regDate)));

if($today == $date)
    //do stuff if 2 weeks
else
    //do stuff if isn't
?>

You will have to modify it to your needs but the method of determining the dates is there.

How it works:

strtotime("+2 weeks", strtotime($regDate))

This will take the $regDate string and make a time out of it. Then it will add 2 weeks and make a time out of the new time.

date("Y-m-d", above);

This will then make a date which can be used to compare to $today

Please read the manual : PHP Date

Upvotes: 2

Thibault
Thibault

Reputation: 1596

The reason why data is not saved, is because you are actually not saving it.

You need to make a call to :

mysql_query($query);

to make your record put into the database.

Moreover, in the cron job, the "minute" column should be at "0". So the cron job will run at 23h00 every day. If you put "*" in the minute column, it will run on every minute of 23h, so 23h00, 23h01, 23h02, 23h03... and so on.

You can find basic help on crontab here : http://crontab.org/

Also as stated before, you should use mysqli_ functions, or PDO modules if you can.

mysql_ functions are deprecated since very long time, and are replaced by mysqli_ functions ("i" is for improved). They work very likely and you won't have to change a lot of your code.

Good luck for you project.

Upvotes: 1

Related Questions