I have an Apache server that I’m hosting from.
I have a php page that sends emails based on some script that looks like this:
<?php
chdir(dirname(__FILE__)); //need this line so cron works! cron doesn't know
the relative file paths otherwise.
require_once 'core/init.php';
$db = DB::getInstance();
$company = new Company(1);
require 'added-assets/plugins/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
if($company->find_yesterday_appts(1)) {
.... email based on query....
From cpanel I have the cron job set to run every day at 12.
0 12 * * * php -q public_html/personnellonline/email_yesterdays_appts.php
core/init.php contains my connection string.
There are no errors I was told on the server, but no emails are ever sent when the cron runs. If I go to the page directly though then the query runs and emails are sent!
I once solved this issue by adding:
chdir(dirname(__FILE__)); //need this line so cron works! cron doesn't
know the relative file paths otherwise.
But not I’m back to square one. Any thoughts on what could be the issue?
2
Answers
GracefulRestart, I changed the command to use CURL which I never used or heard of. It works now, thank you. I'm learning about CURL now as I read online.
ArtisticPhoenix, you are correct. I should move this outside the public area of my domain. I will do this!
The following works now:
Thank you for your comments and help.
When you say “I go to the page directly”, how exactly are you getting things to work? Does running the PHP command you showed us work without using cron?
If you are loading the page through a web server, that is not the same as executing the file through PHP as you are doing in your cron command. If your script needs to be run through the web server, try using curl to load the URL that works in your cron command.
The only other thing I could think of, if running the PHP command actually works when not run through cron, would be that you may need a specific user to execute your cron command.