简体   繁体   English

我终于可以使用cron了……但是我不确定它是否可以与我的代码一起使用。 我该如何更改?

[英]I finally was able to use cron… but I'm not sure if it will work with my code. How can i change it?

My webhost supports cronjobs. 我的虚拟主机支持cronjobs。 I am very new so I hav almost no idea what I'm doing. 我很新,所以我几乎不知道自己在做什么。 I scheduled cron to run a script that sends an email. 我安排cron运行一个发送电子邮件的脚本。 But I don't know what to do! 但是我不知道该怎么办! Here's my cron: 0 0 28 * * php -f /home/a7269592/contact.php Now how could I adjust my code so when It's the 28th, that code will send out an email. 这是我的cron: 0 0 28 * * php -f /home/a7269592/contact.php现在我该如何调整我的代码,所以当它是28号时,该代码将发送一封电子邮件。 Here's the PHP: 这是PHP:

<?php    
if(isset($_POST['email']))
{

 $headers = "From: Memory Jet <your_company@example.com>\r\n";


$to_visitor = $_POST["email"];
$common_data = $_POST["message"];
mail($to_visitor, "Your Memory", $common_data, $headers);

} ?>

Here's the html form: 这是html表单:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<html> <body><form method="post" action="contact.php">
 Email: <input name="email" type="text"><br> name:<br> 
<textarea name="name" rows="15" cols="40"></textarea><br> 
Message:<br> <textarea name="message" rows="15" cols="40"></textarea><br>
 <input type="submit"> </form> <body> <html>
</body>

So how would I change the php so that cron would schedule it?? 那么我将如何更改php,以便cron安排它? Thanks in advance! 提前致谢! -Ben -本

Not so far, i was in exactly the same situation. 到目前为止,我的处境完全相同。 To my mind, you're trying to deligate mailing from exactly "at posting moment" to scheduled timetable. 在我看来,您正在尝试将邮件从“发布时”精确地调度到预定的时间表。 So that, first that you need to know is: 因此,首先需要知道的是:

WHEN YOU'RE RUNNING CRON YOU HAVE NO ACCESS TO $_SERVER, $_POST, and other global variables 在运行CRON时,您将无法访问$ _SERVER,$ _ POST和其他全局变量

That's because you run PHP, NOT through the server, which is the AUTHOR of the global variables. 那是因为您运行PHP而不是通过服务器运行,而PHP是全局变量的作者。

So, you need to create temporary storage for your mail, such as an mail task file (might be problem with at-execute time appending tasks, for example if you cron mailer sends mails, and at the same moment your frontend PHP script wants to add some tasks, so that the task file might me corrupted) or DB, or whatever your imagination can do. 因此,您需要为邮件创建临时存储,例如邮件任务文件(例如,在执行cron mailer发送邮件时,附加执行任务时可能会出现问题,同时您的前端PHP脚本希望添加一些任务,以便任务文件可能被损坏)或数据库,或您想像的任何事情。

After that, you need to set up your cron task, which would take for example first 500 mail tasks and mails it. 之后,您需要设置cron任务,例如,将执行前500个邮件任务并将其邮寄。 Sure, you can mail all mail tasks at this moment, not a problem. 当然,您现在可以邮寄所有邮件任务,这不是问题。

Hope, it helps. 希望能帮助到你。

I see what you are trying to do, but are you planning for it only to send an email if it the 28th or store it and send it on the 28th? 我知道您正在尝试做什么,但是您是否打算仅在28日发送电子邮件或存储并在28日发送电子邮件呢? If you are using the first option you should change it so it says this. 如果您使用的是第一个选项,则应对其进行更改,以便其说明如下。

<?php   
$today = getdate()
if(isset($_POST['email']) && $today['mday'] == 28)
{

$headers = "From: Memory Jet <your_company@example.com>\r\n";


$to_visitor = $_POST["email"];
$common_data = $_POST["message"];
mail($to_visitor, "Your Memory", $common_data, $headers);

} ?>

As for the storing, the comments are right, you should re-architect your code. 至于存储,注释是正确的,您应该重新构建代码。 The crontab will run the code without gettimg data from the form - web based and CLI requests are handled diffirently. crontab将运行代码,而不会从表单获取gettimg数据-基于Web和CLI请求的处理方式不同。 I would definetely add another file to handle the form request and right it to a form or database. 我将明确地添加另一个文件来处理表单请求,并将其移至表单或数据库。 The one run by the crontab would read from that file or database. 由crontab运行的程序将从该文件或数据库中读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM