Why mail() PHP function does not work with WAMP default installation?

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 … 🙁

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

Windows doesn’t include an MTA by default. In Windows, to be able to send mail from PHP you need to have access to some outgoing email server and 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 yourself, this 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 somewhere in your local network.

Solution 2

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.

Solution 3

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply