Wednesday, February 08, 2012

php shell script that will run runs at specific hour with crontab and send emails with the html form extracted content


Here is how to create a daily crontab script that runs at specific hour 
gets the web content and then saves the output in a file folder (the form must use a sumbit button with post) and then 
it send email with the html file 

you need to install php5-cli , php-pear 
sudo apt-get install php5-cli php-pear

and then from pear Mail , Mail_Mime http://pear.php.net/package/Mail_Mime/redirected

sudo pear install Mail_Mime
sudo pear install Mail

crontab -l

# m h  dom mon dow   command
0 1 * * * /home/ubuntu/curl_cron.php


cat /home/ubuntu/curl_cron.php


#!/usr/bin/php
<?php
include 'Mail.php';
include 'Mail/mime.php' ;
$url = 'https://www.google.com/search?hl=en&gl=us&tbm=nws&btnmeta_news_search=1&q=android&oq=android';
$emails = 'example@example.com';
$path = '/home/ubuntu/';
$today = getdate();
$day= $today["mday"];
$month= $today["mon"];
$year= $today["year"];
// The submitted form data, encoded as query-string-style
// name-value pairs
$post_body = 'submit=android';
$curl_handle = curl_init ($url);
curl_setopt ($curl_handle, CURLOPT_POST, true);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $post_body);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
$curl_output = curl_exec ($curl_handle);
$file_path = "$path"."curl.out/android-news-$month-$day-$year.html";
$fh = fopen ($file_path,'w');
fwrite($fh,$curl_output);
fclose($fh);
curl_close ($curl_handle);
$text = " Google Android News Update $month-$day-$year";
$crlf = "\n";
$hdrs = array(
'From' => 'example@example.com',
'Subject' => $text
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text);
$mime->addAttachment($file_path, 'text/html');
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send($emails, $hdrs, $body);
?>

No comments: