2011-08-27 182 views
5

我在本地笔记本电脑上使用iis7配置了php/mysql应用程序进行测试。我使用PHP邮件()在服务器上使用本地主机smtp服务发送电子邮件,并希望在本地进行复制以进行测试。 (它一直在服务器上很好地工作,所以我只是为了测试目的而在本地复制)。在windows-7上设置smtp iis-7.5

使用technet文章:http://technet.microsoft.com/en-us/library/cc772058(WS.10).aspx但我仍然无法配置SMTP设置发电子邮件。

我已经回收服务器很多次,没有任何效果。

我已经运行了netstat -an,并且没有任何监听端口25 - 是否还有其他事情我需要做以获得侦听端口25上的smtp服务?

我收到的错误:

PHP的警告:电子邮件()[function.mail]: 无法连接在本地主机"端口" 25,到邮件服务器 验证" SMTP "和" SMTP_PORT "设置在 php.ini中或使用的ini_set()

的php.ini:

SMTP = localhost 
smtp_port = 25 
+0

除非您尝试将电子邮件发送到地址'@ localhost',否则不需要运行本地SMTP服务器以使用'mail()'发送。当你尝试发送电子邮件时,你会得到什么错误? – DaveRandom

+0

我不想运行本地smtp服务器 - 我想在本地主机上侦听并将邮件传递到远程邮件服务器。 (我在IIS SMTP功能中配置) –

+0

基于该错误,'mail()'正在工作,但SMTP服务器拒绝它。我要做的第一件事是获取[Wireshark](http://www.wireshark.org/),并查看正在交换的原始SMTP。但是,警告:您无法在Windows上的Wireshark上监听127.0.0.1上的流量。在调试时,您必须通过远程计算机进行反弹(除非您从无线NIC进入有线NIC,反之亦然)... – DaveRandom

回答

16

您可以使用类似smtp4dev(http://smtp4dev.codeplex.com/)而不是iis来进行测试。对我来说就像一个魅力。

+0

这是否意味着IIS中的SMTP功能不起作用? –

+0

它确实有效,但显然不在Windows 7中。操作系统没有内置的SMTP服务器 – Dima

+0

在Win7中工作得很好 – jvenema

2

Windows 7不提供SMTP服务。所以你必须使用第三方产品。这是一个众所周知的问题,但不知道为什么你没有通过在互联网上搜索找到它。

+0

指向一个文件,指出iis 7.5 smtp功能不能在windows上运行7没有smtp服务器? –

+0

你不应该配置你的应用程序,并让它尝试在本地查找SMTP服务器(就像你上面提到的那样) 。如果你想让它工作,你需要在本地运行一个SMTP服务器。这是常识,我认为它不需要记录在某个地方。 –

+0

什么?知道IIS中的SMTP功能不提供SMTP功能的常识?如何知道这可能是常识? –

1

那么我同意OP。 W7(即使是旗舰版)在没有SMTP服务器的情况下运行(我确信我们已经在Vista 64 Ultimate,甚至XP上运行了它)并不是很明显,所以您将不得不识别要使用的服务器,无论是本地的还是远程。

如果服务器不使用授权,那么这应该不必乱用IIS7或IIS7快递周围的工作:如果服务器使用明文授权(不TLS/SSL)

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

,然后加入凭证可以正常工作,这取决于你的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('[email protected]'); 
// add to address 
$m->AddTo('[email protected]'); 
// 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: '[email protected]'/'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, '[email protected]', '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的快递(这是我使用的是什么)的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 = '[email protected]'; // from (Gmail mail address) 
$t = '[email protected]'; // 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); 

两者与GMail的上述工作,因为这后之日,使用IIS7而不必做任何额外的配置。