PHP警告:include_once(Net / SMTP.php):无法打开流

时间:2017-07-24 11:41:25

标签: php pear mail-mime

我想使用Gmail发送电子邮件。为此,我只需运行以下命令

pear install Mail Mail_Mime

我的php文件(filename.php)代码如下所示

<?php
  require_once "Mail.php";
  $from = 'from@gmail.com';
  $to = 'to@gmail.com';
  $subject = 'Hi!';
  $body = "Hi,\n\nHow are you?";
  $headers = array(
      'From' => $from,
      'To' => $to,
      'Subject' => $subject
  );
  $smtp = Mail::factory('smtp', array(
          'host' => 'ssl://smtp.gmail.com',
          'port' => '465',
          'auth' => true,
          'username' => 'from@gmail.com',
          'password' => 'password'
      ));
  $mail = $smtp->send($to, $headers, $body);
  if (PEAR::isError($mail)) {
      echo('<p>' . $mail->getMessage() . '</p>');
  } else {
      echo('<p>Message successfully sent!</p>');
  }
?>

当我运行这个&#34; filename.php&#34;使用以下命令

php filename.php

我收到以下错误

PHP Warning:  include_once(Net/SMTP.php): failed to open stream: No such file or directory in /usr/share/pear/Mail/smtp.php on line 365
PHP Warning:  include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /usr/share/pear/Mail/smtp.php on line 365
PHP Fatal error:  Class 'Net_SMTP' not found in /usr/share/pear/Mail/smtp.php on line 366

我正在使用CentOS 7.

3 个答案:

答案 0 :(得分:8)

现在它正在为我工​​作。我只是使用以下步骤来解决这个问题:

1. pear upgrade --force --alldeps http://pear.php.net/get/PEAR-1.10.1
2. pear clear-cache
3. pear update-channels
4. pear upgrade
5. pear upgrade-all
6. pear install Auth_SASL
7. pear install pear/Net_SMTP

之后一切正常。

谢谢大家。

答案 1 :(得分:0)

找不到/usr/share/pear/Mail/smtp.php文件,请确保存在smtp.php文件。

您可以先检查文件是否存在。

    if(file_exists("./usr/share/pear/Mail/smtp.php") == 1){
    require_once "Mail.php";
      $from = 'from@gmail.com';
      $to = 'to@gmail.com';
      $subject = 'Hi!';
      $body = "Hi,\n\nHow are you?";
      $headers = array(
          'From' => $from,
          'To' => $to,
          'Subject' => $subject
      );
      $smtp = Mail::factory('smtp', array(
              'host' => 'ssl://smtp.gmail.com',
              'port' => '465',
              'auth' => true,
              'username' => 'from@gmail.com',
              'password' => 'password'
          ));
      $mail = $smtp->send($to, $headers, $body);
      if (PEAR::isError($mail)) {
          echo('<p>' . $mail->getMessage() . '</p>');
      } else {
          echo('<p>Message successfully sent!</p>');
      }
    }
else{
    echo "./usr/share/pear/Mail/smtp.php file not found";
    }

答案 2 :(得分:0)

对于Windows计算机-

Net_SMTP是SMTP协议的一种实现。您需要安装该扩展程序。通过在CMD中运行以下命令。

pear install Net_SMTP

请确保您已在环境路径中安装并设置了梨。