mail() fails on windows server

the mail() function

Moderators: egami, macek, gesf

Post Reply
Snowbird30ds
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Dec 22, 2020 7:49 am

Tue Dec 22, 2020 8:21 am

I get an "Uncaught error" any time I try to use the mail() function, server is windows shared on hostgator, they tell me it's a known bug with php in windows servers and don't seem to care much, .asp will email fine using cdosys so I have the right settings/auth.
Is there a genuine known problem or are they fobbing me off?
Is there a workaround for a complete newbie?
TIA.
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 90
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Wed Dec 23, 2020 2:41 am

Do you see any other text with your error?
Snowbird30ds
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Dec 22, 2020 7:49 am

Wed Dec 23, 2020 7:14 am

The full error message is:

Code: Select all

Fatal error: Uncaught Error: Class 'Mail' not found in D:\InetPub\vhosts\normanboats.net\httpdocs\junk\mail.php:19 Stack trace: #0 {main} thrown in D:\InetPub\vhosts\normanboats.net\httpdocs\junk\mail.php on line 19
Line 19 is:

Code: Select all

$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
Full code is:

Code: Select all

<?php

ini_set("SMTP","asp1mail.win.hostgator.com" );
ini_set('sendmail_from', 'me@domain.net');

require_once "Mail.php";
 
$from = "me@domain.net";
$to = 'my.email@outlook.com';
$subject = "Hi!";
$body = "Hi,\n\nHow are you?\nphp test";
$host = '127.0.0.1';
$username = 'me@domain.net';
$password = 'Mypa55word';
 
$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>");
}
?>
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 90
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Wed Dec 23, 2020 2:43 pm

OK. That looks like the file Mail.php is supposed to define a class called "Mail" but it either doesn't or isn't loaded correctly. Is there a "Mail" class defined in Mail.php?
Snowbird30ds
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Dec 22, 2020 7:49 am

Thu Dec 24, 2020 3:46 am

Oops! Sorry I posted the wrong code, that was a later trial of something.
The proper code is:

Code: Select all

<?php
 require_once "Mail.php";
 
 $from = "Sandra Sender <sender@example.com>";
 $to = "Ramona Recipient <recipient@example.com>";
 $subject = "Hi!";
 $body = "Hi,

How are you?";
 
 $host = "mail.example.com";
 $username = "smtp_username";
 $password = "smtp_password";
 
 $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>");
  }
 ?>
This is taken straight from hostgators help pages and is the php windows test code they supply but then tell me there's a known bug with windows servers, I would have gone linux but it's a legacy website I took over with an old forum in .asp using an access database file so a bit tied.
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 90
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Thu Dec 24, 2020 2:54 pm

The problem line is still trying to reference the class "Mail" right? Which isn't found. My first thought is it should be in the required "mail.php". Is that file in the same directory as this script, and does it have the "Mail" class in it?
Post Reply