那么我同意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而不必做任何额外的配置。
除非您尝试将电子邮件发送到地址'@ localhost',否则不需要运行本地SMTP服务器以使用'mail()'发送。当你尝试发送电子邮件时,你会得到什么错误? – DaveRandom
我不想运行本地smtp服务器 - 我想在本地主机上侦听并将邮件传递到远程邮件服务器。 (我在IIS SMTP功能中配置) –
基于该错误,'mail()'正在工作,但SMTP服务器拒绝它。我要做的第一件事是获取[Wireshark](http://www.wireshark.org/),并查看正在交换的原始SMTP。但是,警告:您无法在Windows上的Wireshark上监听127.0.0.1上的流量。在调试时,您必须通过远程计算机进行反弹(除非您从无线NIC进入有线NIC,反之亦然)... – DaveRandom