1

I have a default installation of WAMP Server 2.0.

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

<?php

if (mail('my_email@gmail.com', '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 ... :(

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
  • 2
    You need access to an outgoing mail server to be able to send messages. The `mail()` function connects to the server configured in your php.ini file (as outlined in the error message) and instructs it to send a message. I would suggest googling *sending email with php and gmail* for a good tutorial about the basics of `mail()` and smtp servers. – Mike B Aug 05 '10 at 05:50

3 Answers3

6

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.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • 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 Aug 05 '10 at 05:56
  • 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 Aug 05 '10 at 06:03
  • Note that if you use your ISP's outgoing email server and you're on a residential connection you will need to abide by their policies which might prohibit running mailing lists, limit the number of emails you can send, require that you send from your ISP email address in the "From" line, etc. If you want to send serious email you could consider setting up an SMTP server yourself, or paying for an external mail relay. – thomasrutter Nov 30 '15 at 23:24
1

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.

leepowers
  • 37,828
  • 23
  • 98
  • 129
0

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
  • 2,712
  • 2
  • 28
  • 47