/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass,$thistimes="")
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = ""; $this->sock = FALSE;
$this->times = $thistimes;
//$this->attfile=$file;
//echo($thistimes);
} /* Main Function */
function sendmail($tmpfile="",$iffile=false,$tmpname="",$tmptype,$to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") {
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\\.)", "\\1.\\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($iffile){
$header.= "Content-Type: multipart/mixed; boundary=\"----=_Part_12038_5896801.1067927729644\"\r\n";
}
else{
$header .= "Content-Type:text/html\r\n";
} $header .= "To: ".$to."\r\n";
if ($cc != "") {
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: Friend Studio<$from>\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n"; $header .= "socket mailsender by jxxysong\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; $TO = explode(",", $this->strip_comment($to)); if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));//将$cc合并到发送数组中
} if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));//将$bcc合并到发送数组中
} $sent = TRUE; if($iffile){
$body="------=_Part_12038_5896801.1067927729644\r\nContent-Type: text/html; charset=\"GBK\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n".$body;
$body.="\r\n------=_Part_12038_5896801.1067927729644\r\nContent-Type: ".$tmptype."; name=".$tmpname."\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=".$tmpname."\r\n\r\n".$tmpfile;
$body.="------=_Part_12038_5896801.1067927729644--";
} foreach ($TO as $rcpt_to) { $rcpt_to = $this->get_address($rcpt_to); if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
echo("发送成功!");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
$sent = FALSE;
echo("发送失败!");
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
return $sent;
} /* Private Functions */ function smtp_send($helo, $from, $to, $header, $body = "")
{
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass,$thistimes="")
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = ""; $this->sock = FALSE;
$this->times = $thistimes;
//$this->attfile=$file;
//echo($thistimes);
} /* Main Function */
function sendmail($tmpfile="",$iffile=false,$tmpname="",$tmptype,$to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") {
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\\.)", "\\1.\\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($iffile){
$header.= "Content-Type: multipart/mixed; boundary=\"----=_Part_12038_5896801.1067927729644\"\r\n";
}
else{
$header .= "Content-Type:text/html\r\n";
} $header .= "To: ".$to."\r\n";
if ($cc != "") {
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: Friend Studio<$from>\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n"; $header .= "socket mailsender by jxxysong\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; $TO = explode(",", $this->strip_comment($to)); if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));//将$cc合并到发送数组中
} if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));//将$bcc合并到发送数组中
} $sent = TRUE; if($iffile){
$body="------=_Part_12038_5896801.1067927729644\r\nContent-Type: text/html; charset=\"GBK\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n".$body;
$body.="\r\n------=_Part_12038_5896801.1067927729644\r\nContent-Type: ".$tmptype."; name=".$tmpname."\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=".$tmpname."\r\n\r\n".$tmpfile;
$body.="------=_Part_12038_5896801.1067927729644--";
} foreach ($TO as $rcpt_to) { $rcpt_to = $this->get_address($rcpt_to); if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
echo("发送成功!");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
$sent = FALSE;
echo("发送失败!");
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
return $sent;
} /* Private Functions */ function smtp_send($helo, $from, $to, $header, $body = "")
{
return $this->smtp_error("sending HELO command");
}
#auth
if($this->auth){
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
return $this->smtp_error("sending HELO command");
} if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("sending HELO command");
}
}
#
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
return $this->smtp_error("sending MAIL FROM command");
} if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
echo("发送失败!");
return $this->smtp_error("sending RCPT TO command");
} if (!$this->smtp_putcmd("DATA")) {
return $this->smtp_error("sending DATA command");
} if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
} if (!$this->smtp_eom()) {
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
} if (!$this->smtp_putcmd("QUIT")) {
return $this->smtp_error("sending QUIT command");
} return TRUE;
} function smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
} function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."\n");
return TRUE;;
} function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\\1", $address);
if (!@getmxrr($domain, $MXHOSTS)) {
$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
return FALSE;
}
foreach ($MXHOSTS as $host) {
$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Warning: Cannot connect to mx host ".$host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
continue;
}
$this->log_write("Connected to mx host ".$host."\n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
return FALSE;
} function smtp_message($header, $body)
{ //fputs($this->sock, $header."\r\n".$body.$attfile_name);
fputs($this->sock, $header."\r\n".$body);
$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); return TRUE;
} function smtp_eom()
{
fputs($this->sock, "\r\n.\r\n");
$this->smtp_debug(". [EOM]\n"); return $this->smtp_ok();
} function smtp_ok()
{
$response = str_replace("\r\n", "", fgets($this->sock, 512));
$this->smtp_debug($response."\n"); if (!ereg("^[23]", $response)) {
fputs($this->sock, "QUIT\r\n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned \"".$response."\"\n");
return FALSE;
}
return TRUE;
} function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
} fputs($this->sock, $cmd."\r\n");
$this->smtp_debug("> ".$cmd."\n"); return $this->smtp_ok();
} function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".\n");
return FALSE;
} function log_write($message)
{
$this->smtp_debug($message); if ($this->log_file == "") {
return TRUE;
} $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
return FALSE;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp); return TRUE;
}
{
$comment = "\\([^()]*\\)";
while (ereg($comment,$address)) {
$address = ereg_replace($comment, "", $address);
} return $address;
} function get_address($address)
{
$address = ereg_replace("([ \t\r\n])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\\1", $address); return $address;
} function smtp_debug($message)
{
if ($this->debug) {
echo $message;
}
}
}
?>
mymail.php
<?
require("sm.php");
$smtpserver = "";//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = "[email protected]";//SMTP服务器的用户邮箱(您的邮箱)
$smtpuser = "xxx";//SMTP服务器的用户帐号
$smtppass = "xxxx";//SMTP服务器的用户密码
$mailtype = "TXT";//邮件格式(HTML/TXT),TXT为文本邮件
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Mail Post</title>
</head><?
if(isset($_GET["stats"])){
$file2=$_FILES["file"];
$toadd=$_POST["toadd"];
$subject=$_POST["subject"];
$Message=$_POST["Message"];
if (!$toadd||!$subject||!$Message)
{
echo "请正确填写收件人,主题和邮件内容";
echo "<br>";
echo "<a href=javascript:history.back()>点这里返回</a>";
exit;
}
$oldmessage=$Message;
$Message= str_replace(" ", " ", $Message);
$Message= str_replace("<", "<", $Message);
$Message= str_replace(">", ">", $Message);
$Message= str_replace("\"", """, $Message);
$Message=nl2br($Message); if($file2=="none"){
//if($file2==""){ //类UNIX系统中不要用NONE
$hasfile=false;
}
else{
$hasfile=true;
$file_name=$_FILES['file']['name'];
echo "附件为:".basename($file_name);#################################################################################################switch(strrchr(basename($file_name),"."))
{
case ".xls":
$content_type="application/excel";
break;
case ".hqx":
$content_type="application/macbinhex40";
break;
case ".doc":
case ".dot":
case ".wrd":
$content_type="audio/wav";
break;
case ".gif":
$content_type="image/gif";
break;
case ".jpg":
case ".jpe":
case ".jpeg":
$content_type="image/jpeg";
break;
case ".png":
$content_type="image/png";
break;
case ".tif":
case ".tiff":
$content_type="image/tiff";
break;
case ".css":
$content_type="text/css";
break;
case ".txt":
$content_type="text/plain";
break;
case ".htm":
case ".html":
$content_type="text/html";
break;
case ".xml":
$content_type="text/xml";
break;
case ".mpg":
case ".mpe":
case ".mpeg":
$content_type="video/mpeg";
break;
case ".qt":
case ".mov":
$content_type="video/quicktime";
break;
case ".avi":
$content_type="video/x-ms-video";
break;
default:
$content_type="application/octet-stream";
break;
}
#################################################################################################
$file=$_FILES["file"];
$attachment = fread(fopen($file, "rb"), filesize($file));
$attachment = chunk_split(base64_encode($attachment));//将字串 $attachment 格式化成 MIME BASE64 格式
}//end $hasfile=true//________________________________________________________________________________
$smtpemailto = $toadd;//发送给谁
$mailsubject = $subject;//邮件主题
$mailbody = $Message;//邮件内容$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
//$smtp->debug = true;//是否显示发送的调试信息//$attachment=>附件的编码,$hasfile=>是否含有附件,$file_name=>附件的名称,$content_type=>附件的类型
//$smtpemailto=>收信邮箱,$smtpusermail=>发信邮箱,$mailsubject=>邮件主题,$mailbody=>邮件内容,$mailtype=>邮件类型
$smtp->sendmail($attachment,$hasfile,$file_name,$content_type,$smtpemailto,$smtpusermail,$mailsubject,$mailbody,$mailtype);
}?>
<body>
<table width="480" border="0" cellspacing="1" bgcolor="#000066">
<form action="<? echo($_SERVER["PHP_SELF"]."?stats=new")?>" method="post" enctype="multipart/form-data" name="form1">
<tr>
<td width="495" valign="top" bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="0">
<tr height="10">
<td colspan="4"></td>
</tr>
<tr>
<td width="3"> </td>
<td width="51" align="right"><font color="#000066">收件人:</font></td>
<td width="266"><input type="text" style="width:260;BORDER-RIGHT: #000066 1px solid; BORDER-TOP: #000066 1px solid; FONT: 12px Verdana; BORDER-LEFT: #000066 1px solid; COLOR: #666666; BORDER-BOTTOM: #000066 1px solid; BACKGROUND-COLOR: #ffffff"name="toadd">
</td>
<td width="148" rowspan="4" align="left" valign="top"> </td>
</tr>
<tr>
<td> </td>
<td align="right"><font color="#000066">主 题:</font></td>
<td><input type="text" style="width:260;BORDER-RIGHT: #000066 1px solid; BORDER-TOP: #000066 1px solid; FONT: 12px Verdana; BORDER-LEFT: #000066 1px solid; COLOR: #666666; BORDER-BOTTOM: #000066 1px solid; BACKGROUND-COLOR: #ffffff"name="subject"></td>
</tr>
<tr>
<td> </td>
<td align="right"><font color="#000066">日 期:</font></td>
<td><input type="text" style="width:260;BORDER-RIGHT: #000066 1px solid; BORDER-TOP: #000066 1px solid; FONT: 12px Verdana; BORDER-LEFT: #000066 1px solid; COLOR: #666666; BORDER-BOTTOM: #000066 1px solid; BACKGROUND-COLOR: #ffffff" readonly name="date" value="<? echo(date("Y-m-d h:i:s"))?>"></td>
</tr>
<tr>
<td> </td>
<td align="right"><font color="#000066">附 件:</font></td>
<td><input type="file" style="width:260;BORDER-RIGHT: #000066 1px solid; BORDER-TOP: #000066 1px solid; FONT: 12px Verdana; BORDER-LEFT: #000066 1px solid; COLOR: #666666; BORDER-BOTTOM: #000066 1px solid; BACKGROUND-COLOR: #ffffff" name="file"></td>
</tr>
<tr align="left">
<td> </td>
<td colspan="3"><table width="97%" border="0" cellspacing="0">
<tr height="5">
<td align="center"></td>
</tr>
<tr>
<td align="left"> <textarea name="Message" cols=72 rows=10 wrap="PHYSICAL" id="Message" style="BORDER-RIGHT: #000066 1px solid; BORDER-TOP: #000066 1px solid; FONT: 12px Verdana; BORDER-LEFT: #000066 1px solid; COLOR: #666666; BORDER-BOTTOM: #000066 1px solid; BACKGROUND-COLOR: #ffffff" onFocus=this.select() ></textarea></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td align="right"><input type="hidden" name="filename"></td>
<td colspan="2" align="right"> <table width="200" height="23" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="8%"> </td>
<td width="46%" align="right">
<input type=submit value=send style="width:50;BORDER-RIGHT: #000066 1px solid; BORDER-TOP: #000066 1px solid; FONT: 12px Verdana; BORDER-LEFT: #000066 1px solid; COLOR: #666666; BORDER-BOTTOM: #000066 1px solid; BACKGROUND-COLOR: #ffffff">
</td>
<td width="4%"> </td>
</tr>
</table>
<label></label></td>
</tr>
<tr valign="top">
<td> </td>
<td colspan="2"> </td>
<td> </td>
</tr>
</table>
</td>
</tr></form>
</table>
</body>
<style>
td{font-size:9pt}
BODY {
SCROLLBAR-FACE-COLOR: white; SCROLLBAR-HIGHLIGHT-COLOR: white; SCROLLBAR-SHADOW-COLOR: white; SCROLLBAR-3DLIGHT-COLOR: white; SCROLLBAR-ARROW-COLOR: #b9dcff; SCROLLBAR-TRACK-COLOR: white; SCROLLBAR-DARKSHADOW-COLOR: white;scrollbar-arrow-color: #000066;}
.smalltext1{font-family: "Arial", "Helvetica", "sans-serif";font-size: 10px;color:#000066}
.smalltext{font-family: "Arial", "Helvetica", "sans-serif";font-size: 10px;}
</style>
</html>