Using PEAR to send Email via SMTP
The php mail() function is great. Super easy to use and no setup required. But it would seem some spam filters are often suspicious of email sent this way and rightly or wrongly I just don’t feel it is reliable enough.
I’m currently redesigning the Goonanism home page – it’s starting to look a bit stale (launch TBA) and I thought it was important that the contact page on the new site was as reliable as possible.
Knowing that my web hosts supported PEAR I decided to use PEAR’s Mail function instead. So I wrote the following script:
< ?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
require_once("Mail.php");
$from = 'Website Enquiry
$to = "Hammy Goonan
$subject = "Website enquiry";
$body = $message;
$host = "hostdetails";
$username = "username";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => '25'
)
);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
}
else {
echo("Message successfully sent!");
}
?>
This gave me an error saying:
Class 'Net_SMTP' not found in /usr/local/php52/pear/Mail/smtp.php on line 210
So, using siteground as my host, I followed this tutorial.
Having uploaded the Net_STMP files and setting up my php.ini files I then got this error:
Fatal error: require_once() [function.require]: Failed opening required 'PEAR.php' (include_path='.:/usr/lib/php:/usr/local/lib/php:/home/goonanis/pear') in /home/goonanis/pear/Net_SMTP/SMTP.php
So it was clear to me that because of the new php.ini file, I would have to upload all the dependant packages to get this working.
Having uploaded various packages I was still getting an error saying:
Call to undefined method PEAR_Error::send()
Reading through various forums I realised that Mail::factory() will return a PEAR_Error object upon failure. So $smtp became a PEAR_Error so when I tried to call PEAR_Error::send() it wasn’t there, hence the error.
When I print_r() $smtp the PEAR_Error Object was actually throwing an error saying:
Unable to find class for driver smtp
A much more useful error message.
I only mention all of this because I had a lot of trouble sorting through forums to find this information out.
Anyway, the long and the short of it was that I needed to have the following packages and file structure for the PEAR Mail function to work:
pear
/Mail
/RFC822.php
/mail.php
/mock.php
/null.php
/sendmail.php
/smtp.php
/smtpmx.php
/Net
/SMTP.php
/Socket.php
/Mail.php
/PEAR.php
/PEAR5.php
Which you can find in the following PEAR packages:
- Pear
- Net_Socket
- Net_STMP
I hope that helps!
REPLY))
I was searching through zillions of forums, until finally found this one…
it is brilliant!
I followed the instructions, and could make my php script to work well!
Thank you very much!
REPLY))
That helps tremendously!!! Just like the person above I googled and googled… until I found your post and it solved my problems.
Thank you!!!
REPLY))
Thank you so much! I have searched forever for this answer. I didn’t realize there was more to PEAR mail than just the Mail folder.
REPLY))
You’re awesome man !!! Thanks so much… I had searched for hours for what you gave in this post…….
REPLY))
Wow, the post that keeps on giving.
Even though Pear’s mail package was installed, and running pear install mail.. confirmed this, the Mail.php was not there. The Net_SMTP etc that you clearly list was found once again after HOURS AND HOURS of searching for the solution.
I can go to bed now and get what’s left of a good nights sleep thanks to you. I appreciate it.
Jay
Owner
CompuMatter
REPLY))
Fantastic!
Like others above it took me a lot of searching to find this solution. I posted in a few forums looking for answers. I’ll go back and post links to the solution. Hopefully that will make it easier for others to find this brilliant post.
Thanks so much!
Blake
REPLY))
Too good friend!
Thank u very much
REPLY))
Very useful guide, shifting my install of PEAR around to match resolved a mail issue for me. Thanks for posting!
REPLY))
Small typo at the end
package: Net_STMP should be NET_SMTP
*grin*
REPLY))
I also found by default my path was incorrect – and setting this got things working.
set_include_path(“/path/to/pear”);
REPLY))
i install all it in windows hosting webroot
PEAR
MAIL
NET_SOCKET
NET_SMTP
package
apply the mail authentication on my enquiry page
but its still not working
plz anybody hwlp me
REPLY))
below is my code on enquiry.php
if($_POST['submit'])
{
set_include_path(“/pear”);
require_once (“Mail.php”);
// $from is your Arvixe account
$from = “jack gill “;
// $to is the receivers email
$to = “deepakgill1988@yahoo.com”;
$subject = “Self Explaining…”;
// $host where XXXX is the name of the server where your account is located
$host = “smtp.gmail.com”;
$port = “465″;
// $username and $password are the uid & pwd of your account
$username = “itsmejack.jack@gmail.com”;
$password = “password”;
$headers = array (‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘port’ => $port,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));
// $tok is my prebuilt message body (Ex $tok = “First Name: .$fName.”\nMiddle Initial: “.$mName;
$mail = $smtp->send($to, $headers, $tok);
if (PEAR::isError($mail)) {
echo(“” . $mail->getMessage() . “”);
} else {
echo(“Message successfully sent!”);
}
}
REPLY))
Very helpful, thank you! I didn’t realize that my PEAR installation didn’t come with the mail and Net_SMTP packages installed.
REPLY))
Thanks. It is very helpful for me.
I used pear to send email but it was not working because i have not installed all dependency for pear. after check your post I installed all packages then I faced path issue and i resolved all.
Thanks