简体   繁体   中英

How to correctly set up a cron job that runs a PHP script which sends an email?

I've put together a PHP script that sends some content to a predefined email address and wanted to set up a cron job that would run that PHP script each day. Additionally, I've set it up to also send me an email each time the cron runs. I should mention that this script uses the PHP mail() function to send the email in html format and that I've tested if the script works by accessing directly through my browser's address bar.

The cron command: php /home/myuserid/public_html/projects/jobs/index.php

Surprisingly, when the cron ran, I received the following error message through email:

Use of uninitialized value in concatenation (.) or string at /usr/bin/sendmail line 15. Content-type: text/html

I'm not even sure where to start looking for the problem, so I need to ask: how do I correctly set up a simple cron job that runs a PHP scripts which sends an email using the mail() function?

It's difficult to say exactly what the problem is without looking at the script itself, but the underlying issue was that the output stream was being sent to the wrong place. In a standard POSIX environment there are 2 standard output streams, standard output ( stdout ) and standard error ( stderr ). When viewing the script in a web browser stdout will be passed to the browser and stderr will be handled by Apache and normally sent to a logger.

When calling from the command line this is different: they could be expected to print to the console (which is the standard stdout ), but are dependent on things like the script and the CLI php.ini . In your case the line referencing the Content-Type indicates that the output is being sent to the wrong location. You can redirect the output using shell (normally BASH) redirection. You use > for write and >> for append and you can also indicate the output stream where the default is stdout and 2 represents stderr . The output can be sent anywhere such as a basic logging file, or if it is not needed at all it can be discarded by sending it to the special device /dev/null .

So....the provided solution of

php /home/myuserid/public_html/projects/jobs/index.php 2> /dev/null

runs your script while discarding errors sent to stderr which were previously getting sent to the wrong destination and causing some receiver to throw up.

Did you check that the script execute properly outside of a cron? What happens when you run the same command on your shell?

You have to make sure your server has a SMTP Server active. Plus if it's saying you have a concatenation problem then you've done something wrong along lines like;

$output = $string . " and " . $string;

or like this;

$output = "Hello";
$output .= " there!";

The method of running cron seems correct. There must be a problem with your mail sending. Your server may not support using mail() function. You can prefer smtp mail using PhpMailer .

Try setting headers:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

mail($to, $subject, $message, $headers);

I've contacted my hosting provider and they've changed the cron command themselves to php /home/myuserid/public_html/projects/jobs/index.php 2> /dev/null , which did the trick. @Matt Whipple suggested a partial fix (ie adding /dev/null to the command line).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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