【问题标题】:How to send a future email using AT command如何使用 AT 命令发送未来的电子邮件
【发布时间】: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&gt;&amp;1 以查看 shell 是否由于语法冗长而抱怨。还可以尝试创建一个临时的 shell 脚本来读取,并使用单引号作为内部回显。
  • 是的,我会在 1 分钟后收到电子邮件。我也关闭了安全模式。

标签: php email cron schedule at-job


【解决方案1】:

你基本做到了

mail | at

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

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

at whenver < mailcmd.sh

【讨论】:

  • 他实际上是在使用外部 echo 编写一个完整的 mailcmd。但无论如何,组装一个单独的脚本似乎是一种更好的方法。
  • 两者都相当混乱,但我觉得这对我来说更具可读性。另外,我正在回显稍后要运行的命令,这些命令仅在执行时才会创建临时文件。您的想法是将命令存储到文件中,将文件加载到 at,然后您要删除临时文件。我在 echo 中包含了 tmp 文件的创建/删除。
猜你喜欢
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 2013-11-28
  • 2010-09-07
  • 2011-02-05
  • 2010-11-22
  • 2023-03-10
相关资源
最近更新 更多