9.Mail/smtp
对于现在有些站点不允许使用sendmail,那么如果你的php程序希望使用发信功能,就需要能够通过使用外部的smtp服务器来完成相应的功能了。
使用方法:使用上这个模块和Mail::sendmail基本上是一样的。需要注意的是:这个模块需要使用Net::SMTP模块:Mail_smtp($params)
$params的有效参数是:
'host' smtp的服务器地址,缺省是 localhost
'port' smtp服务端口,缺省是25
'auth' smtp是否需要授权验证,缺省是false
'usename' smtp授权的用户名
'password' smtp授权的密码
send($recipients, $headers, $body)
发送
<?php
require_once "Mail/sendmail.php";
$params=array('host'=>'smtp.nightsailer.com','auth'=true,
'username'=>'night','password'=>'123456');
$sendmail = new Mail_sendmail($params);
$header = array('Subject'=>'Hello','BCC'=>'test2@hotmail.com');
$body = 'This is a test message from nightsailer.com';
$result = $sendmail->send('test@nightsailer.com', $header, $body);
if ( PEAR::isError($result) ){
echo "<h1> 发送失败 </h1><br>原因:".$result->getMessage()."<br>";
}else {
echo "<h1>恭喜!发送成功!</h1><br>";
}
?>