我在 PHP 文件中使用以下标准 Sendgrid WebAPI 代码,该代码在通过 Web 浏览器和 Cron wget 访问时成功发送电子邮件。但是,当我尝试使用 Cron php 执行它时,它不起作用。这是示例 SendGrid 代码:
$url = 'http://sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'example3@sendgrid.com',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'example@sendgrid.com',
);
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
print_r($response);
这是有效的cron:
*/5 * * * * root /usr/bin/wget -O /dev/null "http://www.mysite.com/directory/test.php"
这是我想要的 cron,但不起作用:
*/5 * * * * /usr/bin/php /var/www/html/directory/test.php
非常困惑...非常感谢您对此的帮助!