我用网页运行时正常,能发送,用命令运行php文件时报错,主要代码经检查无误,如下
function sendmail($setEmail, $setName, $body, $subject) {
require_once("PHPMail/phpmailer.php");
$mail = new PHPMailer ( true ); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP (); // telling the class to use SMTP
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = SBT_SSL; // sets the prefix to the servier
$mail->Host = SBT_SMTP_HOST; // sets GMAIL as the SMTP server
$mail->Port = SBT_SMTP_PORT; // set the SMTP port for the GMAIL server
$mail->Username = SBT_SMTP_USER; // GMAIL username
$mail->Password = SBT_SMTP_PASS; // GMAIL password
$mail->AddReplyTo ( SBT_MAIL_FROM, SBT_MAIL_FROM_NAME );
$mail->AddAddress ( $setEmail, $setName );
$mail->SetFrom (SBT_SMTP_USER,SBT_SMTP_USER); //sender name
$mail->Subject = $subject;
$mail->MsgHTML ( $body );
if (! $mail->Send ()) {//(这边没报错)
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit ();

} catch ( phpmailerException $e ) {
echo $e->errorMessage (); //Pretty error messages from PHPMailer(就是这边输出错误的)
} catch ( Exception $e ) {
echo $e->getMessage (); //Boring error messages from anything else!
}
}
问下是什么原因,目的是为了可以用linux计划任务来执行该PHP文件

解决方案 »

  1.   

    我用的是gmail邮箱,465$mail->Host ="smtp.gmail.com"; 
    $mail->Port =465
      

  2.   

    SMTP Error: Could not connect to SMTP host,问题题目就是啊
      

  3.   

    估计就是扩展的问题,用命令行就会出现这个扩展那个扩展的问题opensll扩展开了么?呵呵,应该开了吧,不开的话网页发送不会成功
      

  4.   

    刚刚测试了一下,网页好使,命令行不好使,报错:说扩展php_pdo_mysql.dll找不到
      

  5.   

    php --ini 一下,看使用的ini是否和作为apache模块运行的php一致.
      

  6.   

    实在不行,我安装个linux环境来测试吧!
    看网上说这个问题是php.ini没配好,我看了下,配的是SMTP,照他们说的改了也没用,关键是我SMTP已经重新赋值,用Gmail邮箱的服务器的啦
      

  7.   

    老虎头,能不能说详细点,修改ini文件吗?你看这样行不?[mail function]
    ; For Win32 only.
    SMTP = localhost
    smtp_port = 25; For Win32 only.
    sendmail_from = [email protected]; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ;sendmail_path = "/usr/sbin/sendmail -t -i"; Force the addition of the specified parameters to be passed as extra parameters
    ; to the sendmail binary. These parameters will always replace the value of
    ; the 5th parameter to mail(), even in safe mode.
    ;mail.force_extra_parameters =
      

  8.   

    嗯,我没说清楚,我说的是命令php --ini,看cli下运行的php和apache模块运行的php是否公用一个ini文件
    有时候phpcli单独安装的话会自带一个ini文件.
      

  9.   

    大哥,你对linux很熟吗?那问下像我这样 require_once("PHPMail/phpmailer.php"); 载入了phpmailer能行不?我怕linux不接受phpmailer插件
      

  10.   

    老虎头,请帮我看看呢!
    C:\wamp\bin\php\php5.2.6>php --ini
    Configuration FILE (php.ini) Path: C:\WINDOWS
    Loaded Configuration FILE:         C:\wamp\bin\php\php5.2.6\php.ini
    Scan FOR additional .ini files IN: (NONE)
    Additional .ini files parsed:      (NONE)
      

  11.   

    不可能,$mail->IsSMTP ();这个表明你已经设置了smtp发送了.
    你现在的主要问题是找找看为什么socket连接不了smtp服务器.
    防火墙也有可能.
      

  12.   

    Loaded Configuration FILE:         C:\wamp\bin\php\php5.2.6\php.ini
    这个php.ini不是我目前运行的那个PHP.INI
      

  13.   

    wamp环境,应该是共用一份ini文件的.想再一次确定的话,php网页输出phpinfo(),看使用的ini文件,是否和这个一致.
    命令:telnet smtp.gmail.com 465 ,确定能连接成功?
      

  14.   

    good
    那么就使用php -c 命令引入你的正确ini路径
    php -c ini路径 php文件
    具体可以php -help查看命令参数
      

  15.   

    telnet smtp.gmail.com 465不能连接,那我应该用什么邮件服务器呢?
      

  16.   

    telnet都不行,肯定是你防火墙禁止了访问 
      

  17.   

    谢谢大家,解决了,首先是防火墙的问题,360防火墙忘关了,其次是ini问题
      

  18.   

    to  RXPY2007
    php.ini什么问题,请说清楚些,我也遇到这个问题了,linux环境
      

  19.   

    希望楼主说的清楚点,怎么解决的,我的win环境也出现了一下问题
      

  20.   

    嗯,我没说清楚,我说的是命令php --ini,看cli下运行的php和apache模块运行的php是否公用一个ini文件
    有时候phpcli单独安装的话会自带一个ini文件.
      请问php --ini怎么用