3

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

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,

Aman
  • 623
  • 1
  • 11
  • 23
  • did you check the return value of `include`? It'll return boolean FALSE if the file couldn't be loaded for whatever reason. – Marc B Jun 11 '14 at 15:52
  • Hey Marc, When I do var_dump(include 'Mail.php'); it returns int(1). Same for Mail/mime.php . – Aman Jun 11 '14 at 16:00
  • Could it be that It might be clashing with the Zend Mail class ? I am asking this because if I replace include 'Mail.php'; with include 'C:/wamp/bin/php/php5.5.12/pear/Mail.php'; and then run the script :- I get a different error :- Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\bin\p hp\php5.5.12\pear\Mail\mail.php on line 153. But then again my smtp setting are right, I tested with simple php mail() function. Any thoughts guys ? Thanks – Aman Jun 11 '14 at 16:14

2 Answers2

1

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


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

    if (PEAR::isError($mail)) {
        echo $mail->getMessage();
    } else {
        echo "Message sent successfully!";
    }
    echo "\n
}
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • Hey Sany, Thanks man. But I think the problem here is not with the code. I think the problem is in include('Mail.php') . What I think is that its clashing with Zend/Mail.php. I insist, i "think" thats what is happening. include 'C:/wamp/bin/php/php5.5.12/pear/Mail.php' worked for me – Aman Jun 11 '14 at 17:25
  • Hey Mate, i also believe your code is fine.But there is a problem with your Mail.php inclusion.You are not single with this problem, but after file inclusion, other get ride of this issue.check here http://stackoverflow.com/questions/12989183/fatal-error-class-not-found-using-pear-in-xampp – A l w a y s S u n n y Jun 11 '14 at 17:59
  • Hey Sany, I did check that post out. I believe I am already doing the right thing by using include 'Mail.php' instead of 'Mail/mail.php' ? Unless you are pointing out to some other issue ? .. Thanks man, I appreciate the help. – Aman Jun 11 '14 at 18:03
0

You need to either specify the full path to the PEAR Mail package that you've installed (instead of include 'Mail.php';), or include that path in php.ini's include_path.

Also update php.ini with your mail server's address and port... Since you are using Mail's send driver "mail", which is PHP's mail() function. Though you can specify it to use sendmail or an SMTP server instead.

rightstuff
  • 6,412
  • 31
  • 20
  • So this is partially correct. I think because I am using Zend, It it picking up Mail.php from Zend instead of pear. May be some priority issue set up by Zend. If I do include 'C:/wamp/bin/php/php5.5.12/pear/Mail.php' , It works Great. Also about the smtp setting. I forgot to update the 2nd php.ini with my smtp setting. Updated it and works great now . Thanks rightstuff – Aman Jun 11 '14 at 17:20