0

I want to send an email with automatted content from a login script, but it should be sent with a delay of some (randomized) seconds (for sending one part of a key pair).

Thats why I was trying to use curl, which generally works, instead of using "include" or a class, but the main script should not have to wait until sleep() is ended.

main code is (simple curl code) playing around with "CURLOPT_MUTE,1" and "CURLOPT_RETURNTRANSFER,false" did not work at all.


    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"http://domain/path/to/delayed_mail.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        'to=' . $to . '&from=' . $from . '&subject=...');
    curl_exec ($ch);
    curl_close ($ch);
    ?>

delayed_mail.php looks something like this


    <?php
    //path to mail class
    //some POST and GET REQUEST filters and authentications
    //with      $_REQ[$key]=$value;   as output

    $delay = rand(2,32);
    sleep($delay);
    $ddlab->mail->html($_REQ['to'],$_REQ['from'],...,$_REQ['options']);
    ?>

As I mentioned above, I got stuck a bit. The main script which is for html output, shall not wait, until email is sent, after sleep().

1st question: How "delayed_mail.php" can be executed independent (Send email whenever you want to do so, but leave my script running!)

2nd question: How can I set an internal path like '../../delayed_mail.php' or getcwd().'/delayed_mail.php' (which both doesn´t seem to work) instead of full "http://"-URL ?

Thanks for your efforts.

ddlab
  • 918
  • 13
  • 28
  • My questions may include "Is there another way instead of using curl to reach the same result as described ?" – ddlab Nov 29 '13 at 22:25

2 Answers2

0

the curl dont calculated at php time out limits this mean curl command can to run for ever to get response but you can limit it with curl internal options with this information your script must work with the regular php sleep command!!

$delay = rand(2,32);
    sleep($delay);

will make an random delay between 2 or 32 seconds

2-with curl its better if use full path like as you set!

http://domain/path/to/delayed_mail.php

curl work with this url like you typed it in your browser!!

peiman F.
  • 1,648
  • 1
  • 19
  • 42
  • randomized delay... this is OK and what I want. I could set a fixed delay using sleep(7), I know. This is not part of the problems :-) – ddlab Nov 29 '13 at 22:29
0

YES I want to answer my question :-)

The solution is, NOT to use curl at all :-), thanks to Martin Lakes´s post at http://php.net/manual/en/function.exec.php I used a Linux command. For Windows based servers it must look different, but there´s not enough space on here ;-)


    <?php
    passthru("
        /usr/bin/php
        /home/www/path/to/delayed_mail.php
        " . $params . "
        >
        /home/www/path/to/delayed_mail.log
        2>&1
        &
        ");
    ?>

which is explained here:


    <?php
    passthru("
        linux_php_call
        executed_file
        transferred_parameters
        linux_command_for_output 
        output_file
        linux_commands
        ampersand_at_the_end_for_silent_mode
        ");
    ?>

(All parameters queued with a space)

Just put all required parameters into $params, e.g. as an array(), you´ll get it as a key of $argv, which is an array() too and here you are.

ddlab
  • 918
  • 13
  • 28