我同意楼主的观点。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一起使用它(这些直接来自包中提供的示例):
error_reporting(E_ALL);
define('DISPLAY_XPM4_ERRORS', true);
require_once '../MAIL.php';
$m = new MAIL;
$m->From('username@gmail.com');
$m->AddTo('client@destination.net');
$m->Subject('Hello World!');
$m->Html('<b>HTML</b> <u>message</u>.');
$c = $m->Connect('smtp.gmail.com', 465, 'username@gmail.com', 'password', 'tls', 10,
'localhost', null, 'plain')
or die(print_r($m->Result));
echo $m->Send($c) ? 'Mail sent !' : 'Error !';
$m->Disconnect();
我使用的是IIS7 Express,它自带FastCGI PHP模块并启用了OpenSSL扩展支持。这样就可以在消息内容中使用HTML标签。下面展示了使用xpm4包的第二种方式,用于纯文本消息(同样,示例来自包源代码):
error_reporting(E_ALL);
define('DISPLAY_XPM4_ERRORS', true);
require_once '../SMTP.php';
$f = 'username@gmail.com';
$t = 'client@destination.net';
$p = 'password';
$m = 'From: '.$f."\r\n".
'To: '.$t."\r\n".
'Subject: test'."\r\n".
'Content-Type: text/plain'."\r\n\r\n".
'Text message.';
$c = SMTP::connect('smtp.gmail.com', 465, $f, $p, 'tls', 10) or die(print_r($_RESULT));
$s = SMTP::send($c, array($t), $m, $f);
if ($s) echo 'Sent !';
else print_r($_RESULT);
SMTP::disconnect($c);
截至本篇文章发布日期,两者都可以在使用IIS7的情况下与GMail配合使用,无需进行任何额外的配置。
mail()
发送。当您尝试发送电子邮件时,会出现什么错误? - DaveRandommail()
函数是正常工作的,但 SMTP 服务器正在拒绝它。我会首先使用 Wireshark 查看正在交换的原始 SMTP 数据包。然而,需要注意的是:在 Windows 上,您无法在 Wireshark 中监听 127.0.0.1 的流量。因此,在调试时,您必须通过远程机器进行跳转(除非您通过有线网卡或无线网卡进行切换)。 - DaveRandom