开发者

Setting up smtp on windows-7 iis-7.5

I have configured a php/mysql app on my local laptop using iis7 for testing. I use php mail() to send emails using localhost smtp service on the server and want to replicate locally for testing. (it has been working fine for a long time on the server so I just want to replicate locally for testing purposes.)

Using the technet article: http://technet.microsoft.com/en-us/library/cc772058(WS.10).aspx I was able to configure my SMTP settings however, I still can't send email.

I have recycled the server a number of times with no effect.

I've ran a netstat -an and there is nothing listening on port25 - is there something else I need to do to get the smtp service listening on port25?

The error I'm receiving:

PHP Warning: 开发者_如何学JAVAmail() [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()

php.ini:

SMTP = localhost
smtp_port = 25


You can use something like smtp4dev (http://smtp4dev.codeplex.com/) instead of iis for test purposes. Works like a charm for me.


Windows 7 does not ship SMTP service. So you have to use a third party product. This has been a well known issue, but not sure why you did not find it by searching on the Internet.


Well I agree with the OP. It's not immediately obvious that W7 (even Ultimate) ships without an SMTP server (I'm pretty sure that we had it on Vista 64 Ultimate and possibly even XP), so you will have to identify a server to use, whether local, or remote.

If the server is not using authorisation, then this should work without having to mess around with IIS7 or IIS7 Express:

$smtpserver = 'host.domain.tld';
$port = 25;
$from = 'mailbox@domain.tld';
$replyto = $from;
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $replyto . "\r\n" . 
    'X-Mailer: PHP/' . phpversion();
$to = 'mailbox@domain.tld';
$subject = 'Test Message';
ini_set('SMTP', $smtpserver);
ini_set('smtp_port', $port);
$message = wordwrap("Hello World!", 70);
$success = mail($to, $subject, $message, $headers);

If the server is using clear-text authorisation (not TLS/SSL), then adding the credentials may work, depending on your version of PHP:

ini_set('username', 'yourusername');
ini_set('password', 'yourpwd');

If the server enforces the use of TLS/SSL to connect with credentials, like GMail does, then the Sourceforge xpm4 package is a straightforward solution. There are two ways you might use it with GMail (these are straight out of the examples provided with the package):

// manage errors
error_reporting(E_ALL); // php errors
define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors
// path to 'MAIL.php' file from XPM4 package
require_once '../MAIL.php';
// initialize MAIL class
$m = new MAIL;
// set from address
$m->From('username@gmail.com');
// add to address
$m->AddTo('client@destination.net');
// set subject
$m->Subject('Hello World!');
// set HTML message
$m->Html('<b>HTML</b> <u>message</u>.');
// connect to MTA server 'smtp.gmail.com' port '465' via SSL ('tls' encryption)
// with authentication: 'username@gmail.com'/'password'
// set the connection timeout to 10 seconds, the name of your host 'localhost'
// and the authentication method to 'plain'
// make sure you have OpenSSL module (extension) enable on your php configuration
$c = $m->Connect('smtp.gmail.com', 465, 'username@gmail.com', 'password', 'tls', 10,
            'localhost', null, 'plain')
        or die(print_r($m->Result));
// send mail relay using the '$c' resource connection
echo $m->Send($c) ? 'Mail sent !' : 'Error !';
// disconnect from server
$m->Disconnect();

The IIS7 Express (which is what I was using) FastCGI PHP module installs with OpenSSL Extension support enabled. The above allows you to use HTML tags in your message content. The second way of using the xpm4 package is shown below, for text-only messages (again, example is from the package source):

// manage errors
error_reporting(E_ALL); // php errors
define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors
// path to 'SMTP.php' file from XPM4 package
require_once '../SMTP.php';
$f = 'username@gmail.com'; // from (Gmail mail address)
$t = 'client@destination.net'; // to mail address
$p = 'password'; // Gmail password
// standard mail message RFC2822
$m = 'From: '.$f."\r\n".
     'To: '.$t."\r\n".
     'Subject: test'."\r\n".
     'Content-Type: text/plain'."\r\n\r\n".
     'Text message.';
// connect to MTA server (relay) 'smtp.gmail.com' via SSL (TLS encryption) with 
// authentication using port '465' and timeout '10' secounds
// make sure you have OpenSSL module (extension) enable on your php configuration
$c = SMTP::connect('smtp.gmail.com', 465, $f, $p, 'tls', 10) or die(print_r($_RESULT));
// send mail relay
$s = SMTP::send($c, array($t), $m, $f);
// print result
if ($s) echo 'Sent !';
else print_r($_RESULT);
// disconnect
SMTP::disconnect($c);

Both the above work with GMail, as of the date of this post, using IIS7 and without having to do any extra configuration.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜