...

View Full Version : SMTP Authentication & mail() - Help Required



lemonadeX
06-04-2007, 03:48 PM
Being able to send emails through PHP is a fundamental part of the site, so I've been searching around and trying to come up with some code to authenticate with the SMTP server to send the email. What I have at the moment is code found on the Internet to perform this task using the PEAR module. I assume this module is installed as the php.ini 'include_path' setting is c:\php4\pear. Please correct me if I am wrong:


require_once "Mail.php";

$from = "MySite Mailer <admin@mysite.com>";
$to = 'mailto@mailto.com';

$host = "smtp.mysite.com";
$username = "smtp_user";
$password = "smtp_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("Success!");
}

The error I am getting is as follows:

Fatal error: sendmail(): Failed opening required 'Mail.php' (include_path='.;c:\php4\pear')

I do not have a file called Mail.php in my directory listing, but I assumed this would be included by default through PEAR? I have not used PEAR before, so am unsure.

Any help would be much appreciated.

-Will

printf
06-04-2007, 03:53 PM
There should be a file in the PHP directory called go-pear.bat, just click on it, then follow the directions.

CFMaBiSmAd
06-04-2007, 03:54 PM
The pear setting in the include path is there by default. It does not mean that the pear framework or any specific pear class is installed.

A lot of servers don't have pear installed, so to create a stand a lone script that will work independent of what is present on the server, I recommend that you use the phpmailer class to do SMTP authentication - http://phpmailer.sourceforge.net/

lemonadeX
06-04-2007, 04:37 PM
Thanks for the quick reply. I have downloaded phpmailer and attempted to implement it. Although it is not returning an error, I am not receiving the email. Here is the code I am using (as part of a function):


require('includes/class.phpmailer.php');
require('includes/class.smtp.php');

$mail = new PHPMailer();
$mail->SetLanguage('en', 'language');

$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.mysite.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username"; // SMTP username
$mail->Password = "password"; // SMTP password

$mail->From = "myemail@gmail.com";
$mail->FromName = "MySite.com Mailer";
$mail->AddAddress('mailto@address.com');
$mail->AddReplyTo("myemail@gmail.com", "MySite");

$mail->IsHTML(true); // set email format to HTML

$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = "";

if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
return 0;
} else {
return 1;
}

Does this code look correct? I've taken it from the PHPMailer documentation. If so, I'm guessing the problem lies elsewhere.

Thanks again,

-Will

firepages
06-05-2007, 01:40 AM
are you doing this from your local computer ? and if so does your ISP block port 25 ? (mine (the biggest Australian ISP) does)

lemonadeX
06-05-2007, 11:47 AM
Hi there and thanks for the reply.

No, I'm executing the script from my webserver (hosting with Acecom). Since posting this thread up, I have actually received some test emails that have been sent - It just takes ~9 hours for them to arrive :confused:

I can't understand why.

-Will



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum