简体   繁体   English

无法从命令行使用Mail.php

[英]Cannot use Mail.php from command line

I have a php script that is going to be a cron job and it was working fine until I added the email script I have been using. 我有一个php脚本,它将成为cron作业,并且在添加我一直在使用的电子邮件脚本之前,它工作正常。

I have used the email script all over the site before but when I try and run the cron job using it from the command line it fails. 我以前在整个站点上都使用过电子邮件脚本,但是当我尝试从命令行使用它运行cron作业时,它将失败。

The mail script looks as followed 邮件脚本如下所示

    <?php

require_once "Mail.php";

function sendMail($to, $from, $subject, $message) {

        $no_errors = true;

        $host = "ssl://smtp.server.com";
        $port = "465";
        $username1 = "username";
        $password = "password";

        $headers = array ('From' => 'myfromemail@myemail.com',
          'To' => $to,
          'Subject' => $subject);
        $smtp = Mail::factory('smtp',
          array ('host' => $host,
            'port' => $port,
            'auth' => true,
            'username' => $username1,
            'password' => $password));

        $mail = $smtp->send($to, $headers, $message);

        if (PEAR::isError($mail)) {

            $no_errors = false;

            echo $mail->getMessage();

        } else {

            $no_errors = true;

         }

        return $no_errors;

        }

?>

If I comment out the include send_mail.php it works fine but doesn't send email. 如果我注释掉include send_mail.php,它可以正常工作,但不发送电子邮件。

Any ideas? 有任何想法吗?

You will need to make sure that, when the script is ran from cron , it has the proper working folder set, so it can load mail.php . 您需要确保从cron运行脚本时,它具有正确的工作文件夹设置,以便可以加载mail.php Using the getcwd() and chdir() functions are the two you need to use. 需要使用getcwd()chdir()函数。

Usually, when ran from web browser (on an Apache server), the working directory is /var/www , but from cron , it is usually the root of the filesystem, / . 通常,从Web浏览器(在Apache服务器上)运行时,工作目录为/var/www ,但从cron ,它通常是文件系统/的根目录。

When including external files, you need to be conscious of where the file is, and then where PHP will look for it. 当包含外部文件时,您需要知道文件在哪里,然后在PHP查找它的位置。 You can inspect where it will look with get_include_path() and set it with set_include_path() . 您可以检查它的外观与get_include_path() ,并把它通过set_include_path() 。 When you set a new one, you want to ADD (not replace) the directory where Mail.php is installed. 设置新目录时,您想添加(而不是替换)Mail.php的安装目录。 Use get_include_path() . PATH_SEPARATOR . $yourPath 使用get_include_path() . PATH_SEPARATOR . $yourPath get_include_path() . PATH_SEPARATOR . $yourPath get_include_path() . PATH_SEPARATOR . $yourPath as the new value. get_include_path() . PATH_SEPARATOR . $yourPath作为新值。

The standard folder when you're running a cron is probably different to the folder in which this include is contained. 运行cron时,标准文件夹可能不同于包含此include的文件夹。 Try this: 尝试这个:

require_once realpath( dirname( __FILE__ ) . '/Mail.php' );

In the general case, you can add in .. or folder structures as required - and it should still work. 在一般情况下,您可以根据需要添加..或文件夹结构-它仍然可以正常工作。

Try "locate Mail.php" from Command line, and then try full path of this script in your require_once 在命令行中尝试“找到Mail.php”,然后在您的require_once中尝试此脚本的完整路径。

Also try to dump the response into a log file, when it's ran via cron: 当通过cron运行响应时,也请尝试将响应转储到日志文件中:

Cron Job Log - How to Log? Cron作业日志-如何记录?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM