Jeff
Jeff

Reputation: 191

Send email with Sendgrid WebAPI and Cron PHP

I'm using the following standard Sendgrid WebAPI code in a PHP file which sends email successfully when accessed through the web browser and with Cron wget. However, when I try to execute it with Cron php, it doesn't work. Here is the sample SendGrid code:

$url = 'http://sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD'; 

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => '[email protected]',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => '[email protected]',
  );


$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);

Here is the cron that works:

*/5 * * * * root /usr/bin/wget -O /dev/null "http://www.mysite.com/directory/test.php"

Here is the cron that I want, but doesn't work:

*/5 * * * * /usr/bin/php /var/www/html/directory/test.php

Very confused... would really appreciate some help on this!

Upvotes: 1

Views: 1769

Answers (1)

Jonathan Barlow
Jonathan Barlow

Reputation: 1075

Usually, you would write a php script that can be run from the command line (or a cron job) like this:

#!/usr/bin/php
<?php

...


?>

You'll just need to be sure /usr/bin/php is the path to the php executable. Find that by typing "whereis php" or "which php"

My guess is that's why your script isn't running.

Upvotes: 1

Related Questions