im testing a sendmail function in PHP.
The first, i used mail() function, this is my code:

$subject="This is test mail";
$message="Hello, this is test mail from Viet May Cor. \n Best regards!";
$headers="From: $from";

and i got an error: "Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\AppServ\www\mail.php on line 7"

The second, i was try using PHPmailer, i got an error too,

$mail             = new PHPMailer();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl"; 
$mail->Host       = "";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "";  // GMAIL username
$mail->Password   = "gmailpass";            // GMAIL password

$mail->From       = "";
$mail->Subject    = "PHPMailer Test Subject via gmail";
$mail->Body       = "Hi, this is a test";           
$mail->AddAddress("", "Hussain");


This is result: SMTP Error: Could not connect to SMTP host.

Final, i tested the most basic mail function in phpmailer;

$mail             = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSendmail(); // telling the class to use SendMail transport

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("","First Last");

$mail->SetFrom('', 'First Last');

$mail->AddReplyTo("","First Last");

$address = "";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via Sendmail, basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test


$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

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

and there all was i got: "Could not instantiate mail function. Mailer Error: Could not instantiate mail function. "

I'm using Appache 2.2, and i haven't config anything yet. I thought that sending mail in PHP was too easy before, but i was wrong. There is need to install, config anything ? Please anyone tell my a solution ? Thanks to all

Recommended Answers

All 7 Replies

Member Avatar

Please test your files on webserver it will work. Do not from your localhost

Is this running on a linux server? Do you have sendmail running?

Thanks for all, My code works ok now, because there wasn't smtp server installed before.

But for the code send mail via smtp gmail, i changed port and prefix as

Perhaps this:

But still getting error here:

Warning: stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in C:\AppServ\www\test\mail\class.smtp.php on line 197

This is content at line 197:

if(!stream_socket_enable_crypto($this->smtp_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
return false;

Anybody tell me a solution ?

Heyah, that was successful, cause in Apache version was used(2.5.8), after i run 2.5.10 version, that all successful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.