开发者

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('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 cou开发者_开发百科ld be the reason for that ?

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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜