簡體   English   中英

Cronjob沒有運行php腳本

[英]Cronjob not running php script

我在這篇文章之前做了一些搜索,但我似乎仍然無法讓它工作。 我正在嘗試使用 PHPMailer 設置一個 cron 作業,以便每隔一段時間發送一封電子郵件。 如果我手動運行,下面的腳本確實有效,但在 cron 作業調度程序中不起作用。

對於此示例 - 我將其設置為每分鍾運行一次。 我認為它必須對“vendor/autoload.php”做一些事情並且它的路徑沒有正確加載? 出於安全原因以及這篇文章的收件人,我沒有使用 api 密鑰添加我的 SMTP 憑據。


這是我在 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}";
}

如果有人可以幫助我,我將不勝感激!

你能與我們分享你的錯誤信息嗎? 我認為這對找到問題有很大幫助。

我已經分享了如何在堆棧溢出的不同帖子中啟用登錄的見解(請參閱下面的鏈接)。 這將解釋如何在 cron 執行中顯示錯誤:

https://stackoverflow.com/a/60250715/12880865

如果這對您有幫助,請告訴我。 如果您收到正確的錯誤消息,請與我們分享,以便我們進一步深入研究您的問題。

固定的!

我不得不使用(dirname( DIR )這是文件的目錄。

我變了:

require 'vendor/autoload.php';

到:

require (dirname(__DIR__).'/mailer/vendor/autoload.php');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM