【发布时间】:2011-06-01 19:51:07
【问题描述】:
我只需要向未来发送一封电子邮件,所以我认为我最好使用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>
输出应该类似于
警告:命令将使用 /bin/sh
执行
2010 年 12 月 30 日星期四 19:39:00 工作 60
但是我在 exec 上做错了什么并且没有得到结果?
主要是这看起来很乱。 有没有其他更好的方法可以做到这一点?
PS:我必须将 apache 的用户(对我来说是 www-data)添加到 /etc/at.allow ...我不喜欢,但我可以忍受。
【问题讨论】:
-
你确定
exec()工作正常吗?尝试print exec('echo 1');测试您不是安全模式的受害者。否则添加2>&1以查看 shell 是否由于语法冗长而抱怨。还可以尝试创建一个临时的 shell 脚本来读取,并使用单引号作为内部回显。 -
是的,我会在 1 分钟后收到电子邮件。我也关闭了安全模式。
标签: php email cron schedule at-job