3

Ok. I have used the apparently working code(the main top answer with 136 green ticks): Send email using the GMail SMTP server from a PHP page

in my php mail script, replacing the gmail user and pass with my own... at first I got errors that PEAR, then PEAR5 could not be found... so I copied those 2 files(I downloaded from pear site) into the script's folder...

Still, script didn't work.

I added some echoes to see where it halts, and it halts at this line(I think):

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

My apache/php error log says this:

PHP Fatal error: Call to undefined method PEAR_Error::send()`

I have googled this error and found over a dozen pages but havn't found an anwser... mostly they seem to say something about "installing pear libraries."

I have not tried installing/configuring pear on my local server.... because I do not think I can install new packages on my webhost, so I need a more portable solution: What I mean by this is a working script, and any relative class files I can just copy all into one folder to get it working..... so I can just copy this folder to any apache/php server and it will automatically work(by refrencing the script in html form), without having to install/configure some third party package on the server.

I also tried phpmailer which gave similar problems and it seems to also require pear so I don't really see the point of experimenting with phpmailer further if I cannot get pear to even work.

Community
  • 1
  • 1
droidtech
  • 65
  • 2
  • 6

3 Answers3

2

PHP Fatal error: Call to undefined method PEAR_Error::send()

This means you didn't get a mail object but an error object, probably because logging in failed or so. Add the following to your code:

$mail = Mail::factory(..);
if (PEAR::isError($mail)) {
    echo $mail->getMessage() . "\n" . $mail->getUserInfo() . "\n";
    die();
}
$mail->send(...);

Btw, using $searchengine to look for that error message would have given you the same answer.

cweiske
  • 30,033
  • 14
  • 133
  • 194
  • iv got it all working now thanks. i have another question though. should the sent mails appear in the sent box of the gmail account or whatever account you connect to with the smtp/php script? the contact form is being sent by that account succesfully to my other inbox... but i cant see anything in the sent box – droidtech Mar 16 '13 at 21:31
  • No. Sending Mail via SMTP has nothing to do with storing a mail in a folder of your mail account. Most email clients do both: Send mail and store a copy of it in your mailbox. If you want to keep it, you need to store the mail via IMAP in your sent folder. – cweiske Mar 17 '13 at 13:47
1

As @cweiske states, adding the code provided by him will give the detailed error. In my case it is

Unable to find class for driver smtp

PHP Fatal error: Call to undefined m ethod PEAR_Error::send()

So basically it attempted to include "Mail/smtp.php" and checkd for the Mail_smtp class, but was unable to find it. So either Mail/smtp.php doesn't exist, or including it did not define the Mail_smtp class.

To resolve this all you need to do is to download Pear mail from here and copy the mail folder on to your domain. that's it. your code should work without any issues.

Subhash
  • 33
  • 1
  • 4
0

Thanks that helped lead me to this error: Unable to find class for driver smtp

Which led me to this page: http://goonanism.com/blog/2010/06/08/using-pear-to-send-email-via-smtp/

which told me I needed additional files. after I added the files it worked.

Issue solved. But I think the requirement of those files should be documented better for people who want portable solutions like myself :)

droidtech
  • 65
  • 2
  • 6
  • those requirements are documented in package.xml and *automatically* resolved when installing pear packages with the pear installer. The pear download pages explicitly tell you to use the installer and only do the manual installation if you're really know what you are doing. – cweiske Mar 14 '13 at 14:55
  • yes i noticed some installer things. the reason why i didnt do that is because i can't install new things on my webhost, so i wanted a portable solution that i can use on my local server and any webhost that supports php very easily – droidtech Mar 14 '13 at 16:30