2

我只需要向未来发送一封电子邮件,所以我认为我最好使用at而不是使用cron. 到目前为止,这就是我所拥有的,它凌乱而丑陋,而且不擅长逃避:

<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>

输出应该类似于


警告:命令将
在 2010 年 12 月 30 日星期四 19:39:00 使用 /bin/sh 作业 60执行

但是我在 exec 上做错了什么并且没有得到结果?

最主要的是这看起来很乱。有没有其他更好的方法可以做到这一点?

PS:我必须将 apache 的用户(对我来说是 www-data)添加到 /etc/at.allow ...我不喜欢,但我可以忍受。

4

1 回答 1

1

你基本上在做

mail | at

这会将邮件的输出通过管道传输到 at 命令。这是不正确的。mail 命令将立即执行,然后将安排输出(通常没有任何内容,除非有警告)在您指定的任何时间运行。

您的脚本应该将邮件命令转储到文件中,然后执行 exec()

at whenver < mailcmd.sh
于 2010-12-31T01:11:15.700 回答