嗯,我同意 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 且无需进行任何额外配置。