【问题标题】:Cronjob not running php scriptCronjob 没有运行 php 脚本
【发布时间】:2020-02-28 03:42:21
【问题描述】:

在这篇文章之前我已经进行了一些搜索,但我似乎仍然无法让它工作。我正在尝试使用 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}";
}

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

【问题讨论】:

    标签: php cron


    【解决方案1】:

    你能和我们分享你的错误信息吗?我认为这将有助于找到问题。

    我已经分享了我的见解如何在堆栈溢出的不同帖子中启用日志记录(请参阅下面的链接)。这将解释如何在 cron 执行中显示错误:

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

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

    【讨论】:

    • 作为评论可能会更好,因为它没有提供问题的解决方案。
    • 谢谢克里斯托夫!很好的主意。我会在早上查看您的帖子并回复您。
    • @Christoph Kluge 它没有得到我的最终答案,但它帮助我到达了那里,所以谢谢!
    • @code 很高兴它有帮助。只是出于好奇。您想在检查错误消息后分享您遇到的错误吗?缺少安装的 php 包?缺少依赖?权限错误?
    【解决方案2】:

    已修复!

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

    我变了:

    require 'vendor/autoload.php';
    

    收件人:

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

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 2018-04-09
      • 2013-01-26
      • 1970-01-01
      • 2019-08-16
      • 2012-04-18
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多