<?
//作者:秋衫客
print <<< eof
<title>邮件在线发送工具</title>
<style type=text/css>
body{font-size:9pt;background-color:#eeeeee}
td{font-size:9pt}
input,textarea{border-width:1;border-color:#dddddd;font-family:verdana;}
a:link,a:visited{color:#000000;text-decoration:none}
a:hover{color:#7abc01;text-decoration:none;position:relative;left:1;top:1}
</style>
<center><font size=+1><b>邮件在线发送工具</b></font> (<B>利用Socket技术</B>)</center><br/>
eof; set_time_limit(120);
if(!$submit||$submit=="")
{
print <<< eof
<table border=0 cellspacing=1 bgcolor=#000000 width=80% align=center>
<tr bgcolor=#dddddd><td colspan=2><center><b>邮件发送 (<font color=red>所有项都必须正确填写,否则不能正常发送。</font>)</b></center></td></tr>
<form action=$PHP_SELF method=post>
<tr bgcolor=#ffffff><td width=40%><p align=right>发信地址:</p></td><td><input type=text name=from> * 推荐使用网易信箱</td></tr>
<tr bgcolor=#ffffff><td><p align=right>发信箱密码:</p></td><td><input type=password name=password></td></tr>
<tr bgcolor=#ffffff><td><p align=right>发信者:</p></td><td><input type=text name=from_name></td></tr>
<tr bgcolor=#ffffff><td><p align=right>收信地址:</p></td><td><input type=text name=to></td></tr>
<tr bgcolor=#ffffff><td><p align=right>收信者:</p></td><td><input type=text name=to_name></td></tr>
<tr bgcolor=#ffffff><td><p align=right>主题:</p></td><td><input type=text name=subject>
<input type=checkbox name=html value=1> 发送超文本信件</td></tr>
<tr bgcolor=#dddddd><td colspan=2><center>邮件内容</center></td></tr>
<tr bgcolor=#ffffff><td colspan=2><center><textarea name=data cols=80 rows=10></textarea>
</center></td></tr>
<tr bgcolor=#dddddd><td colspan=2><center><input type=submit value=发送 name=submit>
<input type=reset value=重置></center></td></tr>
</form>
</table>
eof;
}
else
{
if($from==""||$password==""||$to==""||$subject==""||$data==""||!ereg("^.+@.+\\..+$",$from)||!ereg("^.+@.+\\..+$",$from)){
print "<script>alert('资料填写不完整或者格式出错。');history.back();</script>";
exit;
}
$temp=split("@",$from);
$host="smtp." .$temp[1];
$port=25;
$username=$temp[0];
send_mail($host,$port,$username,$password,$from,$from_name,$to,$to_name,$subject,$data,$html);
}
print <<< eof
<br/>
eof;
function send_mail($host,$port,$username,$password,$from,$from_name,$to,$to_name,$subject,$data,$html=0){////////邮件Socket发送函数定义 $fp=@fsockopen($host,$port)or die("无法连接到远程主机!");
$temp=fgets($fp,512);
echo $temp;
die("aa");
//连接远程服务器
//向服务器打招呼
fputs($fp,"HELO $username\n");
$temp=fgets($fp,512);

//ESTMP验证
fputs($fp,"AUTH LOGIN\n");
$temp=ltrim(fgets($fp,512));

fputs($fp,base64_encode($username)."\n");
$temp=ltrim(fgets($fp,512)); fputs($fp,base64_encode($password) ."\n");
$temp=ltrim(fgets($fp,512));

if(substr($temp,0,3)!="235")
{
$result.="<center><font color=red>密码不对!</font></center><br/>";
$err=1;
}
if($err!=1)
{
fputs($fp,"mail from:$from\n");
$temp=ltrim(fgets($fp,512));

fputs($fp,"rcpt to:$to\n");
$temp=ltrim(fgets($fp,512));

fputs($fp,"data\n");
$temp=ltrim(fgets($fp,512));
//$temp=eregi_replace("<","&lt;",$temp);
//$temp=eregi_replace(">","&gt;",$temp);

fputs($fp,"To:" .$to ."\n");
fputs($fp,"From:$from_name<" .$from .">\n");
fputs($fp,"Subject:" .$subject ."\n");
if($html==1)
{
fputs($fp,"Content-type:text/html\n");
}
fputs($fp,"$data\n");
fputs($fp,".\n");
$temp=ltrim(fgets($fp,512));
if(substr($temp,0,3)!="250")
{
$result.="<center>由于不可知原因,邮件没有发送成功。</center><br/>";
}
else
{
$result.="<center><font color=red>发送成功!</font></center><br/>";
}
fputs($fp,"quit\n");
$temp=ltrim(fgets($fp,512));
}
print $result;
global $PHP_SELF;
print "<center><br/><a href=$PHP_SELF><b>发送新信件</b></a><br/>";
fclose($fp);
}
?>

解决方案 »

  1.   

    谢谢,kingerq(多菜鸟),需要对运行环境作什么设置吗?
      

  2.   

    方法一:(不好)<form action=mailto:你的管理员邮件地址 name=form1>
    表单内容
    </form>方法二:下面是一个类,方便你重用。<?php define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
    define('SMTP_STATUS_CONNECTED', 2, TRUE); class smtp{ var $connection;
    var $recipients;
    var $headers;
    var $timeout;
    var $errors;
    var $status;
    var $body;
    var $from;
    var $host;
    var $port;
    var $helo;
    var $auth;
    var $user;
    var $pass; function smtp($params = array()){ if(!defined('CRLF'))
    define('CRLF', "\r\n", TRUE);

    $this->timeout = 5;
    $this->status = SMTP_STATUS_NOT_CONNECTED;
    $this->host = 'localhost';
    $this->port = 25;
    $this->helo = 'localhost';
    $this->auth = FALSE;
    $this->user = '';
    $this->pass = '';
    $this->errors   = array(); foreach($params as $key => $value){
    $this->$key = $value;
    }
    } function connect($params = array()){ if(!isset($this->status)){
    $obj = new smtp($params);
    if($obj->connect()){
    $obj->status = SMTP_STATUS_CONNECTED;
    } return $obj; }else{
    $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
    socket_set_timeout($this->connection, 0, 250000); $greeting = $this->get_data();
    if(is_resource($this->connection)){
    return $this->auth ? $this->ehlo() : $this->helo();
    }else{
    $this->errors[] = 'Failed to connect to server: '.$errstr;
    return FALSE;
    }
    }
    } function send($params = array()){ foreach($params as $key => $value){
    $this->set($key, $value);
    } if($this->is_connected()){ if($this->auth){
    if(!$this->auth())
    return FALSE;
    } $this->mail($this->from);
    if(is_array($this->recipients))
    foreach($this->recipients as $value)
    $this->rcpt($value);
    else
    $this->rcpt($this->recipients); if(!$this->data())
    return FALSE; $headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
    $body    = str_replace(CRLF.'.', CRLF.'..', $this->body);
    $body    = $body[0] == '.' ? '.'.$body : $body; $this->send_data($headers);
    $this->send_data('');
    $this->send_data($body);
    $this->send_data('.'); return (substr(trim($this->get_data()), 0, 3) === '250');
    }else{
    $this->errors[] = 'Not connected!';
    return FALSE;
    }
    } function helo(){
    if(is_resource($this->connection)
    AND $this->send_data('HELO '.$this->helo)
    AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){ return TRUE; }else{
    $this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
    return FALSE;
    }
    } function ehlo(){
    if(is_resource($this->connection)
    AND $this->send_data('EHLO '.$this->helo)
    AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){ return TRUE; }else{
    $this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
    return FALSE;
    }
    }

    function auth(){
    if(is_resource($this->connection)
    AND $this->send_data('AUTH LOGIN')
    AND substr(trim($error = $this->get_data()), 0, 3) === '334'
    AND $this->send_data(base64_encode($this->user))
    AND substr(trim($error = $this->get_data()),0,3) === '334'
    AND $this->send_data(base64_encode($this->pass))
    AND substr(trim($error = $this->get_data()),0,3) === '235' ){ return TRUE; }else{
    $this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
    return FALSE;
    }
    }

    function mail($from){ if($this->is_connected()
    AND $this->send_data('MAIL FROM:<'.$from.'>')
    AND substr(trim($this->get_data()), 0, 2) === '250' ){ return TRUE; }else
    return FALSE;
    }

    function rcpt($to){ if($this->is_connected()
    AND $this->send_data('RCPT TO:<'.$to.'>')
    AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){ return TRUE; }else{
    $this->errors[] = trim(substr(trim($error), 3));
    return FALSE;
    }
    } function data(){ if($this->is_connected()
    AND $this->send_data('DATA')
    AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
     
    return TRUE; }else{
    $this->errors[] = trim(substr(trim($error), 3));
    return FALSE;
    }
    } function is_connected(){ return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
    } function send_data($data){ if(is_resource($this->connection)){
    return fwrite($this->connection, $data.CRLF, strlen($data)+2);
    }else
    return FALSE;
    } function &get_data(){ $return = '';
    $line   = ''; if(is_resource($this->connection)){
    while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' '){
    $line    = fgets($this->connection, 512);
    $return .= $line;
    }
    return $return; }else
    return FALSE;
    }

    function set($var, $value){ $this->$var = $value;
    return TRUE;
    }
    }
    ?>用法:$params['host'] = 'smtp.163.com';
    $params['port'] = 25;
    $params['helo'] = 'localhost';
    $params['auth'] = TRUE;
    $params['user'] = 'xxx';
    $params['pass'] = 'yyy';
    $send_params['recipients'] = array( $_POST['email'] );
    $send_params['headers']= array('From: "kindergarder" <[email protected]>',To: ' . $_POST['email'],'Subject: 主题');
    $send_params['from'] = '[email protected]';
    $send_params['body'] = '内容';
    $smtp = smtp::connect( $params );
    $smtp->send( $send_params ) );