pear fatal Error: Class 'Mail' not found .wamp

Aman picture Aman · Jun 11, 2014 · Viewed 15.8k times · Source

I am having some trouble with using the send mail functionality of pear with wamp. I went through with the steps in this link : (http://pear.php.net/manual/en/installation.checking.php) to check if my pear was installed correctly, and it seems like I have it right.

<?php
require_once 'System.php';
var_dump(class_exists('System', false));
?>

The code above returns bool(true). So I am assuming that my paths are set right. But for the code below I am getting an error.

<?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);
?>

Error is on this line : $mail =& Mail::factory('mail'); . Fatal error: Class 'Mail' not found

Also, I installed pear Mail with this command : pear install Mail Mail_mime

I would appreciate any help.

Thanks,

Answer

Always Sunny picture Always Sunny · Jun 11, 2014

This one works for me, try this way

   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
}