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

时间:2012-04-06 00:28:38

标签: php cron crontab sendgrid

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

非常困惑......真的很感激一些帮助!

1 个答案:

答案 0 :(得分:1)

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

#!/usr/bin/php
<?php

...


?>

你只需要确保/ usr / bin / php是php可执行文件的路径。通过输入“whereis php”或“which php”

来查找

我的猜测是你的脚本没有运行的原因。

相关问题