【问题标题】:php script not sending mail using phpmailer when using a cronjob but does mail when accessing script directlyphp脚本在使用cronjob时不使用phpmailer发送邮件,但在直接访问脚本时发送邮件
【发布时间】:2020-04-15 11:02:15
【问题描述】:

我有一个使用 phpmailer 发送电子邮件的脚本。

这是我的脚本:

<?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';
  }
}

?>

当我直接通过浏览器打开它时,它工作正常。但是当我在 cronjob 中使用它时,我根本没有收到任何电子邮件。这是我的 cronjob 命令:wget -O /dev/null -quiet https://mywebsite.nl/new/includes/checkemailverify.php &gt;/dev/null 2&gt;&amp;1

什么可能导致问题?

【问题讨论】:

  • 您是否尝试过使用wget,而是在您的cronjob 中直接通过php 调用脚本?
  • Shell 和 web 有不同的属性:users、envs 和其他。
  • @kerbholz 是的,我之前尝试过但没有结果,这就是我尝试通过浏览器进行操作的原因。
  • 在你的 crontab 中给出 wget 的完整路径。
  • @YvesLeBorg 你能给我举个例子吗?我对 cronjobs 不是很熟悉。

标签: php email cron


【解决方案1】:

运行 cron 的用户可能与您的 Web 服务器的用户不同。所以可能有一些环境变量像 PATH 可能是未定义的,并且 cron 无法找到wget。要修复 PATH,您必须在 crontab 中提供完全限定的路径 th wget

假设您在 *nix 机器上,以将执行其 crontab 的用户身份登录。查找 wget 在您的文件系统上的位置:

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

注意,wget 可由系统用户yvesleborg 执行。确保它适用于运行 cron 作业的用户。

然后编辑你的 crontab

crontab -e

并将 wget 更改为您在上面得到的完全限定路径。在您的情况下,这将使您的命令:

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

【讨论】:

    猜你喜欢
    • 2013-12-22
    • 1970-01-01
    • 2013-07-13
    • 2013-03-13
    • 2011-09-16
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多