Member Avatar for dan_ord

Hi all,

I'm having a problem getting a cron job to execute properly.

I'm using my hosting control panel to set the task up and execute a script.

What I want the script to be able to do is search for jobs that were posted over 7 days ago, loop through the results and update the records returned and then send an email out to all the customers that were found.

I've tested the script by running it from a browser and it works perfectly fine.

Below is my script and below that is the errors im getting back off cron:

<?php

// Automated Script to check for jobs that have been posted and to email the customer and change the job status

require_once ('include/config.inc.php');

require_once (MYSQL);

$q = "SELECT * FROM inat_quote WHERE job_status = 'active' AND date_posted <= DATE_SUB(NOW(), INTERVAL 7 DAY)";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

	while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
		$quote_id = $row['quote_id'];
		$contact_email = $row['contact_email'];
		$contact_name = $row['contact_name'];
		$contact_surname = $row['contact_surname'];
		
		$q2 = "UPDATE inat_quote SET job_status = 'inactive' WHERE quote_id = '$quote_id'";
		$r2 = mysqli_query ($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));
		
		//Send an email to the customer who posted a job
		
				$to = "$contact_email";
				$subject = "Your job on  Has Expired.";
				
				$message = "
				
				<style type=\"text/css\">
				<!--
				.p_main {
				font-family: Arial, Helvetica, sans-serif;
				font-size: 14px;
				margin-left: 12px;
				margin-right: 12px;
				}
				#email_border {
				border: 2px solid #0099FF;
				position: relative;
				height: auto;
				width: 760px;
				margin-right: auto;
				margin-left: auto;
				}
				.orange {
				font-family: Arial, Helvetica, sans-serif;
				font-size: 14px;
				font-weight: 700;
				color: #F7923B;
				}
				-->
				</style>
				<body>
				<div id=\"email_border\">
				  <p><a href=\"http://www.net\"><img src=\"http://www.net/images/email_header.jpg\" alt=\"I Need A Tradesman\" width=\"750\" height=\"133\" border=\"0\" /></a></p>
				  <p class=\"p_main\">Hi $contact_name $contact_surname, Thank you for posting a job on .</p>
				  <p class=\"p_main\">It has been one week since you posted your job on our website. It has now been revomed from our records.</p>
				  <p class=\"p_main\">We value your honest feedback, we ask that when your job is completed that you could leave feedback on how the job was carried out. Please click this <a href=\"http://www.customer_feedback.php\">link</a> to go to our feedback page. Be sure to have your job reference number to hand.</p>
				  <p class=\"p_main\">We also appreciate your feedback about our website, if you spot something which needs improving or even just a suggestion, click on the contact us section of the website and let us know. We'll take this feedback on-board, which in turn will make for a better experience for you!</p>
				  <p class=\"p_main\"><strong>Your job reference is:</strong><span class=\"orange\"> $quote_id</span></p>
				  <p class=\"p_main\">Thank you,</p>
				  <p class=\"p_main\"><strong>.</strong></p>

				</div>
				</body>
				</html>";
				
    			$headers = 'From: info@testdomain.net' . "\r\n" .
				'Content-type: text/html; charset=iso-8859-1rn' . 
				'X-Mailer: PHP/' . phpversion();

				mail($to, $subject, $message, $headers);
	}
?>

Cron Errors:

httpdocs/auto_mailer.php: line 1: ?php: No such file or directory
httpdocs/auto_mailer.php: line 3: //: is a directory
httpdocs/auto_mailer.php: line 5: syntax error near unexpected token `'include/config.inc.php''
httpdocs/auto_mailer.php: line 5: `require_once ('include/config.inc.php');'

Anyone know where i'm going wrong? Can Cron do what i'm asking of it?

Any help would be greatly appreciated

Regards,

Dan

Recommended Answers

All 4 Replies

It looks like the cron is getting the php file raw, before it is actually processed by the php interpreter. Not familiar enough with cron to know how to fix. Any thoughts?

Hi all,

I'm having a problem getting a cron job to execute properly.

I'm using my hosting control panel to set the task up and execute a script.

What I want the script to be able to do is search for jobs that were posted over 7 days ago, loop through the results and update the records returned and then send an email out to all the customers that were found.

I've tested the script by running it from a browser and it works perfectly fine.

Below is my script and below that is the errors im getting back off cron:

<?php

// Automated Script to check for jobs that have been posted and to email the customer and change the job status

require_once ('include/config.inc.php');

require_once (MYSQL);

$q = "SELECT * FROM inat_quote WHERE job_status = 'active' AND date_posted <= DATE_SUB(NOW(), INTERVAL 7 DAY)";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

	while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
		$quote_id = $row['quote_id'];
		$contact_email = $row['contact_email'];
		$contact_name = $row['contact_name'];
		$contact_surname = $row['contact_surname'];
		
		$q2 = "UPDATE inat_quote SET job_status = 'inactive' WHERE quote_id = '$quote_id'";
		$r2 = mysqli_query ($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));
		
		//Send an email to the customer who posted a job
		
				$to = "$contact_email";
				$subject = "Your job on  Has Expired.";
				
				$message = "
				
				<style type=\"text/css\">
				<!--
				.p_main {
				font-family: Arial, Helvetica, sans-serif;
				font-size: 14px;
				margin-left: 12px;
				margin-right: 12px;
				}
				#email_border {
				border: 2px solid #0099FF;
				position: relative;
				height: auto;
				width: 760px;
				margin-right: auto;
				margin-left: auto;
				}
				.orange {
				font-family: Arial, Helvetica, sans-serif;
				font-size: 14px;
				font-weight: 700;
				color: #F7923B;
				}
				-->
				</style>
				<body>
				<div id=\"email_border\">
				  <p><a href=\"http://www.net\"><img src=\"http://www.net/images/email_header.jpg\" alt=\"I Need A Tradesman\" width=\"750\" height=\"133\" border=\"0\" /></a></p>
				  <p class=\"p_main\">Hi $contact_name $contact_surname, Thank you for posting a job on .</p>
				  <p class=\"p_main\">It has been one week since you posted your job on our website. It has now been revomed from our records.</p>
				  <p class=\"p_main\">We value your honest feedback, we ask that when your job is completed that you could leave feedback on how the job was carried out. Please click this <a href=\"http://www.customer_feedback.php\">link</a> to go to our feedback page. Be sure to have your job reference number to hand.</p>
				  <p class=\"p_main\">We also appreciate your feedback about our website, if you spot something which needs improving or even just a suggestion, click on the contact us section of the website and let us know. We'll take this feedback on-board, which in turn will make for a better experience for you!</p>
				  <p class=\"p_main\"><strong>Your job reference is:</strong><span class=\"orange\"> $quote_id</span></p>
				  <p class=\"p_main\">Thank you,</p>
				  <p class=\"p_main\"><strong>.</strong></p>

				</div>
				</body>
				</html>";
				
    			$headers = 'From: info@testdomain.net' . "\r\n" .
				'Content-type: text/html; charset=iso-8859-1rn' . 
				'X-Mailer: PHP/' . phpversion();

				mail($to, $subject, $message, $headers);
	}
?>

Cron Errors:

httpdocs/auto_mailer.php: line 1: ?php: No such file or directory
httpdocs/auto_mailer.php: line 3: //: is a directory
httpdocs/auto_mailer.php: line 5: syntax error near unexpected token `'include/config.inc.php''
httpdocs/auto_mailer.php: line 5: `require_once ('include/config.inc.php');'

Anyone know where i'm going wrong? Can Cron do what i'm asking of it?

Any help would be greatly appreciated

Regards,

Dan

It looks like the cron is getting the php file raw, before it is actually processed by the php interpreter. Not familiar enough with cron to know how to fix. Any thoughts?

The usual method of setting a cron job is a simple command line like the following:

php /home/<user>/public_html/cron.php

The above cron command will execute cron.php in the base of the users web directory and remember to replace <user> with your username for cpanel login.

Member Avatar for dan_ord

The usual method of setting a cron job is a simple command line like the following:

php /home/<user>/public_html/cron.php

The above cron command will execute cron.php in the base of the users web directory and remember to replace <user> with your username for cpanel login.

Hi,

Sorry for the late reply, however i've still not managed to sort the problem out.

I've included a screent shot of how i've got the cron task set up.

As far as i know what i've got in there should be ok.

httpdocs/ is my root folder and the path i've used is: httpdocs/auto_mailer.php.

Not to sound daft but would i need to change the file name to cron.php or can i have the file name as what ever i want?

Regards,

Dan

In that case you would need to use something like the following

php /home/<user>/httpdocs/auto_mailer.php

Where <user> is your cpanel login username assuming this is a linux machine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.