-3

In my project i need to send an email in php before 2 days of my account expiry date. the date is stored in mysql db, how to send. Email shoul be send automatically from server.

<?php
include("config.php");
$select_date=mysql_query("SELECT `date` FROM `app_users` WHERE `user_id`=1");
$fetch_date=mysql_fetch_assoc($select_date);
$date=$fetch_date['date'];
$now = date("Y-m-d"); 
$cur_date=strtotime($now);
$your_date = strtotime($date);
$datediff = $your_date-$cur_date;
$days=floor($datediff/(60*60*24));
$email="nisha.gksh@gmail.com";
$subject="Account Expiry";
$message="Your account expires in 2 days";
$headers = "From: info@datesreminder.com" . "\r\n";
if($days==2)
{
    mail($email,$subject,$message,$headers);
}
?>
Kannan Chakkara
  • 151
  • 2
  • 15

1 Answers1

1

You need to setup a cron job. A cron job is a scheduler -commonly used in Unix-like operating systems- to periodically execute commands.

Your PHP code could be something like this:

<?php
$expired_users = getExpiringUsers();
foreach($expired_users as $user) {
   sendMail($user);
}

Now you could setup when to execute this code by setting it as a cronjob. For this add the entry to crontab. You could do this by entering crontab -e. For example if your file should be called everyday 2:30 AM and your file is in /usr/local/bin

30 2 * * * /usr/bin/php -f /usr/local/bin/mail_to_expired_users.php &> /dev/null

You can use a crontab generator to make things easy.

Check this out if you are using a Windows server.

Community
  • 1
  • 1
Dark 73
  • 1,398
  • 10
  • 23