tags:

views:

55

answers:

7

Hi,

I have a small redirection script that does 2 things,

First, it executes an email (using the mail() func) and only then redirects the person to the new link.

Problem is, for some reason, the mail func on my server works really really slow and I don't want it to affect any of my visitors,

How can I still run the mail function but do it in a way that will not affect the visitor from redirecting to the new link instantly?

Thanks,

A: 

What about putting the mail in a database, and run a cron for sending mails?

In this way, you can control the mail queue too.

Lekensteyn
Is there a simpler way? I won't have so many mails that I need a queue for..
Jim
A: 

One possibility for this kind of background processing is to use some kind of messaging queue system (AMQP, ZeroMQ, ...)

plaes
A: 

There is a discussion on php.net on how to do close a connection but continue executing your script. It involves output buffering and sending a Connection: close HTTP header.

You should also see this question.

Jesse Dhillon
A: 

hm... if your server run in a *nix environment, you could fork the process using pcntl_fork() and run the mail function in the newly created child process and continue the redirection concurrently.

sl4sh
+3  A: 

You can fork to process the email as a child process: http://us2.php.net/manual/en/function.pcntl-fork.php

Or you can write the email to a database and have a cron job process emails after the fact.

Depending on server configuration, you might find that you can redirect the user, flush(), then send the mail and have it still succeed.

MightyE
A: 

Use a combination of an external mail program, system (or one of the related functions), and nice.

Weston C
A: 

I have seen it done before by using fsockopen. The basically what you do is call a page dedicated to this task alone and pass it whatever GET variables you need to. The called page will then do some security checks such as make sure the IP is in a white-list, that the args are valid etc.

Depending on what you are trying to accomplish, this may do what you need it to.

Icode4food