【发布时间】: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 >/dev/null 2>&1
什么可能导致问题?
【问题讨论】:
-
您是否尝试过不使用
wget,而是在您的cronjob 中直接通过php调用脚本? -
Shell 和 web 有不同的属性:users、envs 和其他。
-
@kerbholz 是的,我之前尝试过但没有结果,这就是我尝试通过浏览器进行操作的原因。
-
在你的 crontab 中给出 wget 的完整路径。
-
@YvesLeBorg 你能给我举个例子吗?我对 cronjobs 不是很熟悉。