【问题标题】:Setting up smtp on windows-7 iis-7.5在 windows-7 iis-7.5 上设置 smtp
【发布时间】:2011-08-27 08:10:19
【问题描述】:

我在本地笔记本电脑上配置了一个 php/mysql 应用程序,使用 iis7 进行测试。我使用 php mail() 在服务器上使用 localhost smtp 服务发送电子邮件,并希望在本地复制以进行测试。 (它已经在服务器上运行了很长时间,所以我只想在本地复制以进行测试。)

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

我已经多次回收服务器但没有效果。

我运行了 netstat -an,但在 port25 上没有任何监听 - 我还需要做些什么来让 smtp 服务在 port25 上监听吗?

我收到的错误:

PHP 警告:mail() [function.mail]: 无法在“localhost”端口 25 连接到邮件服务器, 验证您的“SMTP”和“smtp_port”设置 php.ini 或使用 ini_set()

php.ini:

SMTP = localhost
smtp_port = 25

【问题讨论】:

  • 您无需运行本地 SMTP 服务器即可使用 mail() 进行发送,除非您尝试将电子邮件发送到地址 @localhost。当您尝试发送电子邮件时遇到什么错误?
  • 我不想运行本地 smtp 服务器 - 我想在 localhost 上收听并将邮件传递到远程邮件服务器。 (我在 IIS SMTP 功能中配置)
  • 基于该错误,mail() 正在工作,但 SMTP 服务器拒绝它。我要做的第一件事是获取 Wireshark 并查看正在交换的原始 SMTP。但是,需要注意的是:您不能在 Windows 上的 Wireshark 中收听 127.0.0.1 上的流量。在调试时,您将不得不通过远程机器反弹(除非您离开无线网卡并进入有线网卡,反之亦然......
  • 该错误通常意味着电子邮件地址格式错误 - 您的 mail() 命令是什么样的?另外,我会研究 Swiftmailer 或类似的东西,而不是原生的 mail() 函数。

标签: php email windows-7 smtp iis-7.5


【解决方案1】:

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

【讨论】:

  • 确实可以,但在windows 7中显然不行。这个操作系统没有内置的SMTP服务器。
【解决方案2】:

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

【讨论】:

  • 指向一个文档,该文档指出 iis 7.5 smtp 功能将无法在没有 smtp 服务器的 Windows 7 上运行?
  • 您不应该配置您的应用程序并让它尝试在本地查找 SMTP 服务器(就像您上面提到的那样)。如果你想让它工作,你需要有一个本地运行的 SMTP 服务器。这是常识,我认为不需要在某处记录。
  • 什么?常识知道 IIS 中的 SMTP 功能不提供 SMTP 功能?这怎么可能是常识????
  • 请再次阅读 Microsoft 文章,technet.microsoft.com/en-us/library/cc772058%28WS.10%29.aspx。请注意,您将看到“在 SMTP 服务器文本框中键入 SMTP 服务器的唯一名称或选择使用 localhost 框将名称设置为 LocalHost”。常识是,如果本地没有本地 SMTP 服务(如 Windows 7 的情况),你应该做“或”之前的事情,比如使用 GMail SMTP 服务器或其他。如果您打算执行“或”之后的操作,就像@Dima 指出的那样,您需要在本地安装第三方 SMTP 服务器。
【解决方案3】:

嗯,我同意 OP。 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);

截至本文发布之日,上述两种方法都可与 GMail 一起使用,使用 IIS7 且无需进行任何额外配置。

【讨论】:

    猜你喜欢
    • 2014-05-23
    • 2010-09-18
    • 2011-05-04
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2012-09-30
    • 1970-01-01
    相关资源
    最近更新 更多