Cronjob未运行php脚本
在发布这篇文章之前,我已经做了一些搜索,但我似乎仍然无法让它发挥作用。我正在尝试使用PHPMailer设置一个cron作业,以便每隔一段时间发送一封电子邮件。如果我手动运行下面的脚本,它确实可以工作,但在cron作业调度器中不工作 对于本例,我将其设置为每分钟运行一次。我认为它和“vendor/autoload.php”有关,它的路径没有正确加载?出于安全原因以及此帖子的收件人原因,我没有使用api密钥添加SMTP凭据Cronjob未运行php脚本,php,cron,Php,Cron,在发布这篇文章之前,我已经做了一些搜索,但我似乎仍然无法让它发挥作用。我正在尝试使用PHPMailer设置一个cron作业,以便每隔一段时间发送一封电子邮件。如果我手动运行下面的脚本,它确实可以工作,但在cron作业调度器中不工作 对于本例,我将其设置为每分钟运行一次。我认为它和“vendor/autoload.php”有关,它的路径没有正确加载?出于安全原因以及此帖子的收件人原因,我没有使用api密钥添加SMTP凭据 这是我在Cpanel中的cron作业设置。 这是我的PHPMailer
这是我在Cpanel中的cron作业设置。
这是我的PHPMailer代码:
// 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\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
// Server settings
// $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = ''; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
// Recipients
$mail->setFrom('email@email.com', '');
$mail->addAddress('email@email.com', ''); // Add a recipient
$mail->addReplyTo('email@email.com', '');
// $mail->addCC('cc@example.com');
// $mail->addBCC('');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'PHPMailer email';
// $mail->Body = 'This is the HTML message body <b>in bold!</b>';
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->msgHTML(file_get_contents('email.html'), __DIR__); // Use this if not using the above code
// ********* PHP-MAILER ********* //
$mail->send();
echo 'Email sent!';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
//将PHPMailer类导入全局命名空间
//它们必须位于脚本顶部,而不是函数内部
使用PHPMailer\PHPMailer\PHPMailer;
使用PHPMailer\PHPMailer\SMTP;
使用PHPMailer\PHPMailer\Exception;
//加载作曲家的自动加载器
需要“vendor/autoload.php”;
//实例化并传递'true'将启用异常
$mail=新的PHPMailer(true);
试一试{
//服务器设置
//$mail->SMTPDebug=SMTP::DEBUG_SERVER;//启用详细调试输出
$mail->isSMTP();//使用SMTP发送
$mail->Host='';//设置要通过的SMTP服务器
$mail->SMTPAuth=true;//启用SMTP身份验证
$mail->Username='';//SMTP用户名
$mail->Password='';//SMTP密码
$mail->SMTPSecure=PHPMailer::ENCRYPTION\u STARTTLS;//启用TLS加密;`PHPMailer::ENCRYPTION\u SMTPS`也被接受
$mail->Port=587;//要连接的TCP端口
//接受者
$mail->setFrom('email@email.com', '');
$mail->addAddress('email@email.com“,”;//添加收件人
$mail->addReplyTo('email@email.com', '');
//$mail->addCC('cc@example.com');
//$mail->addBCC(“”);
//内容
$mail->isHTML(true);//将电子邮件格式设置为HTML
$mail->Subject='PHPMailer-email';
//$mail->Body='这是以粗体显示的HTML邮件正文!';
//$mail->AltBody='这是非HTML邮件客户端的纯文本正文';
$mail->msgHTML(file_get_contents('email.html'),uu DIR_uu);//如果不使用上述代码,请使用此选项
//*********PHP-MAILER************//
$mail->send();
回音“已发送电子邮件!”;
}捕获(例外$e){
echo“无法发送邮件。邮件程序错误:{$mail->ErrorInfo}”;
}
如果有人能帮助我,我将非常感激 你能和我们分享你的错误信息吗?我想这将有助于找到问题所在 我已经分享了我的见解,如何在堆栈溢出的另一篇文章中启用日志记录(请参阅下面的链接)。这将解释如何在cron执行中显示错误: 如果这对你有帮助,请告诉我。如果您收到正确的错误消息,请与我们分享,以便我们进一步深入了解您的问题。已修复 我必须使用文件的目录 我改变了:
require 'vendor/autoload.php';
require (dirname(__DIR__).'/mailer/vendor/autoload.php');
收件人:require 'vendor/autoload.php';
require (dirname(__DIR__).'/mailer/vendor/autoload.php');
这可能是一个更好的评论,因为它没有提供isse的解决方案。谢谢Christoph!好主意。我会在早上查看你的帖子并回复你。@Christoph Kluge它没有得到我的最终答案,但它帮助我到达了那里,所以谢谢你@代码很高兴它有帮助。只是出于好奇。是否要共享检查错误消息后出现的错误?缺少安装的php包?缺少依赖关系?权限错误?