3

我在使用 wamp 的梨的发送邮件功能时遇到了一些麻烦。我完成了这个链接中的步骤:(http://pear.php.net/manual/en/installation.checking.php)检查我的梨是否安装正确,看起来我做对了。

<?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'    => 'myemail@gmail.com',
                  '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('myemail2@gmail.com', $hdrs, $body);
?>

错误在这一行:$mail =& Mail::factory('mail');Fatal error: Class 'Mail' not found

另外,我用这个命令安装了 pear Mail:pear install Mail Mail_mime

我将不胜感激任何帮助。

谢谢,

4

2 回答 2

1

这个对我有用,试试这个

   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 bounce@domain.com');


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

    if (PEAR::isError($mail)) {
        echo $mail->getMessage();
    } else {
        echo "Message sent successfully!";
    }
    echo "\n
}
于 2014-06-11T16:45:16.923 回答
0

您需要指定已安装的 PEAR Mail 包的完整路径(而不是include 'Mail.php';),或者在 php.ini 中包含该路径include_path

还要用您的邮件服务器的地址和端口更新 php.ini... 因为您使用的是 Mail 的发送驱动程序“mail”,它是 PHP 的 mail() 函数。尽管您可以指定它使用 sendmail 或 SMTP 服务器。

于 2014-06-11T16:59:27.653 回答