我是PHP的新手,我确实创建了一个cron作业,每天在cPanel中发送两次邮件:
Minute Hour Day Month Weekday Command
0 0,12 * * * /home/user/public_html/cronjob/send-mail.php
发送-mail.php
<?php
$to = "john@domain.com";
$sender = "sender@domain.com";
$subject = "TEST CRONJOB";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Bcc: ".strip_tags($sender)."\r\n";
$headers .= "From: Domain.com <".strip_tags($sender).">\r\n";
$html = "<html>
<body style='font-family:arial;'>
<p>Have a good day!<br>Test email sent twice per day.</p>
</body>
</html>";
mail($to, $subject, $html, $headers);
?>
经过几次测试,我收到了以下错误:
/home/user/public_html/cronjob/send-mail.php: line 1: ?php: No such file or directory
/home/user/public_html/cronjob/send-mail.php: line 3: =: command not found
/home/user/public_html/cronjob/send-mail.php: line 4: =: command not found
/home/user/public_html/cronjob/send-mail.php: line 5: =: command not found
/home/user/public_html/cronjob/send-mail.php: line 6: =: command not found
/home/user/public_html/cronjob/send-mail.php: line 7: .=: command not found
/home/user/public_html/cronjob/send-mail.php: line 8: syntax error near unexpected token `('
/home/user/public_html/cronjob/send-mail.php: line 8: `$headers .= "Bcc: ".strip_tags($sender)."\r\n";'
我可以通过简单地运行脚本来发送电子邮件,但不能在cron中发送。
答案 0 :(得分:1)
当你从shell执行时,它运行正常,因为系统知道你正在执行(通过./),但是cron不知道;所以明确告诉它使用php执行该文件。
所以在你的crontab中,替换
/home/user/public_html/cronjob/send-mail.php
与
php -f /home/user/public_html/cronjob/send-mail.php
答案 1 :(得分:0)
将您的cron标签内容替换为:
#Minute Hour Day Month Weekday Command
0 0,12 * * * /usr/bin/php /home/user/public_html/cronjob/send-mail.php
请注意,send-mail.php
中使用的路径可能需要更改为绝对路径。