简体   繁体   中英

php script not sending mail using phpmailer when using a cronjob but does mail when accessing script directly

I have a script that sends an email using phpmailer.

This is my script:

<?PHP
require "/home/websitename/public_html/new/phpMailer/class.phpmailer.php";
// Connectie script
class Connection {
    // Configure Database Vars
    private $host     = 'localhost';
    private $username = 'username';
    private $password = 'mypass';
    private $db_name  = 'mydbname';
    public $db;

    function __construct() {
        // Create connection
        $db = new mysqli($this->host, $this->username, $this->password, $this->db_name);
        // Check connection
        if ($db->connect_errno > 0) {
            die('Unable to connect to the database: '.$db->connect_error);
        }
        $this->db = $db;
    }

    public function query($query) {
        $db = $this->db;
        $this->db->query('SET NAMES utf8');
        if (!$result = $this->db->query($query)) {
            die('There was an error running the query ['.$db->error.']');
        } else {
            return $result;
        }
    }

    public function multi_query($query) {
        $db = $this->db;
        if (!$result = $this->db->multi_query($query)) {
            die('There was an error running the multi query ['.$db->error.']');
        } else {
            return $result;
        }
    }

    public function real_escape_string($value) {
        return $this->db->real_escape_string($value);
    }

    public function inserted_id() {
        return $this->db->insert_id;
    }
}

$conn = new Connection();

$getaccounts = 'SELECT * FROM accounts WHERE verified_email = 0';
$getaccountscon = $conn->query($getaccounts);
while($getaccounts = $getaccountscon->fetch_assoc()){
  $timer = strtotime($getaccounts['timer']);

  $time = '1 minuut voorbij van account van '.$getaccounts['voornaam'];
  $mail = new PHPMailer;
  $mail->CharSet = 'UTF-8';
  $mail->From = 'noreply@website.nl';
  $mail->FromName = 'website.nl';
  $mail->addAddress('twan@website.nl');
  $mail->isHTML(true);
  $mail->Subject = 'Verifieer je e-mail bij website.nl';
  $mail->msgHTML($time);
  if(!$mail->send())
  {
    echo 'Er is iets foutgegaan tijdens het verzenden van de verificatie mail';
  }
}

?>

When I open it directly through the browser, it works fine. But when I use it in a cronjob I get no emails at all. This is my cronjob command: wget -O /dev/null -quiet https://mywebsite.nl/new/includes/checkemailverify.php >/dev/null 2>&1

What can be causing the issue?

The user running the cron may have a different user than your web server's user. So there may be some environment variables like PATH that could be undefined, and cron be unable to find wget . To fix PATH you must provide in crontab the fully qualified path th wget .

Assuming you are on a *nix box, login as the user whose crontab will be executed. Find where wget is on your file system:

DarkMax:~ yvesleborg$ which wget
/usr/local/bin/wget
DarkMax:~ yvesleborg$ ls -al /usr/local/bin/wget
lrwxr-xr-x  1 yvesleborg  admin  30 27 Sep  2018 /usr/local/bin/wget -> ../Cellar/wget/1.19.5/bin/wget

Notice, that wget is executable by system user yvesleborg . Make certain it is for the user running the cron job.

Then edit your crontab

crontab -e

and change wget to the fully qualified path you got above. In your case that would make your command :

/fully/qualified/path/to/wget -O /dev/null -quiet https://mywebsite.nl/new/includes/checkemailverify.php >/dev/null 2>&1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM