简体   繁体   English

phpmailer-使用php从命令行运行时出错

[英]phpmailer - error when run from command line with php

I got a strange behavior. 我有一个奇怪的行为。 I am using xampp installed on Win7 and PHPMailer class. 我正在使用Win7和PHPMailer类上安装的xampp。 When I run a simple .php script to send a mail from the browser, all goes well but if I run the same script using command prompt with php sufix (c:..... php c:\\xampp\\htdocs......script.php) i got the following error message "Helo command rejected: need fully-qualified hostname" Any ideea why? 当我运行一个简单的.php脚本从浏览器发送邮件时,一切正常,但是如果我使用命令提示符和php sufix(c:..... php c:\\ xampp \\ htdocs ... ... script.php)我收到以下错误消息“ Helo命令被拒绝:需要完全限定的主机名”为什么有任何想法?

I can see in ECHLO that when run from browser i got the right server address (my hostname) but when i access the script from command promt i got as a host name the machine name. 我可以在ECHLO中看到,从浏览器运行时,我得到了正确的服务器地址(我的主机名),但是当我从命令promt访问脚本时,得到了作为主机名的计算机名。 Can this be changed or set-up to run like from the browser? 可以更改它或将其设置为像在浏览器中一样运行吗? I guess this is the issue, the hostname. 我想这就是问题所在,主机名。

Thank you 谢谢

As the others have mentioned, when you're not in an HTTP environment, and if your host name is not set correctly, or is unavailable to PHP for some reason, there may be difficulty obtaining the host name that's used in HELO/EHLO commands. 正如其他人提到的那样,当您不在HTTP环境中时,如果主机名设置不正确,或者由于某种原因PHP无法使用主机名,则可能很难获得HELO / EHLO命令中使用的主机名。 You can provide one explicitly by setting the Hostname property : 您可以通过设置Hostname属性显式地提供一个:

$mail->Hostname = 'myserver.example.com';

Note that this is different to the Host and Helo properties; 请注意,这与HostHelo属性不同。 see the docs for the differences. 有关差异,请参阅文档。

Most likely the script relies on some environment variable to use as its own hostname, probably $_SERVER['HTTP_HOST'] . 该脚本很可能依赖于某些环境变量来用作其自己的主机名,可能是$_SERVER['HTTP_HOST'] This will be set when running from your webserver, but not over command line because it's not an HTTP environment. 从Web服务器运行时将设置此属性,但不能通过命令行运行,因为它不是HTTP环境。

If you enable full error reporting you may see something like this in the error output: 如果启用完整的错误报告,则错误输出中可能会显示以下内容:

PHP Notice:  Undefined index: HTTP_HOST in script.php

You could force the variable to exist from the command line, like this: 您可以从命令行强制变量存在,如下所示:

HTTP_HOST=example.com php -f script.php

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

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