Hello I have made a script that is run via a cron to insert emails into a database table however I am having problems when I run it via a cron but yet it works just fine in a regular browser.
here is what the cron comand looks like:
/2 * php -q /home/pcgeek/public_html/p/mailcheck.php
here is the error cron reports:
<br />
<b>Warning</b>: mysql_query(): Access denied for user: 'pcgeek@localhost' (Using password: NO) in <b>/home/pcgeek/public_html/p/mailcheck.php</b> on line <b>28</b><br />
<br />
<b>Warning</b>: mysql_query(): A link to the server could not be established in <b>/home/pcgeek/public_html/p/mailcheck.php</b> on line <b>28</b><br />
<br />
<b>Warning</b>: mysql_close(): 3 is not a valid MySQL-Link resource in <b>/home/pcgeek/public_html/p/mailcheck.php</b> on line <b>32</b><br />
Now for the script itself:
mailcheck.php
<?
require 'common.php';
// conects to imap server
$link=imap_open($server,$email,$password);
$headers=imap_headers($link);
$numEmails = sizeof($headers);
// gets the emails off the imap server
for($i = 1; $i < $numEmails+1; $i++){
$mailHeader = @imap_headerinfo($link, $i);
$id = $mailHeader->message_id;
$date = $mailHeader->date;
$from = $mailHeader->fromaddress;
$subject = strip_tags($mailHeader->subject);
$message = @imap_body($link, $i);
$message = addslashes($message);
$sql1 = mysql_query("INSERT INTO `sms_emails` ( `mailid` , `date` , `from` , `subject` , `message` ) VALUES ( '$id', '$date', '$from', '$subject', '$message' )");
// deletes the email after its been inported into db
// imap_delete($link, $i);
}
mysql_close($dbcnx);
imap_close($link);
?>
common.php (*note removed connection vars for security)
<?
// Database conection setings
$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
// mail acount settings
$server="";
$email = "";
$password = "";
// makes the conection to the db
function dbopen($dbname = ""){
global $dbuser,$dbpass,$dbhost,$dbname;
//Made the connection
$startconnection = @mysql_connect($dbhost, $dbuser, $dbpass) or print "Unable to connect to the Database.";
@mysql_select_db($dbname, $startconnection) or print "Unable to select database";
return $startconnection;
}
?>
Any ideas why it is not conecting to the mysql database properly when run via a cron but it works fine through a browser? Thanks.