![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
由于我本机没有smtp,我使用smtp的时候,就是采用了网易163的
代码如下
[php]include_once ("email.php");
$sendmail = new Mail_SMTP("smtp.163.com","25","5");
$sendmail->connect('name','pass');
[/php]
name是我的用户名,我的真实用户名没有写在上面
pass是我的密码
email.php是一个mail类,代码如下, 我哪里错了?
他总是提示我:
Ran into problems sending Mail. Response: 550 : Invalid User
我用户应该没有错呀,我可以直接登录的~
[php]<?
class Mail_SMTP
{
var $host = 'smtp.163.com'; // SMTP主机名
var $port = 25; // 主机的smtp端口,一般是25号端口
var $timeout = 5; // 连接主机的最大超时时间
var $debug = 0; // 做为标识,是否在调试状态,是的话,输出调试信息
var $showerr = true;
var $handle;
function Mail_SMTP($server = 'smtp.163.com', $port = 25, $time_out = 5){
$this ->host = $server;
$this ->port = $port;
$this ->timeout = $time_out;
return true;
}
function errmsg($err,$show=false){
if ($this->showerr || $show){
echo $err."
";
exit();
}
}
function _command($cmd, $response){
if (!(fputs($this->handle,$cmd . "\r\n"))){
$this->errmsg("Couldn't send command to server");
return false;
}
while ( substr($server_response,3,1) != ' ' ){
if( !( $server_response = fgets($this->handle, 256))){
$this->errmsg("Couldn't get mail server response codes");
}
}
if( !( substr($server_response, 0, 3) == $response )){
$this->errmsg( "Ran into problems sending Mail. Response: $server_response");
}
}
function connect($user = '',$pass = ''){
if( !$this->handle = fsockopen($this->host, 25, $errno, $errstr, 20)){
$this->errmsg( "Could not connect to smtp host : $errno : $errstr");
}
$r = fgets($this->handle, 256);
if ( substr($r, 0, 3) != "220"){
$this->errmsg("couldn't connection to smtp server");
return false;
}
if( $user != '' && $pass != ''){
// Send the RFC2554 specified EHLO.
// This improved as provided by SirSir to accomodate
// both SMTP AND ESMTP capable servers
$this->_command("EHLO \"" . $this->host . "\"", "250");
$this->_command("AUTH LOGIN","334");
$this->_command(base64_encode($user), "334");
$this->_command(base64_encode($pass), "235");
}
else{
// Send the RFC821 specified HELO.
$this->_command("HELO \"" . $this->host . "\"", "250");
}
}
function sendmail($header, $body = ''){
//header
if (isset($header)){
if(is_array($header)){
if(sizeof($header) > 1){
$header = join("\r\n", $header);
}
else{
$header = $header[0];
}
}
$header = chop($header);
$header = preg_replace("/(?.*)/si", "\\1", $header);
}
elseif ( preg_match("/^to:/si", $header)){
$to = preg_replace("/^to
.*)/si", "\\1", $header);
}
elseif ( preg_match("/^subject:/si", $header)){
$subject = preg_replace("/^subject
.*)/si", "\\1", $header);
}
elseif ( preg_match("/^cc:/si", $header)){
$cc = preg_replace("/^cc
.*)/si", "\\1", $header);
}
elseif( preg_match("/^bcc:/si", $header )){
$bcc = preg_replace("/^bcc
.*)/si", "\\1", $header);
$header = "";
continue;
}
$headers .= $header . "\r\n";
}
$headers = chop($headers);
}
//body
$body = preg_replace("/(?errmsg("No email address specified");
}
if(trim($subject) == ""){
$this->errmsg( "No email Subject specified");
}
$to = explode(",", $to);
$cc = explode(",", $cc);
$bcc = explode(",", $bcc);
$this->_command("MAIL FROM: " . $from, "250");
@reset( $to );
while( list( , $to_address ) = each( $to )){
$to_address = trim($to_address);
if ( preg_match('/[^ ]+\@[^ ]+/', $to_address)){
$this->_command("RCPT TO: <$to_address>", "250");
}
}
// Ok now do the CC and BCC fields...
@reset( $bcc );
while( list( , $bcc_address ) = each( $bcc )){
$bcc_address = trim( $bcc_address );
if ( preg_match('/[^ ]+\@[^ ]+/', $bcc_address)){
$this->_command("RCPT TO: <$bcc_address>", "250");
}
}
@reset( $cc );
while( list( , $cc_address ) = each( $cc )){
$cc_address = trim( $cc_address );
if ( preg_match('/[^ ]+\@[^ ]+/', $cc_address)){
$this->_command("RCPT TO: <$cc_address>", "250");
}
}
// Ok now we tell the server we are ready to start sending data
$this->_command("DATA", "354");
$bodys = $headers . "\r\n\r\n" . $body . "\r\n";
fputs($this->handle, $bodys);
$this->_command("\r\n.", "250");
return TRUE;
}
function quit(){
// Now tell the server we are done and close the socket...
fputs($this->handle, "QUIT\r\n");
fclose($this->handle);
}
}//end class
?>[/php]

