[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

php SMTP email doesn't work

Posted on 2008-01-29
12
Medium Priority
?
2,283 Views
Last Modified: 2013-12-13
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>");
 }
?>
0
Comment
Question by:carolynusinger
  • 6
  • 3
9 Comments
 

Author Comment

by:carolynusinger
ID: 20774131
thanks
0
 
LVL 48

Expert Comment

by:hernst42
ID: 20775148
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
 

Author Comment

by:carolynusinger
ID: 20777889
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
LVL 48

Expert Comment

by:hernst42
ID: 20778130
0
 

Author Comment

by:carolynusinger
ID: 20778161
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
 
LVL 48

Expert Comment

by:hernst42
ID: 20778352
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
 

Author Comment

by:carolynusinger
ID: 20778494
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
 

Accepted Solution

by:
carolynusinger earned 0 total points
ID: 20778637
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
 

Author Comment

by:carolynusinger
ID: 20778638
tell me how to assign points please
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question