Send Email After Page Load


My third-party mail service works 99% of the time. On those rare occasions when it doesn’t work, the mailing script will wait until a timeout, then fail.

In my eCommerce application this can be confusing for the user, because it happens right after they send in their payment. They click “Submit” sit and wait thirty seconds, then the page times out. Sometimes, they’ll get impatient hit the back button and resubmit payment - but the payment’s already been processed, resulting in a double-charge.

Ideally, I’d like to display the Thank You page BEFORE sending the thank you email - this way if there’s a timeout, there’s no confusion on the user’s part.

What’s the best way to do this?

I know I could use javascript to send a request to the server to send the email after page load, but I’d have to work to make sure the email wasn’t sent each time the page was loaded.

I was wondering if there was a Yii way to do this?

Has anyone else coped with this sort of a timeout issue?

How do you render a page - then when rendering is complete, do something else?


You could just tell the controller to redirect to the thank you page.

public function actionPayment() { 

   //query db for payment by user that matches current items in the last XX seconds [size=2]to stop double charge[/size]

   //if exsist just show thank you or if is okay and processed show thank you for impatient users or timeouts

    if ($payment_processed) {

        //redirect to thank you page with payment id to check db against on page load and also if you already showed it redirect to 

        return $this->render('thankyou', ['payment_id'=>$payment_id]);



on page load ajax check if email was sent and send if it wasn’t sent

Set a flag in the payment for email sent after the email sends

The timeout is most likely going to be a server configuration.

I would personally ajax send the payment with a spinner and a message not to refresh. if payment fails notify user and ask to send again. On success of payment empty the cart and change the content of the page by rendering the thank you page / message. With this approach if the user refreshed or went back and forward the cart couldn’t double submit because it would be empty (if processed) so the user couldn’t double charge and it wouldn’t send again on page load because it’s ajax.

After a successful payment send the payment info to an email queue table that gets processed every xxx seconds in the background via cron or similar. That way if it fails you can just retry until it sends and the user doesn’t know anything went wrong.

You could even try the new queue extension.

Just a couple ideas off the top of my head that may or may not work for you but hopefully it gets you in the right direction.

thanks - that queue looks interesting. I’ll probably do that eventually - when I’ve got more time to delve into it. There are a lot of other ways I could use it.

For now, though, I think just an ajax call with a flag is the short term fix I need. Thanks.

I will second the queue solution, with that you can also do retries and handle timeouts all in background.