PHP Fatal Error: require_once(Mail.php): Failed opening required 'Mail.php'

107 views Asked by At

I am encountering a fatal error in my PHP script and would appreciate some assistance in resolving it. It is a Cron job that generates a report every Monday and mails it. It was working fine about a month ago but now I am getting this warning & error in emails. The error message is as follows:

Warning: require_once(Mail.php): failed to open stream: No such file or directory in /home/www/healthSolutions/migrations/hs_report.php on line 2 Fatal error: require_once(): Failed opening required 'Mail.php' (include_path='.:/usr/local/php7/lib/php') in /home/www/healthSolutions/migrations/hs_report.php on line 2

Code snippet:

<?php
 require_once "Mail.php";
ini_set('display_errors', 1); //php inif file (error temporary shows)
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

The PHP version I am using is PHP 7.

1

There are 1 answers

3
gseidel On

If you require or include a file in php, it will look at several places to find your file.

It is a combination of your include_path + required file name. In your case, it will look at:

  • /home/www/healthSolutions/migrations/Mail.php
  • /usr/local/php7/lib/php/Mail.php

It will also look at the current working directory + required file name. In case of a cronjob, it will be the home directory of the user. So if you execute it as root, php will look also at:

  • /root/Mail.php

If the Mail.php is not found at any of that places, you will get the error you show us.

Because we don't know where is Mail.php in your System, I can't provide any examples, but to solve this you have two good options.

We just assume the file is located at: /home/www/healthSolutions/Mail.php

In your CronJob, just cd first to the path where the Mail.php is:

* * * * * cd /home/www/healthSolutions && php migrations/hs_report.php

Or you change the path on your hs_report.php with __DIR__ + relative path (Most Recommended)

<?php

require_once __DIR__ . "/../Mail.php";
ini_set('display_errors', 1); //php inif file (error temporary shows)
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Why it has changed?

I don't know, but it don't need to be a code change. Your CronJob, Home Directory or PHP Setting (inlude_path) may have changed.