在windows-7 is-7.5上设置smtp

时间:2021-08-24 15:17:20

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.)

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

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.

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

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?

我运行了一个netstat -an,但是在port25上没有监听——我是否需要做一些其他的事情来让smtp服务监听端口25?

The error I'm receiving:

我收到的错误:

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警告:邮件()函数。:在“localhost”端口25连接到mailserver失败,请在php中验证您的“SMTP”和“smtp_port”设置。ini或使用报错()

php.ini:

php . ini中:

SMTP = localhost
smtp_port = 25

3 个解决方案

#1


16  

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

您可以使用类似smtp4dev (http://smtp4dev.codeplex.com/)之类的东西来代替iis进行测试。对我来说,这是一种魅力。

#2


2  

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.

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

#3


1  

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.

我同意opp, W7(甚至是Ultimate)没有SMTP服务器(我很确定我们在Vista 64 Ultimate上有它,甚至可能是XP上有它),所以您必须确定要使用的服务器,无论是本地的还是远程的。

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

如果服务器没有使用授权,那么这应该可以正常工作,而不必使用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);

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

如果服务器正在使用纯文本授权(而不是TLS/SSL),那么根据您的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):

如果服务器强制使用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();

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):

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

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

以上两种方法都适用于GMail,在本文发布时使用IIS7,并且无需进行任何额外配置。

#1


16  

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

您可以使用类似smtp4dev (http://smtp4dev.codeplex.com/)之类的东西来代替iis进行测试。对我来说,这是一种魅力。

#2


2  

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.

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

#3


1  

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.

我同意opp, W7(甚至是Ultimate)没有SMTP服务器(我很确定我们在Vista 64 Ultimate上有它,甚至可能是XP上有它),所以您必须确定要使用的服务器,无论是本地的还是远程的。

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

如果服务器没有使用授权,那么这应该可以正常工作,而不必使用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);

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

如果服务器正在使用纯文本授权(而不是TLS/SSL),那么根据您的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):

如果服务器强制使用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();

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):

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

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

以上两种方法都适用于GMail,在本文发布时使用IIS7,并且无需进行任何额外配置。