4

I have a page where users enter their email address, click "Send", and the webserver will email them a ~10mb attachment. Right now, I have the page just display "Sending..." and the user waits about 20 seconds on this page.

Instead, I want to have it say "Your attachment will be emailed in a few minutes," sending that process somewhere else and allowing the user to continue browsing without having to open up a new tab.

I really just need someone to point me in the right direction because I don't even know what to Google at this point.

BNL
  • 7,085
  • 4
  • 27
  • 32
  • 1
    Search for "cron job" or "spooler". You want to just create a job in the database and have a long running script or daemon look for that and occasionally do some work. – mario Nov 15 '11 at 19:02
  • possible duplicate of [how to send emails via cron job usng php mysql](http://stackoverflow.com/questions/3368307/how-to-send-emails-via-cron-job-usng-php-mysql) – mario Nov 15 '11 at 19:06
  • To add more info, if your server allows it, set_time_limit(0); will allow your script to run continuously. Make sure to put in Sleep and unset to make sure it doesn't run excessively (sleep) or overflow memory (unset). – Mario Lurig Nov 15 '11 at 19:08

2 Answers2

2

You could call another php file that will process the email sending and make sure to put in this call:

ignore_user_abort(true);

What this does is allows the php process to finish, even though the browser leaves. So you could initiate the process via ajax and then go to another page saying your attachment has been sent.

For more info:

http://www.php.net/manual/en/function.ignore-user-abort.php

Clint
  • 2,871
  • 2
  • 25
  • 28
  • Would it be advisable to then use asynchronous requests to poll the DB every 10 seconds (or whatever interval) to check if the process has been logged as complete and then notify the user? – Jon Kirkman Nov 15 '11 at 19:21
  • Depends on the user case. In this question, he takes them to a page that says "Your attachment will be emailed in a few minutes" so it may not be necessary. But that certainly would be a good option if they wanted to stay on the page. – Clint Nov 15 '11 at 19:23
  • Sounds like to do asynchronous requests polling the DB you could either use proc_open or cron. – user1022422 Nov 15 '11 at 21:35
  • You can also do that. There are situations where that could be more difficult though. – Clint Nov 15 '11 at 21:40
0

I recommend checking out this question I posted a while ago.

How can I run a PHP script in the background after a form is submitted?

The answer explains how you can run a PHP script in the background after a form is submitted. This, of course, is just one way to accomplish this. Another would be to build a list of addresses and set up a cron to run a script on a timed interval. Both can accomplish the same thing, it just depends on how you wish to tackle the issue and what you can do on your server.

Community
  • 1
  • 1
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131