views:

198

answers:

4

I have a default installation of WAMP Server 2.0.

I'm trying to send email using this simple script:

<?php

if (mail('[email protected]', 'My Title', 'Some Text')) {
    echo "OK";
} else {
    echo "Why ??";
}

?>

Unfortunately, I get the following warning:

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:\My_Path\send_email.php on line 3 Why ??

What could be the reason for that ?

I expected sending email to be a very simple task ... :(

+3  A: 

To be able to send email you need an outgoing email server. In most Linux systems there exists one by default, and PHP will use sendmail, a Linux app for submitting mail to whichever mail transfer agent you have installed.

In Windows, to be able to send mail from PHP you need an outgoing mail server somewhere and you need to tell PHP the address and port of it. This is done in php.ini using the SMTP and smtp_port settings. It will default to 'localhost' on port 25. Unless you have set up a mail server on that machine, that will fail.

If your ISP gives you an outgoing mail server, for example, you could use its address and port number. Or, if you're serious about sending mail, you'd set up your own mail server on the local machine or someone in your local network.

thomasrutter
Thank you, I'm really looking for a simple and easy to use solution for Windows, because I believe when I put the application in a real Unix server I won't have this problem. So what would be the simplest solution for Windows ?
Misha Moroshko
Simplest would be just to point it at your ISP's SMTP server I think (though it may not be good to do this if you are in a test environment and don't actually want the email to send). Or you can look at these solutions: http://stackoverflow.com/questions/30076/need-a-lightweight-free-windows-smtp-server - looks like Windows Server also comes with its own SMTP server that you can install.
thomasrutter
A: 

Check this :http://crazzycoder.blogspot.com/2010/03/sending-mail-from-php-using-wamp.html this can solve your problem

Ankur Mukherjee
You can if there is a mail server on localhost.
thomasrutter
When changing your answer completely it may be a good idea to delete it and write a separate answer. It will also help to get rid of that negative score too.
thomasrutter
A: 

Short answer: no SMTP server is configured for the local computer (localhost). Windows does not ship with a built-in SMTP server ready to go out of the box. You can relay mail through a different host (using the SMTP php.ini directive) - but it's rare where you'll find an open relay for you test environment mail messages.

Instead of using mail(), you can use a script like PHPMailer which can connect directly to your outgoing email server with proper authentication. Here's a quick snippet for Gmail (though it's not complete) and a full example.

pygorex1
A: 

You can use "Fake Sendmail": http://glob.com.au/sendmail/

So you don't need a smtp server on your test machine, you only have to set the path to the program in your php.ini

Ciao! Stefan

habakuk