我正在尝试通过PHP Pear Mail包发送邮件,我在这里有2个方案,在第一个方案中邮件正常发送没有任何错误但在第二个方案中我收到错误消息。
情景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中运行文件,则发送邮件时没有任何错误。
场景2: 文件名: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文件中进行任何设置
答案 0 :(得分:0)
您可以将__DIR__
用于当前文件的目录路径
require_once __DIR__ . "/Net/SMTP.php";
答案 1 :(得分:0)
在代码中的任何位置添加了dirname( FILE )到路径名,并且它正常工作
require_once dirname(__FILE__)."/Net/SMTP.php";