PHPMailer可在浏览器中工作,但不能用作cron作业

问题描述

我有一个简单的PHP文件,可使用PHPMailer发送电子邮件。从浏览器运行时,效果很好。 当我尝试从Cron作业运行它时,我没有收到电子邮件。我已经在这里找了一段时间了,但是我一直无法使它起作用。 大多数解决方案都与PHP代码中的相对路径有关。如果cron作业的路径不同,我将require行上的路径更改为绝对路径

我对所有这些都是新手,因此将不胜感激。 我可能缺少一些简单的东西。

预先感谢您的帮助。

<?PHP
/* Namespace alias. */
// Import PHPMailer classes into the global namespace
// These must be at the top of your script,not inside a function

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

/* If you installed PHPMailer without Composer do this instead: */

require '/hermes/bosnaweb21a/b1837/ipw.whitmerd/public_html/test03/include/PHPMailer/PHPMailer.PHP';
require '/hermes/bosnaweb21a/b1837/ipw.whitmerd/public_html/test03/include/PHPMailer/Exception.PHP';
require '/hermes/bosnaweb21a/b1837/ipw.whitmerd/public_html/test03/include/PHPMailer/SMTP.PHP';

//SMTP needs accurate times,and the PHP time zone MUST be set
date_default_timezone_set('America/New_York');

echo PHPinfo();
echo __DIR__;

// Email the results
$mailSubject = "Email Subject";

// Create the body of the mail
$mailBody = "Body of email";

/* Create a new PHPMailer object. Passing TRUE to the constructor enables exceptions. */
$mail = new PHPMailer(TRUE);

// Configure the mail object for iPower email account
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Host = "smtp.ipower.com";
$mail->Port = 25;
$mail->SMTPSecure = 'tls';
$mail->isHtml(true);
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$mail->Username = "validuser";
$mail->Password = "password";
$mail->setFrom('[email protected]','Example Name');
$mail->Subject = $mailSubject;
$mail->addAddress('[email protected]','Example Name');
$mail->Body = $mailBody;
$mailSendSuccess = $mail->send();
$mail = null;

// Success or failure message
if ($mailSendSuccess) {
    echo "<br>Email was sent sucessfully.";
} else {
    echo "<br>There was a problem sending the email.";
}

我想从cron作业中获取作业的PHPinfo()信息。因此,我试图在已知位置创建文件。 我尝试了这种简单的文件操作,该操作在浏览器中效果很好,但在cron作业中效果不佳。 我注释掉了将PHPinfo捕获到文件中以仅创建一个虚拟文件代码,但该代码不起作用。

<?PHP

$tempDir = '/hermes/bosnaweb21a/b1837/ipw.whitmerd/public_html/test03/misc/';
$tempFile = 'PHPini_fromcron.html';

$fileContents = dirname(__DIR__);
$fileContents .= '<br>';
$fileContents .= 'Test file contents.';

// ob_start();
// PHPinfo();
// $PHPini = ob_get_contents();
// ob_end_clean();

// $fileContents .= $PHPini;

$createFile = fopen($tempDir . $tempFile,'w');
if (fwrite($createFile,$fileContents)) {
    echo "File created.";
} else {
    echo "There were problems.";
}
fclose($createFile);

解决了吗?

原来是目录名有问题。我不知道是什么,因为权限设置相同。

如果有人对原因有一个理论,我很想听听。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)