使用 PEAR Mail 包发送邮件时出错

Error while sending mail using PEAR Mail package

我正在尝试通过 PHP Pear Mail 包裹发送邮件,我这里有两种情况,在第一种情况下,邮件发送正确,没有任何错误,但在第二种情况下,我收到错误消息。

场景 1:

文件名:testmail.php

<?php
 require_once "Mail.php";

 $from = "erp@askspidy.com";
 $to = "support@askspidy.com";
 $subject = "Landing Page Enquiry From -";
 $body = "Hello just testing";

 $host = "ssl://mail.askspidy.com";
 $port = "465";
 $username = "support@askspidy.com";
 $password = "askspidy@1234";

 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject,
   'MIME-Version' => 1,
    'Content-type' => 'text/html;charset=iso-8859-1'
   );

 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'port' => $port,
     'auth' => true,
     'username' => $username,
     'password' => $password));

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

 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?>

如果我直接 运行 在 url 中发送邮件,那么邮件将毫无错误地发送。

场景二: 文件名:sendmail.php

<?php


    function send_mail($subject,$body)
    {
         require_once "Mail.php";

         $from = "erp@askspidy.com";
         $to = "support@askspidy.com";

         $host = "ssl://mail.askspidy.com";
         $port = "465";
         $username = "support@askspidy.com";
         $password = "askspidy@1234";

         $headers = array ('From' => $from,
           'To' => $to,
           'Subject' => $subject,
           'MIME-Version' => 1,
            'Content-type' => 'text/html;charset=iso-8859-1'
           );

         $smtp = Mail::factory('smtp',
           array ('host' => $host,
             'port' => $port,
             'auth' => true,
             'username' => $username,
             'password' => $password));


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

    }

?>

现在我从不同的文件调用这个 send_mail 函数,像这样

文件名:service/process.php

<?php

require_once("../sendmail.php");

$subject="Landing page enquiry";
$email_body="Hello Just Testing! ";

send_mail($subject,$email_body);

?>

当此文件在浏览器中执行时,我在行

收到错误消息
send_mail($subject,$email_body);

错误:

Warning: include_once(Net/SMTP.php): failed to open stream: No such file or directory in /usr/local/lib/php/Mail/smtp.php on line 348

Warning: include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /usr/local/lib/php/Mail/smtp.php on line 348

Fatal error: Class 'Net_SMTP' not found in /usr/local/lib/php/Mail/smtp.php on line 349

在场景 1 中一切正常,那么为什么在场景 2 中出现此错误,我猜路径有问题,但我不确定路径应该是什么以及我应该将路径包含在哪里。

文件夹结构:

在文件 Mail/smtp.php

中包含代码
 require_once "Net/SMTP.php";

注意:我已经在 Cpanel 帐户中手动安装了 PEAR 包,并且在 php.ini 文件

中没有进行任何设置

您可以使用__DIR__作为当前文件的目录路径

require_once __DIR__ . "/Net/SMTP.php";

在代码中的所有路径名中添加了 dirname(FILE) 并且有效

require_once dirname(__FILE__)."/Net/SMTP.php";