php SMTP email doesn't work

I am trying to create an email using php through an SMTP server - but the code fails at the line:
$mail = $smtp->send($to, $headers, $body);

For the life of me, I don't know what's wrong.  The error is on send() - here is the error message: Fatal error: Call to undefined method PEAR_Error::send() in /home/caledorg/public_html/testingemail.php on line 27.  Here is the full code

<?php
require_once "Mail.php";

$from = "Sandra Sender <fakeemail@domain.zzz>";
$to = "Ramona Recipient <fakeemail@domain2.zzz>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "mail.domain.zzz";
$username = "fakeemail@domain.zzz";
$password = "pass";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

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

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>
carolynusingerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

carolynusingerAuthor Commented:
thanks
0
hernst42Commented:
I suggest using a mailerclass for that like http://phpmailer.sf.net or http://swiftmailer.sf.net

The problem come with the pear-Mailing class. When youcreate the mail with $smtp = Mail::factory(..) you don't get a PEAR-Mailer-Object you get an error and thus in PEAR_Error no send mthod is defined. Try:

$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
 
if (!PEAR::isError($smtp)) {
  $mail = $smtp->send($to, $headers, $body);
  if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
  } else {
    echo("<p>Message successfully sent!</p>");
  }
} else {
    echo("<p>" . $smtp->getMessage() . "</p>");
}

Open in new window

0
carolynusingerAuthor Commented:
OK...now I have a new error:   Unable to find class for driver smtp

I'm at a loss.  How do you set up a mailer class?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

hernst42Commented:
0
carolynusingerAuthor Commented:
I went to http://phpmailer.sf.net and downloaded their phpmailer.  Their instructions for installing it were:


Installation:

Copy class.phpmailer.php into your php.ini include_path. If you are
using the SMTP mailer then place class.smtp.php in your path as well.

I am used to Cold Fusion where the host internet provider has to install things - so I don't know what it means when it says copy class.phpmailer.php into your php.ini include_path.  Can you help me please? Do I do that, or do I ask the itnernet provider?
0
hernst42Commented:
alternate installing from http://phpmailer.codeworxtech.com/install.html, if you don't have access to the include path:

Copy these files class.phpmailer.php to your web server:
class.phpmailer.php
languages/phpmailer.lang-en.php

If you are using the SMTP mailer then copy class.smtp.php as well.
If you do not have control of the include directory and you are running PHP 4.0 or higher you can use this function to set it to the path of your files:

ini_set("include_path", ".:/path/to/phpmailer/dir");
Or this:
$mail->PluginDir = "/path/to/phpmailer/dir";


Or use:
set_include_path(get_include_path . PATH_SEPARATOR . "/path/to/phpmailer/dir");
0
carolynusingerAuthor Commented:
I copied the files and then did the sample code which I have below.  Now it comes up with this error:


Warning: fsockopen() [function.fsockopen]: unable to connect to mail.caled.org:25 (Connection refused) in /home/caledorg/public_html/class.smtp.php on line 122
Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.


I went into the host provider and see that outgoing email is on port 26 so I am thinking I need to specify the port.  Is that correct, and what code do I use?

Thanks
<?php
require("class.phpmailer.php");
 
$mail = new PHPMailer();
 
$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = "mail.oururl.com";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "kristyemailaddress@oururl.com";  // SMTP username
$mail->Password = "kristypassword"; // SMTP password
 
$mail->From = "kristyemailaddress";
$mail->FromName = "Kristy";
$mail->AddAddress("myemailaddress", "carolyn");
 
 
$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML
 
$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
 
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
?>

Open in new window

0
carolynusingerAuthor Commented:
FIXED!!!!!  Attached is the code I used.  

I put 26 after our domain and it works!

Thanks
<?php
require("class.phpmailer.php");
 
$mail = new PHPMailer();
 
$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = "mail.ourdomain.com:26";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "kristyemail@ourdomain.com";  // SMTP username
$mail->Password = "kristysmtp_password"; // SMTP password
$mail->From = "kristyemail@ourdomain.com";
$mail->FromName = "Kristy";
$mail->AddAddress("myemailaddress", "Carolyn");
 
 
$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML
 
$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
 
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
carolynusingerAuthor Commented:
tell me how to assign points please
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.