我在使用 pear 和 wamp 的发送邮件功能时遇到一些问题。我完成了此链接中的步骤:(http://pear.php.net/manual/en/installation.checking.php)检查我的 pear 是否安装正确,看起来我安装正确.
<?php
require_once 'System.php';
var_dump(class_exists('System', false));
?>
上面的代码返回
bool(true)
。所以我假设我的路径设置正确。
但对于下面的代码我收到错误。
<?php
include 'Mail.php';
include 'Mail/mime.php' ;
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = 'test.xls';
$crlf = "\n";
$hdrs = array(
'From' => '[email protected]',
'Subject' => 'Test mime message'
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send('[email protected]', $hdrs, $body);
?>
此行有错误:
$mail =& Mail::factory('mail');
。 Fatal error: Class 'Mail' not found
另外,我使用以下命令安装了 pear Mail:
pear install Mail Mail_mime
我将不胜感激任何帮助。
谢谢,
这个对我有用,试试这个方法
function sendEmail($subject,$from,$to,$bodymsg,$cc=null)
{
require_once "Mail.php";
require_once "Mail/mime.php";
$crlf = "\n";
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
//$host = "smtp.gmail.com";
$host = "ssl://smtp.gmail.com"; // try this one to use ssl
$port = 465;
$username = "myusername"; //<> give errors
$password = "mypass";
//$mime = new Mail_mime($crlf);
$mime = new Mail_mime(array('eol' => $crlf)); //based on pear doc
$mime->setHTMLBody($bodymsg);
//$body = $mime->get();
$body = $mime->getMessageBody(); //based on pear doc above
$headers = $mime->headers($headers);
$smtp = Mail::factory("smtp",array("host" => $host,
"port" => $port,
"auth" => true,
"username" => $username,
"password" => $password), '-f [email protected]');
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n
}
您需要指定已安装的 PEAR Mail 包的完整路径(而不是
include 'Mail.php';
),或者将该路径包含在 php.ini 的 include_path
中。
还要用您的邮件服务器的地址和端口更新 php.ini...因为您使用的是 Mail 的发送驱动程序“mail”,这是 PHP 的 mail() 函数。尽管您可以指定它使用 sendmail 或 SMTP 服务器。