PHPMailer 6.9 - 不使用 CRONJOB 发送电子邮件

问题描述 投票:0回答:1

当我在浏览器中加载脚本时,它工作得很好, 但是当我使用 cronjob 运行脚本时,它不起作用。 我希望脚本既可以在浏览器中运行,也可以在由 cron 作业调用时运行。

这是script.php

    require_once 'init.php'; 
   
    $msg_id     = "custom_id" . time();
    $mb_html    = 'html body';
    $mb_text    = 'text body';

    $send = new Email();
    $success_mail_sent = $send->sendEmailWithPHPMailer(
            false,                      //$smtp true / false
            5,                          //$priority, 1/3/5
            $msg_id,                    //$msg_id,
            $configs['my_email'],       // $to_email,
            ucfirst(DOMAIN_NAME),       //$to_name,
            null,                       // $add_cc_email (null if we want without CC)
            null,                       // $subject_emoji, null / emoji
            'test',                     // $subject_text,
            $mb_html,
            $mb_text,
            false,                      // $getAcopy to my other mail
            'kkk'                       // script origin
            );
    if ($success_mail_sent == null) { // sending failed
        // do error handling stuff
    } else { // sending succeeded
       // do mail sent stuff
    }
    echo $success_mail_sent;
script.php 中调用的

Email.php 类如下所示:

<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; use PHPMailer\PHPMailer\SMTP; class Email { public static function sendEmailWithPHPMailer( $smtp, $priority, $msg_id, $to_email, $to_name, $add_cc_email=null, $subject_emoji, $subject_text, $mail_body_html, $mail_body_text, $getAcopy, $origine=null) { require $_SERVER['DOCUMENT_ROOT'].'/path/to/site_settings.php'; $mail = new PHPMailer; if($smtp == true){ $mail->isSMTP(); $mail->Host = 'mail.domain.com'; $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = 'password'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->ContentType = "text/html; charset=utf-8\r\n"; } $mail->Priority = $priority; $mail->setFrom($configs['my_email'], ucfirst(DOMAIN_NAME)); $mail->Sender=$configs['my_email']; $mail->addAddress($to_email, $to_name); if($getAcopy == true){ $mail->addBCC($configs['my_other_email'], ucfirst(DOMAIN_NAME)); } if($add_cc_email !== null){ $mail->addCC($add_cc_email); // For CC } $mail->addReplyTo($configs['my_email'], ucfirst(DOMAIN_NAME)); $mail->BouncedTo = $configs['my_email']; $mail->MessageID = $msg_id; $mail->Date = date('r', time()); $mail->WordWrap = 50; $mail->isHTML(true); $mail->CharSet = "UTF-8"; $mail->Subject = $subject_emoji.$subject_text; $mail->Body = $mail_body_html; $mail->AltBody = $mail_body_text; if(!$mail->send()) { // echo 'Message could not be sent. '; // echo 'Mailer Error: ' . $mail->ErrorInfo; $success_mail_sent = null; } else { // echo 'Message has been sent<br> // MessageID: '. $msg_id; $success_mail_sent = $mail->getLastMessageID(); } unset($mail); return ($success_mail_sent); } }
另外,我在 script.php 开头调用的 

init.php 文件如下所示:

<?php // initialization file that we include in each page session_start(); spl_autoload_register(function($class){ $class = str_replace("\\", DIRECTORY_SEPARATOR, $class); $phpMailerClassesArray = array('DSNConfigurator', 'Exception', 'OAuth.php', 'OAuthTokenProvider', 'PHPMailer', 'POP3', 'SMTP'); if(in_array($class, $phpMailerClassesArray)){ require_once 'classes/PHPMailer/PHPMailer/' .$class. '.php'; } else { require_once 'classes/' .$class. '.php'; } });
注意:我有 2 个文件夹,其中有类:classes/(适用于所有类)和classes/PHMailer/PHMailer/(仅适用于 php 邮件程序类)

我做错了什么?我该如何解决这个问题?谢谢!

php email cron phpmailer
1个回答
0
投票
经过一些分钟的调试,我得出的结论是 site_settings.php 的路径是错误的:

require $_SERVER['DOCUMENT_ROOT'].'/path/to/site_settings.php';
$_SERVER['DOCUMENT_ROOT'] 为空...剩下的字符串是:require '/path/to/site_settings.php'。
该路径不应以“/”开头。
因此,如果不加载 site_settings.php,则无法提供发件人电子邮件和发件人姓名以及 PHPMailer 类中所需的其他变量。所以它没有发送邮件。

我纠正了它,现在它就像一个魅力。

在这种情况下,Email.php 类中已更正。 感谢大家的帮助和启发!特别是ADyson,加油兄弟!

© www.soinside.com 2019 - 2024. All rights reserved.