Pergunta

I just need to send one email into the future, so I figured i'd be best at using at rather than using cron. This is what I have so far, its messy and ugly and not that great at escaping:

<pre>
<?php
    $out = array();

    // Where is the email going?
    $email = "you@gmail.com";

    // What is the body of the email (make sure to escape any double-quotes)
    $body = "This is what is actually emailed to me";
    $body = escapeshellcmd($body);
    $body  = str_replace('!', '\!', $body);


    // What is the subject of the email (make sure to escape any double-quotes)
    $subject = "It's alive!";
    $subject = escapeshellcmd($subject);
    $subject  = str_replace('!', '\!', $subject);


    // How long from now should this email be sent? IE: 1 minute, 32 days, 1 month 2 days.
    $when = "1 minute";

    $command= <<<END
    echo "
            echo \"$body\" > /tmp/email;
            mail -s \"$subject\" $email < /tmp/email;
            rm /tmp/email;
    " | at now + $when;
END;

    $ret = exec($command, $out);
    print_r($out);
?>

</pre>

The output should be something like


warning: commands will be executed using /bin/sh
job 60 at Thu Dec 30 19:39:00 2010

However I am doing something wrong with exec and not getting the result?

The main thing is this seem very messy. Is there any alternative better methods for doing this?

PS: I had to add apache's user (www-data for me) to /etc/at.allow ...Which I don't like, but I can live with it.

Foi útil?

Solução

You're doing basically

mail | at

which would pipe mail's output to the at command. This is incorrect. The mail command will execute immediately, and the output (usually nothing, unless there was a warning) will then be scheduled to run at whatever time you specify.

Your script should dump the mail commands into a file, then do an exec() on

at whenver < mailcmd.sh
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top