使用 Sendgrid WebAPI 和 Cron PHP 发送电子邮件


Send email with Sendgrid WebAPI and Cron PHP

我在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

很困惑...非常感谢这方面的帮助!

通常,你会编写一个可以从命令行(或cron作业)运行的php脚本,如下所示:

#!/usr/bin/php
<?php
...

?>

你只需要确保/usr/bin/php 是 php 可执行文件的路径。 通过键入"whereis php"或"哪个php"找到它

我的猜测是,这就是您的脚本无法运行的原因。