在Windows 7 IIS 7.5上设置SMTP

5
我已经在本地笔记本电脑上使用iis7配置了一个php/mysql应用程序进行测试。我使用php mail()在服务器上使用localhost smtp服务发送电子邮件,并且想要在本地进行复制以进行测试。(它在服务器上已经正常工作了很长时间,因此我只想在本地进行测试。)
使用这篇文章:http://technet.microsoft.com/en-us/library/cc772058(WS.10).aspx,我能够配置我的SMTP设置,但是我仍然无法发送电子邮件。
我已经多次重启服务器,但没有效果。
我运行了netstat -an命令,没有任何东西在监听端口25-我需要做其他什么才能使smtp服务在端口25上监听吗?
我收到的错误信息:
PHP 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() php.ini:
SMTP = localhost
smtp_port = 25

除非您要发送电子邮件到地址“@localhost”,否则无需运行本地SMTP服务器即可使用mail()发送。当您尝试发送电子邮件时,会出现什么错误? - DaveRandom
我并不想运行一个本地的SMTP服务器 - 我想在本地监听,并将邮件传递到远程邮件服务器。(我在IIS SMTP功能中进行了配置) - php-b-grader
根据该错误,mail() 函数是正常工作的,但 SMTP 服务器正在拒绝它。我会首先使用 Wireshark 查看正在交换的原始 SMTP 数据包。然而,需要注意的是:在 Windows 上,您无法在 Wireshark 中监听 127.0.0.1 的流量。因此,在调试时,您必须通过远程机器进行跳转(除非您通过有线网卡或无线网卡进行切换)。 - DaveRandom
1
一般来说,这个错误意味着电子邮件地址格式不正确 - 你的mail()命令是什么样子的?另外,我建议使用Swiftmailer或类似的工具代替原生的mail()函数。 - ldg
3个回答

16
您可以使用类似于smtp4dev(http://smtp4dev.codeplex.com/)这样的工具来进行测试,而不是使用IIS。对我而言,它非常好用。

它确实可以工作,但显然无法在Windows 7中使用。这个操作系统没有内置SMTP服务器。 - Dima

2

Windows 7没有SMTP服务。因此,您必须使用第三方产品。这是一个众所周知的问题,但不确定为什么您在互联网上搜索时没有找到它。


请指向一个文档,说明在没有SMTP服务器的情况下,IIS 7.5 SMTP功能将无法在Windows 7上运行。 - php-b-grader
你不应该配置你的应用程序并让它尝试在本地查找SMTP服务器(就像你上面提到的那样)。如果你想让它工作,你需要在本地运行一个SMTP服务器。这是常识,我认为不需要在某个地方记录下来。 - Lex Li
什么?常识不是应该知道IIS中的SMTP功能并不提供SMTP功能吗?怎么可能这是常识呢? - php-b-grader
1
请再次阅读微软的文章,链接为http://technet.microsoft.com/en-us/library/cc772058%28WS.10%29.aspx。请注意,您会看到“在SMTP服务器文本框中输入唯一名称或选择使用localhost框来将名称设置为LocalHost”。常识告诉我们,如果本地没有SMTP服务(如Windows 7情况),则您应该执行“或”之前的操作,例如使用GMail SMTP服务器或其他。如果您打算执行“或”之后的操作,就像@Dima所指出的那样,您需要在本地安装第三方SMTP服务器。 - Lex Li

1

我同意楼主的观点。W7(即使是Ultimate版本)没有SMTP服务器并不是显而易见的事情(我相信我们在Vista 64 Ultimate甚至XP上都有它),因此您需要确定要使用的服务器,无论是本地还是远程。

如果服务器没有使用授权,则无需使用IIS7或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);

如果服务器使用明文授权(而非TLS/SSL),则根据您的PHP版本,添加凭据可能会起作用:
ini_set('username', 'yourusername');
ini_set('password', 'yourpwd');

如果服务器强制使用TLS/SSL连接凭据,例如GMail,则Sourceforge xpm4包是一个简单的解决方案。您可以通过两种方式之一与GMail一起使用它(这些直接来自包中提供的示例):
// 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();

我使用的是IIS7 Express,它自带FastCGI PHP模块并启用了OpenSSL扩展支持。这样就可以在消息内容中使用HTML标签。下面展示了使用xpm4包的第二种方式,用于纯文本消息(同样,示例来自包源代码):

// 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);

截至本篇文章发布日期,两者都可以在使用IIS7的情况下与GMail配合使用,无需进行任何额外的配置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接