Cronjob没有运行php脚本

时间:2020-02-26 08:14:30

标签: php cron

在发布本文之前,我已经进行了一些搜索,但似乎仍然无法正常工作。我正在尝试使用PHPMailer设置cron作业,以便每隔一段时间发送一次电子邮件。如果我手动运行以下脚本,则该脚本可以工作,但在cron作业计划程序中不起作用。

对于此示例-我将其设置为每分钟运行一次。我在想它必须对“ vendor / autoload.php”做些什么,并且路径无法正确加载?出于安全原因,我没有为我的SMTP凭据添加api密钥,也没有为此帖子添加收件人。


这是我在Cpanel中设置的Cron职位。 enter image description here


这是我的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}";
}

如果有人可以帮助我,我将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可以与我们分享错误消息吗?我认为这将有助于发现问题。

我分享了我的见解,即如何在堆栈溢出时启用登录其他帖子的功能(请参阅下面的链接)。这将说明您如何在cron执行中显示错误:

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

请告诉我这是否对您有帮助。如果您会收到正确的错误消息,请与我们分享,以便我们进一步探讨您的问题。

答案 1 :(得分:0)

已修复!

我必须使用(dirname(DIR),它是文件的目录。

我更改了:

require 'vendor/autoload.php';

收件人:

require (dirname(__DIR__).'/mailer/vendor/autoload.php');
相关问题