Jump to content

how to Run a line of code after 10 minutes of running another line of code in PHP


Go to solution Solved by Barand,

Recommended Posts

I want my application will send a email after 10 minutes of sending another email.

In my application

  • A user completes registration with payment
  • Application sends the user a payment confirmation email

Now I want to

  • send another email 10 minutes After payment confirmation email with welcome tips

Below is the function where for user setup .

 

public function finishUserSetup($Sub){

    if($Sub == 0){
        $subscription = SubscriptionPlans::where('identifier', '=', "Monthly")->first();
        $expiry = date('Y-m-d', strtotime('+' . $subscription->months . ' months'));
        $sub_period = "monthly";
    
    } else{
        $subscription = SubscriptionPlans::where('identifier', '=', "Annually")->first();
        $expiry = date('Y-m-d', strtotime('+' . $subscription->months . ' months'));
        $sub_period = "annually";
    }

    $this->expiry_date = $expiry;
    $this->user_type = "SUB";
    $this->subscription_period = $sub_period;
    $this->update();

    $replaceArray = array(
        'fullname' => $this->forename . " " . $this->surname,
        'subscriptionName' => $subscription->name,
        );
    EmailTemplate::findAndSendTemplate("paymentconfirm", $this->email, $this->forename . " " . $this->surname, $replaceArray);

  }

In the above function the last line of code is the one which sends a payment confirmation email to the user which is

EmailTemplate::findAndSendTemplate("paymentconfirm", $this->email, $this->forename . " " . $this->surname, $replaceArray);

I want to execute the following line of code 10 minutes after the above one

 

EmailTemplate::findAndSendTemplate("WelcomeTips", $this->email, $this->forename . " " . $this->surname, $replaceArray);

 

 

How to do that. that is running the last line of code 10 minutes after  

  • Solution

Here's one way.

When you send a payment confirmation write a record (forename, surname, email) to "confirmation" table

CREATE TABLE `confirmation` (
  `confirm_id` int(11) NOT NULL AUTO_INCREMENT,
  `forename` varchar(45) DEFAULT NULL,
  `surname` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `time_confirmed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `tips_sent` datetime DEFAULT NULL,
  PRIMARY KEY (`confirm_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Set up a cron job to run every 5 minutes. This would

SELECT confirm_id, forename, surname, email
FROM confirmation
WHERE tips_sent IS NULL
      AND NOW() - INTERVAL 10 MINUTE > time_confirmed;

foreach record returned

  • Send welcome tips email
  • UPDATE confirmation SET tips_sent = NOW() WHERE confirm_id = ?
Edited by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.